Loads user specified plugins, if found, and initializes them. Looks in /tests/plugins and searches each module for plugin_name. plugin_name is the name of the class that the user has used to implement a plugin. If the class is found, it is initialized and added to self.__plugins. If it's not
| 26 | LOG.setLevel(level=logging.INFO) |
| 27 | |
| 28 | class PluginRunner(object): |
| 29 | ''' Loads user specified plugins, if found, and initializes them. |
| 30 | |
| 31 | Looks in /tests/plugins and searches each module for plugin_name. plugin_name |
| 32 | is the name of the class that the user has used to implement a plugin. If the class |
| 33 | is found, it is initialized and added to self.__plugins. If it's not found, an error |
| 34 | message is logged and the plugin in not loaded. |
| 35 | ''' |
| 36 | |
| 37 | def __init__(self, plugin_infos): |
| 38 | self.__available_modules = self.__get_plugin_modules() |
| 39 | self.__get_plugins_from_modules(plugin_infos) |
| 40 | |
| 41 | @property |
| 42 | def plugins(self): |
| 43 | return self.__plugins |
| 44 | |
| 45 | def __getstate__(self): |
| 46 | state = self.__dict__.copy() |
| 47 | del state['__available_modules'] |
| 48 | return state |
| 49 | |
| 50 | def __get_plugin_modules(self): |
| 51 | ''' Gets all the modules in the directory and imports them''' |
| 52 | modules = pkgutil.iter_modules(path=[PLUGIN_DIR]) |
| 53 | available_modules = [] |
| 54 | for loader, mod_name, ispkg in modules: |
| 55 | yield __import__("tests.benchmark.plugins.%s" % mod_name, fromlist=[mod_name]) |
| 56 | |
| 57 | def __get_plugins_from_modules(self, plugin_infos): |
| 58 | '''Look for user specified plugins in the available modules.''' |
| 59 | self.__plugins = [] |
| 60 | plugin_names = [] |
| 61 | for module in self.__available_modules: |
| 62 | for plugin_info in plugin_infos: |
| 63 | plugin_name, scope = self.__get_plugin_info(plugin_info) |
| 64 | plugin_names.append(plugin_name) |
| 65 | if hasattr(module, plugin_name): |
| 66 | self.__plugins.append(getattr(module, plugin_name)(scope=scope.lower())) |
| 67 | # The plugin(s) that could not be loaded are captured in the set difference |
| 68 | # between plugin_names and self.__plugins |
| 69 | plugins_found = [p.__name__ for p in self.__plugins] |
| 70 | LOG.debug("Plugins found: %s" % ', '.join(plugins_found)) |
| 71 | plugins_not_found = set(plugin_names).difference(plugins_found) |
| 72 | # If the user's entered a plugin that does not exist, raise an error. |
| 73 | if len(plugins_not_found): |
| 74 | msg = "Plugin(s) not found: %s" % (','.join(list(plugins_not_found))) |
| 75 | raise RuntimeError(msg) |
| 76 | |
| 77 | def __get_plugin_info(self, plugin_info): |
| 78 | info = plugin_info.split(':') |
| 79 | if len(info) == 1: |
| 80 | return info[0], 'query' |
| 81 | elif len(info) == 2: |
| 82 | return info[0], info[1] |
| 83 | else: |
| 84 | raise ValueError("Plugin names specified in the form <plugin_name>[:<scope>]") |
| 85 |