Add support for iterating through a list of command packages from the global config file and generate a list of commands to be included. Improve the the _is_command() function to succesfully inspect command classes which do not derive from the edkrepo base command class
Add support to list a command package as preferred. Signed-off-by: Ashley E Desimone <ashley.e.desim...@intel.com> Cc: Nate DeSimone <nathaniel.l.desim...@intel.com> Cc: Puja Pandya <puja.pan...@intel.com> --- edkrepo/commands/command_factory.py | 51 ++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/edkrepo/commands/command_factory.py b/edkrepo/commands/command_factory.py index abdfb1a..9f2e923 100644 --- a/edkrepo/commands/command_factory.py +++ b/edkrepo/commands/command_factory.py @@ -13,6 +13,7 @@ import os import sys from edkrepo.commands.edkrepo_command import EdkrepoCommand from edkrepo.commands.composite_command import CompositeCommand +from edkrepo.config.config_factory import GlobalConfig def _is_command(CommandClass): if CommandClass == EdkrepoCommand: @@ -20,12 +21,20 @@ def _is_command(CommandClass): if CommandClass in EdkrepoCommand.__subclasses__(): return True #Use reflection to see if the class would work as a command anyway - funcs = inspect.getmembers(CommandClass, predicate=inspect.ismethod) + try: + cmd = CommandClass() + except: + return False + funcs = inspect.getmembers(cmd, predicate=inspect.ismethod) has_get_metadata = False has_run_command = False for func in funcs: if func[0] == 'get_metadata' and len(inspect.getargspec(func[1])[0]) == 1: - has_get_metadata = True + try: + cmd.get_metadata() + has_get_metadata = True + except: + has_get_metadata = False if func[0] == 'run_command': arg_spec = inspect.getargspec(func[1]) if len(arg_spec[0]) == 3 and arg_spec[0][1] == "args" and arg_spec[0][2] == "config": @@ -36,16 +45,34 @@ def _is_command(CommandClass): return False def get_commands(): - commands = [] - for module in os.listdir(os.path.dirname(__file__)): - if module == "__init__.py" or os.path.splitext(module)[1] != ".py": - continue - mod = importlib.import_module("edkrepo.commands.{}".format(os.path.splitext(module)[0])) - classes = inspect.getmembers(mod, predicate=inspect.isclass) - for cls in classes: - if _is_command(cls[1]): - commands.append(cls[1]) - return commands + cfg_file = GlobalConfig() + cmd_pkg_list = cfg_file.command_packages_list + pref_cmd_pkg = cfg_file.pref_pkg + commands = {} + pref_commands = {} + final_cmd_list = [] + cmd_search_dirs = [] + + for cmd_pkg in cmd_pkg_list: + mod = importlib.import_module(cmd_pkg) + cmd_search_dirs.append((cmd_pkg, os.path.dirname(mod.__file__))) + for cmd_dir in cmd_search_dirs: + for module in os.listdir(cmd_dir[1]): + if module == '__init__.py' or os.path.splitext(module)[1] != '.py': + continue + mod = importlib.import_module('{}.{}'.format(cmd_dir[0], os.path.splitext(module)[0])) + classes = inspect.getmembers(mod, predicate=inspect.isclass) + for cls in classes: + if _is_command(cls[1]): + if cmd_dir[0] == pref_cmd_pkg: + pref_commands.update([(cls[0], cls[1])]) + else: + commands.update([(cls[0], cls[1])]) + for key in commands.keys(): + if key not in pref_commands.keys(): + final_cmd_list.append(commands[key]) + final_cmd_list.extend(pref_commands.values()) + return final_cmd_list def create_composite_command(): commands = get_commands() -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#49558): https://edk2.groups.io/g/devel/message/49558 Mute This Topic: https://groups.io/mt/39101780/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-