From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com []) by mx.groups.io with SMTP id smtpd.web12.5095.1572304978964245437 for ; Mon, 28 Oct 2019 16:22:59 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: ashley.e.desimone@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Oct 2019 16:22:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,241,1569308400"; d="scan'208";a="283071865" Received: from aedesimo-desk.amr.corp.intel.com ([10.7.159.171]) by orsmga001.jf.intel.com with ESMTP; 28 Oct 2019 16:22:58 -0700 From: "Desimone, Ashley E" To: devel@edk2.groups.io Cc: Nate DeSimone , Puja Pandya Subject: [edk2-staging/EdkRepo] [PATCH 2/3] EdkRepo: Multiple Command Package Command Factory Update Date: Mon, 28 Oct 2019 16:22:44 -0700 Message-Id: <20191028232245.6712-2-ashley.e.desimone@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 In-Reply-To: <20191028232245.6712-1-ashley.e.desimone@intel.com> References: <20191028232245.6712-1-ashley.e.desimone@intel.com> 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 Cc: Nate DeSimone Cc: Puja Pandya --- 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