import logging
import os

from twisted.internet.utils import getProcessOutput
from twisted.internet.defer import succeed

from landscape.package.store import PackageStore
from landscape.package.changer import PackageChanger
from landscape.package.releaseupgrader import ReleaseUpgrader
from landscape.manager.manager import ManagerPlugin


class PackageManager(ManagerPlugin):

    run_interval = 1800
    _package_store = None

    def register(self, registry):
        super(PackageManager, self).register(registry)
        self.config = registry.config

        if not self._package_store:
            filename = os.path.join(registry.config.data_path,
                                    "package/database")
            self._package_store = PackageStore(filename)

        registry.register_message("change-packages",
                                  self.handle_change_packages)
        registry.register_message("release-upgrade",
                                  self.handle_release_upgrade)

        self.run()

    def _handle(self, cls, message):
        """Queue C{message} as a task, and spawn the proper handler."""
        self._package_store.add_task(cls.queue_name, message)
        self.spawn_handler(cls)

    def handle_change_packages(self, message):
        return self._handle(PackageChanger, message)

    def handle_release_upgrade(self, message):
        return self._handle(ReleaseUpgrader, message)

    def run(self):
        result = self.registry.broker.get_accepted_message_types()
        result.addCallback(self._got_message_types)
        return result

    def _got_message_types(self, message_types):
        if "change-packages-result" in message_types:
            self.spawn_handler(PackageChanger)
        if "operation-result" in message_types:
            self.spawn_handler(ReleaseUpgrader)

    def spawn_handler(self, cls):
        args = ["--quiet"]
        if self.config.config:
            args.extend(["-c", self.config.config])
        if self._package_store.get_next_task(cls.queue_name):
            # path is set to None so that getProcessOutput does not
            # chdir to "." see bug #211373
            result = getProcessOutput(cls.find_command(),
                                      args=args, env=os.environ,
                                      errortoo=1,
                                      path=None)
            result.addCallback(self._got_output, cls)
        else:
            result = succeed(None)
        return result

    def _got_output(self, output, cls):
        if output:
            logging.warning("Package %s output:\n%s" %
                            (cls.queue_name, output))
