Exemplo n.º 1
0
    def __unload_hook(self, pkg):
        """
        This is the real unload procedure of plugin.
        Raise a PluginException on fail

        @return None or raise a PluginException
        """

        if not pkg in self.instances:
            raise PluginException(pkg, "Already unloaded")

        for inst in self.instances[pkg]:
            try:
                inst.stop()
            except Exception, err:
                log.critical("Error while stopping %s from %s:" % (inst, pkg))
                log.critical(generate_traceback())
                log.critical("Ignoring instance.")
Exemplo n.º 2
0
    def load_directory(self, modpath):
        if not os.environ.get('UMIT_DEVELOPMENT', False):
            log.error("This method should not be called in release.")
            return

        start_file = 'main'

        log.warning("You are loading a plugin without checking for needs,provides,conflitcts")
        log.warning("* You have been warned! *")

        log.warning("Assuming `%s' as start file!" % start_file)

        # Load the plugin
        sys.path.insert(0, os.path.abspath(modpath))

        if start_file in sys.modules:
            sys.modules.pop(start_file)

        try:
            __builtin__.__import__ = hook_import
            module = hook_import(start_file, level=0)

            if hasattr(module, "__plugins__") and \
               isinstance(module.__plugins__, list):
                lst = module.__plugins__
                ret = []

                for plug in lst:
                    try:
                        inst = plug()
                        inst.start(None)

                        ret.append(inst)
                    except Exception, err:
                        log.critical("Error while starting %s:" % (plug))
                        log.critical(generate_traceback())
                        log.critical("Ignoring instance.")

                if not ret:
                    log.error("Not startable plugin defined in main file")
            else:
Exemplo n.º 3
0
    def __load_hook(self, pkg):
        """
        This is the real load procedure of plugin.
        We'll use zipmodule to import and a global function expose
        to provide a simple method to access files inside the zip file
        to plugin.

        Raise a PluginException on fail

        @return None or raise a PluginException
        """

        if pkg in self.instances:
            raise PluginException(pkg, "Already present")
        
        # We need to get the start-file field from pkg and then try
        # to import it
        
        modpath = os.path.join(pkg.get_path(), 'lib')
        sys.path.insert(0, os.path.abspath(modpath))

        # This were removed
        fname = os.path.join(pkg.get_path(), 'bin', pkg.start_file)
        sys.path.insert(0, os.path.abspath(os.path.dirname(fname)))
        
        if pkg.start_file in sys.modules:
            sys.modules.pop(pkg.start_file)

        try:
            try:
                # We add to modules to avoid deleting and stop working plugin ;)
                sys.plugins_path.insert(0, pkg)
                module = self.__cache_import(pkg)
            
            except Exception, err:
                sys.plugins_path.pop(0)
                raise PluginException(pkg, str(err))
        
        finally:
            # Check that
            sys.path.pop(0)

        if hasattr(module, "__plugins__") and \
           isinstance(module.__plugins__, list):
            lst = module.__plugins__
            ret = []

            for plug in lst:
                try:
                    inst = plug()
                    inst.start(pkg)

                    ret.append(inst)
                except Exception, err:
                    log.critical("Error while starting %s from %s:" % (plug, pkg))
                    log.critical(generate_traceback())
                    log.critical("Ignoring instance.")

            if not ret:
                raise PluginException(pkg, \
                              "No startablePlugin subclass in %s" % pkg)

            self.instances[pkg] = ret