def load(self, name, path=None, reload=False): if name in self.plugins and not reload: return self.plugins[name] mod_name = name if self.prefix: #mod_name determines how the module is named in sys.modules #Using a prefix helps prevent overlap with other modules #(no '.' -- it causes problems) mod_name = self.prefix + name if mod_name in sys.modules and not reload: raise koji.PluginError('module name conflict: %s' % mod_name) if path is None: path = self.searchpath if path is None: raise koji.PluginError("empty module search path") file, pathname, description = imp.find_module(name, self.pathlist(path)) try: plugin = imp.load_module(mod_name, file, pathname, description) except Exception: msg = 'Loading plugin %s failed' % name logging.getLogger('koji.plugin').error(msg) raise finally: file.close() self.plugins[name] = plugin return plugin
def load(self, name, path=None, reload=False): if name in self.plugins and not reload: return self.plugins[name] mod_name = name if self.prefix: # mod_name determines how the module is named in sys.modules # Using a prefix helps prevent overlap with other modules # (no '.' -- it causes problems) mod_name = self.prefix + name if mod_name in sys.modules and not reload: raise koji.PluginError('module name conflict: %s' % mod_name) if path is None: path = self.searchpath if path is None: raise koji.PluginError("empty module search path") file = None try: if importlib: orig_spec = importlib.machinery.PathFinder().find_spec(name, self.pathlist(path)) plugin_spec = importlib.util.spec_from_file_location(mod_name, orig_spec.origin) plugin = importlib.util.module_from_spec(plugin_spec) sys.modules[mod_name] = plugin plugin_spec.loader.exec_module(plugin) else: file, pathname, description = imp.find_module(name, self.pathlist(path)) plugin = imp.load_module(mod_name, file, pathname, description) except Exception: msg = 'Loading plugin %s failed' % name logging.getLogger('koji.plugin').error(msg) raise finally: if file: file.close() self.plugins[name] = plugin return plugin
def register_callback(cbtype, func): if not cbtype in callbacks: raise koji.PluginError('"%s" is not a valid callback type' % cbtype) if not callable(func): raise koji.PluginError('%s is not callable' % getattr(func, '__name__', 'function')) callbacks[cbtype].append(func)
def run_callbacks(cbtype, *args, **kws): if not cbtype in callbacks: raise koji.PluginError('"%s" is not a valid callback type' % cbtype) for func in callbacks[cbtype]: try: func(cbtype, *args, **kws) except: msg = 'Error running %s callback from %s' % (cbtype, func.__module__) if getattr(func, 'failure_is_an_option', False): logging.getLogger('koji.plugin').warn(msg, exc_info=True) else: tb = ''.join(traceback.format_exception(*sys.exc_info())) raise koji.CallbackError('%s:\n%s' % (msg, tb))