From 433b37b1a88be27954438b8678fbd5c0c32e40bd Mon Sep 17 00:00:00 2001 From: Junjie Mao Date: Thu, 3 Mar 2022 19:38:50 +0800 Subject: [PATCH] config_tools: unify the CLI of scenario-manipulating scripts Today the scripts that populate default values and validate scenarios have different command-line interfaces: the former requires an XML schema as input (which is cumbersome in most cases), while the latter always infer where the XML schema is (which is inflexible). This patch unifies the command line options of those scripts as follows: - The scenario XML is always a required positional argument. - The output file path (if any) is an optional positional argument. - The schema XML file is an optional long option. When not specified, the scripts will always use the one under the misc/config_tools/schema directory. Also, this patch makes the validator.py executable, as is done to other executable scripts in the repo. Tracked-On: #6690 Signed-off-by: Junjie Mao --- hypervisor/scripts/makefile/config.mk | 3 +-- .../scenario_config/default_populator.py | 15 +++++++++------ misc/config_tools/scenario_config/validator.py | 0 3 files changed, 10 insertions(+), 8 deletions(-) mode change 100644 => 100755 misc/config_tools/scenario_config/validator.py diff --git a/hypervisor/scripts/makefile/config.mk b/hypervisor/scripts/makefile/config.mk index b45a23f2f..19a8b9828 100644 --- a/hypervisor/scripts/makefile/config.mk +++ b/hypervisor/scripts/makefile/config.mk @@ -111,7 +111,6 @@ HV_UNIFIED_XML_IN := $(BASEDIR)/scripts/makefile/unified.xml.in HV_PREDEFINED_DATA_DIR := $(realpath $(BASEDIR)/../misc/config_tools/data) HV_CONFIG_TOOL_DIR := $(realpath $(BASEDIR)/../misc/config_tools) HV_CONFIG_XFORM_DIR := $(HV_CONFIG_TOOL_DIR)/xforms -HV_SCENARIO_XSD := $(HV_CONFIG_TOOL_DIR)/schema/config.xsd # Paths to the outputs: HV_CONFIG_DIR := $(HV_OBJDIR)/configs @@ -200,7 +199,7 @@ $(HV_SCENARIO_XML): if [ -f $(SCENARIO_FILE) ]; then \ echo "Scenario XML is being fetched from $(realpath $(SCENARIO_FILE))"; \ mkdir -p $(dir $(HV_SCENARIO_XML)); \ - python3 $(HV_CONFIG_TOOL_DIR)/scenario_config/default_populator.py $(HV_SCENARIO_XSD) $(SCENARIO_FILE) $(HV_SCENARIO_XML); \ + python3 $(HV_CONFIG_TOOL_DIR)/scenario_config/default_populator.py $(SCENARIO_FILE) $(HV_SCENARIO_XML); \ else \ echo "No pre-defined scenario available at $(SCENARIO_FILE)"; \ echo "Try setting another predefined BOARD or SCENARIO or specifying a scenario XML file"; \ diff --git a/misc/config_tools/scenario_config/default_populator.py b/misc/config_tools/scenario_config/default_populator.py index d3d74fc54..fd4fef0a6 100755 --- a/misc/config_tools/scenario_config/default_populator.py +++ b/misc/config_tools/scenario_config/default_populator.py @@ -43,7 +43,7 @@ class DefaultValuePopulatingStage(PipelineStage): populator.transform(etree) obj.set("scenario_etree", etree) -def main(xsd_file, xml_file, out_file): +def main(args): from xml_loader import XMLLoadStage from lxml_loader import LXMLLoadStage @@ -54,15 +54,18 @@ def main(xsd_file, xml_file, out_file): DefaultValuePopulatingStage(), ]) - obj = PipelineObject(schema_path = xsd_file, scenario_path = xml_file) + obj = PipelineObject(schema_path = args.schema, scenario_path = args.scenario) pipeline.run(obj) - obj.get("scenario_etree").write(out_file) + obj.get("scenario_etree").write(args.out) if __name__ == "__main__": + config_tools_dir = os.path.join(os.path.dirname(__file__), "..") + schema_dir = os.path.join(config_tools_dir, "schema") + parser = argparse.ArgumentParser(description="Populate a given scenario XML with default values of nonexistent nodes") - parser.add_argument("xsd", help="Path to the schema of scenario XMLs") - parser.add_argument("xml", help="Path to the scenario XML file from users") + parser.add_argument("scenario", help="Path to the scenario XML file from users") parser.add_argument("out", nargs="?", default="out.xml", help="Path where the output is placed") + parser.add_argument("--schema", default=os.path.join(schema_dir, "config.xsd"), help="the XML schema that defines the syntax of scenario XMLs") args = parser.parse_args() - main(args.xsd, args.xml, args.out) + main(args) diff --git a/misc/config_tools/scenario_config/validator.py b/misc/config_tools/scenario_config/validator.py old mode 100644 new mode 100755