Exemplo n.º 1
0
  def _loadplugins(self, tfilter):
    """
    load plugins in all directories under the plugin directory
    """
    _module_list = imputils.find_files(self.basepath, tfilter)
    _module_list.sort()

    load = False

    for fullpath in _module_list:
      modpath = fullpath.replace(self.basepath, '')
      force = False
      if modpath in self.loadedplugins:
        force = True
      modname, dummy = self._loadplugin(modpath, self.basepath,
                                        force=force, runload=load)

      if modname == 'log':
        self.api('log.adddtype')(self.sname)
        self.api('log.console')(self.sname)
        self.api('log.adddtype')('upgrade')
        self.api('log.console')('upgrade')

    if not load:
      testsort = sorted([i['plugin'] for i in self.loadedpluginsd.values()],
                        key=operator.attrgetter('priority'))
      for i in testsort:
        try:
          #check dependencies here
          self.loadplugin(i)
        except Exception: # pylint: disable=broad-except
          self.api('send.traceback')(
              "load: had problems running the load method for %s." \
                          % i.fullimploc)
          imputils.deletemodule(i.fullimploc)
Exemplo n.º 2
0
  def _unloadplugin(self, fullimploc):
    """
    unload a module
    """
    if fullimploc in sys.modules:

      _module = sys.modules[fullimploc]
      success = True
      try:
        if "proxy_import" in _module.__dict__:
          self.api('send.client')(
              'unload: unloading %s' % fullimploc)
          if "unload" in _module.__dict__:
            try:
              _module.unload()
            except Exception: # pylint: disable=broad-except
              success = False
              self.api('send.traceback')(
                  "unload: module %s didn't unload properly." % fullimploc)

          if not self._removeplugin(_module.SNAME):
            self.api('send.client')(
                'could not remove plugin %s' % fullimploc)
            success = False

      except Exception: # pylint: disable=broad-except
        self.api('send.traceback')(
            "unload: had problems unloading %s." % fullimploc)
        success = False

      if success:
        imputils.deletemodule(fullimploc)
        self.api('send.client')("unload: unloaded %s." % fullimploc)

    return success
Exemplo n.º 3
0
    def _updateallplugininfo(self):
        """
    find plugins that are not in self.allplugininfo
    """
        _plugin_list = imputils.find_files(self.basepath, '*.py')
        _plugin_list.sort()

        self.allplugininfo = {}
        badplugins = []

        for fullpath in _plugin_list:
            modpath = fullpath.replace(self.basepath, '')

            imploc, modname = imputils.get_module_name(modpath)

            if not modname.startswith("_"):
                fullimploc = "plugins" + '.' + imploc
                if fullimploc in sys.modules:
                    plugin = self.api('plugins.getp')(modpath)
                    self.allplugininfo[modpath] = {}
                    self.allplugininfo[modpath]['sname'] = plugin.sname
                    self.allplugininfo[modpath]['name'] = plugin.name
                    self.allplugininfo[modpath]['purpose'] = plugin.purpose
                    self.allplugininfo[modpath]['author'] = plugin.author
                    self.allplugininfo[modpath]['version'] = plugin.version
                    self.allplugininfo[modpath]['modpath'] = modpath
                    self.allplugininfo[modpath]['fullimploc'] = fullimploc

                else:
                    try:
                        _module = __import__(fullimploc)
                        _module = sys.modules[fullimploc]

                        self.allplugininfo[modpath] = {}
                        self.allplugininfo[modpath]['sname'] = _module.SNAME
                        self.allplugininfo[modpath]['name'] = _module.NAME
                        self.allplugininfo[modpath][
                            'purpose'] = _module.PURPOSE
                        self.allplugininfo[modpath]['author'] = _module.AUTHOR
                        self.allplugininfo[modpath][
                            'version'] = _module.VERSION
                        self.allplugininfo[modpath]['modpath'] = modpath
                        self.allplugininfo[modpath]['fullimploc'] = fullimploc

                        imputils.deletemodule(fullimploc)

                    except Exception:  # pylint: disable=broad-except
                        badplugins.append(fullimploc)

        return badplugins
Exemplo n.º 4
0
  def _updateallplugininfo(self):
    """
    find plugins that are not in self.allplugininfo
    """
    _plugin_list = imputils.find_files(self.basepath, '*.py')
    _plugin_list.sort()

    self.allplugininfo = {}
    badplugins = []

    for fullpath in _plugin_list:
      modpath = fullpath.replace(self.basepath, '')

      imploc, modname = imputils.get_module_name(modpath)

      if not modname.startswith("_"):
        fullimploc = "plugins" + '.' + imploc
        if fullimploc in sys.modules:
          plugin = self.api('plugins.getp')(modpath)
          self.allplugininfo[modpath] = {}
          self.allplugininfo[modpath]['sname'] = plugin.sname
          self.allplugininfo[modpath]['name'] = plugin.name
          self.allplugininfo[modpath]['purpose'] = plugin.purpose
          self.allplugininfo[modpath]['author'] = plugin.author
          self.allplugininfo[modpath]['version'] = plugin.version
          self.allplugininfo[modpath]['modpath'] = modpath
          self.allplugininfo[modpath]['fullimploc'] = fullimploc

        else:
          try:
            _module = __import__(fullimploc)
            _module = sys.modules[fullimploc]

            self.allplugininfo[modpath] = {}
            self.allplugininfo[modpath]['sname'] = _module.SNAME
            self.allplugininfo[modpath]['name'] = _module.NAME
            self.allplugininfo[modpath]['purpose'] = _module.PURPOSE
            self.allplugininfo[modpath]['author'] = _module.AUTHOR
            self.allplugininfo[modpath]['version'] = _module.VERSION
            self.allplugininfo[modpath]['modpath'] = modpath
            self.allplugininfo[modpath]['fullimploc'] = fullimploc

            imputils.deletemodule(fullimploc)

          except Exception: # pylint: disable=broad-except
            badplugins.append(fullimploc)

    return badplugins
Exemplo n.º 5
0
    def _loadplugin(self, modpath, basepath, force=False, runload=True):
        """
    load a single plugin
    """
        success, msg, module, fullimploc = imputils.importmodule(
            modpath, basepath, self, 'plugins')

        if success and msg == 'import':

            load = True

            if 'AUTOLOAD' in module.__dict__ and not force:
                if not module.AUTOLOAD:
                    load = False
            elif 'AUTOLOAD' not in module.__dict__:
                load = False

            if modpath not in self.allplugininfo:
                self.allplugininfo[modpath] = {}
                self.allplugininfo[modpath]['sname'] = module.SNAME
                self.allplugininfo[modpath]['name'] = module.NAME
                self.allplugininfo[modpath]['purpose'] = module.PURPOSE
                self.allplugininfo[modpath]['author'] = module.AUTHOR
                self.allplugininfo[modpath]['version'] = module.VERSION
                self.allplugininfo[modpath]['modpath'] = modpath
                self.allplugininfo[modpath]['fullimploc'] = fullimploc

            if load:
                if "Plugin" in module.__dict__:
                    self._addplugin(module, modpath, basepath, fullimploc,
                                    runload)

                else:
                    self.api('send.msg')('Module %s has no Plugin class' % \
                                                        module.NAME)

                module.__dict__["proxy_import"] = 1

                return module.SNAME, 'Loaded'
            else:
                imputils.deletemodule(fullimploc)
                self.api('send.msg')(
                    'Not loading %s (%s) because autoload is False' % \
                                            (module.NAME, fullimploc), primary='plugins')
            return True, 'not autoloaded'

        return success, msg
Exemplo n.º 6
0
  def _loadplugin(self, modpath, basepath, force=False, runload=True):
    """
    load a single plugin
    """
    success, msg, module, fullimploc = imputils.importmodule(modpath, basepath,
                                                             self, 'plugins')

    if success and msg == 'import':

      load = True

      if 'AUTOLOAD' in module.__dict__ and not force:
        if not module.AUTOLOAD:
          load = False
      elif 'AUTOLOAD' not in module.__dict__:
        load = False

      if modpath not in self.allplugininfo:
        self.allplugininfo[modpath] = {}
        self.allplugininfo[modpath]['sname'] = module.SNAME
        self.allplugininfo[modpath]['name'] = module.NAME
        self.allplugininfo[modpath]['purpose'] = module.PURPOSE
        self.allplugininfo[modpath]['author'] = module.AUTHOR
        self.allplugininfo[modpath]['version'] = module.VERSION
        self.allplugininfo[modpath]['modpath'] = modpath
        self.allplugininfo[modpath]['fullimploc'] = fullimploc

      if load:
        if "Plugin" in module.__dict__:
          self._addplugin(module, modpath, basepath, fullimploc, runload)

        else:
          self.api('send.msg')('Module %s has no Plugin class' % \
                                              module.NAME)

        module.__dict__["proxy_import"] = 1

        return module.SNAME, 'Loaded'
      else:
        imputils.deletemodule(fullimploc)
        self.api('send.msg')(
            'Not loading %s (%s) because autoload is False' % \
                                    (module.NAME, fullimploc), primary='plugins')
      return True, 'not autoloaded'

    return success, msg
Exemplo n.º 7
0
  def _addplugin(self, module, modpath, basepath, fullimploc, load=True):
    # pylint: disable=too-many-arguments
    """
    add a plugin to be managed
    """
    pluginn = self.api('plugins.getp')(module.NAME)
    plugins = self.api('plugins.getp')(module.SNAME)
    if plugins or pluginn:
      self.api('send.msg')('Plugin %s already exists' % module.NAME,
                           secondary=module.SNAME)
      return False

    plugin = module.Plugin(module.NAME, module.SNAME,
                           modpath, basepath, fullimploc)
    plugin.author = module.AUTHOR
    plugin.purpose = module.PURPOSE
    plugin.version = module.VERSION
    try:
      plugin.priority = module.PRIORITY
    except AttributeError:
      pass

    if load:
      try:
        #check dependencies here
        self.loadplugin(plugin)
      except Exception: # pylint: disable=broad-except
        self.api('send.traceback')(
            "load: had problems running the load method for %s." \
                                                % fullimploc)
        imputils.deletemodule(fullimploc)
        return False

    self.loadedpluginsd[modpath] = {}
    self.loadedpluginsd[modpath]['plugin'] = plugin
    self.loadedpluginsd[modpath]['module'] = module

    self.pluginlookupbysname[plugin.sname] = modpath
    self.pluginlookupbyname[plugin.name] = modpath
    self.pluginlookupbyfullimploc[fullimploc] = modpath

    self.loadedplugins[modpath] = True
    self.loadedplugins.sync()

    return True
Exemplo n.º 8
0
    def _addplugin(self, module, modpath, basepath, fullimploc, load=True):
        # pylint: disable=too-many-arguments
        """
    add a plugin to be managed
    """
        pluginn = self.api('plugins.getp')(module.NAME)
        plugins = self.api('plugins.getp')(module.SNAME)
        if plugins or pluginn:
            self.api('send.msg')('Plugin %s already exists' % module.NAME,
                                 secondary=module.SNAME)
            return False

        plugin = module.Plugin(module.NAME, module.SNAME, modpath, basepath,
                               fullimploc)
        plugin.author = module.AUTHOR
        plugin.purpose = module.PURPOSE
        plugin.version = module.VERSION
        try:
            plugin.priority = module.PRIORITY
        except AttributeError:
            pass

        if load:
            try:
                #check dependencies here
                self.loadplugin(plugin)
            except Exception:  # pylint: disable=broad-except
                self.api('send.traceback')(
                    "load: had problems running the load method for %s." \
                                                        % fullimploc)
                imputils.deletemodule(fullimploc)
                return False

        self.loadedpluginsd[modpath] = {}
        self.loadedpluginsd[modpath]['plugin'] = plugin
        self.loadedpluginsd[modpath]['module'] = module

        self.pluginlookupbysname[plugin.sname] = modpath
        self.pluginlookupbyname[plugin.name] = modpath
        self.pluginlookupbyfullimploc[fullimploc] = modpath

        self.loadedplugins[modpath] = True
        self.loadedplugins.sync()

        return True
Exemplo n.º 9
0
    def _loadplugins(self, tfilter):
        """
    load plugins in all directories under the plugin directory
    """
        _module_list = imputils.find_files(self.basepath, tfilter)
        _module_list.sort()

        load = False

        for fullpath in _module_list:
            modpath = fullpath.replace(self.basepath, '')
            force = False
            if modpath in self.loadedplugins:
                force = True
            modname, dummy = self._loadplugin(modpath,
                                              self.basepath,
                                              force=force,
                                              runload=load)

            if modname == 'log':
                self.api('log.adddtype')(self.sname)
                self.api('log.console')(self.sname)
                self.api('log.adddtype')('upgrade')
                self.api('log.console')('upgrade')

        if not load:
            testsort = sorted(
                [i['plugin'] for i in self.loadedpluginsd.values()],
                key=operator.attrgetter('priority'))
            for i in testsort:
                try:
                    #check dependencies here
                    self.loadplugin(i)
                except Exception:  # pylint: disable=broad-except
                    self.api('send.traceback')(
                        "load: had problems running the load method for %s." \
                                    % i.fullimploc)
                    imputils.deletemodule(i.fullimploc)
Exemplo n.º 10
0
    def _unloadplugin(self, fullimploc):
        """
    unload a module
    """
        if fullimploc in sys.modules:

            _module = sys.modules[fullimploc]
            success = True
            try:
                if "proxy_import" in _module.__dict__:
                    self.api('send.client')('unload: unloading %s' %
                                            fullimploc)
                    if "unload" in _module.__dict__:
                        try:
                            _module.unload()
                        except Exception:  # pylint: disable=broad-except
                            success = False
                            self.api('send.traceback')(
                                "unload: module %s didn't unload properly." %
                                fullimploc)

                    if not self._removeplugin(_module.SNAME):
                        self.api('send.client')('could not remove plugin %s' %
                                                fullimploc)
                        success = False

            except Exception:  # pylint: disable=broad-except
                self.api('send.traceback')(
                    "unload: had problems unloading %s." % fullimploc)
                success = False

            if success:
                imputils.deletemodule(fullimploc)
                self.api('send.client')("unload: unloaded %s." % fullimploc)

        return success