diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/build-command-op-templates | 63 | ||||
-rwxr-xr-x | scripts/build-command-templates | 63 |
2 files changed, 75 insertions, 51 deletions
diff --git a/scripts/build-command-op-templates b/scripts/build-command-op-templates index 6b6688fba..865590c2c 100755 --- a/scripts/build-command-op-templates +++ b/scripts/build-command-op-templates @@ -93,7 +93,7 @@ def get_properties(p): try: props["help"] = p.find("help").text except: - pass + props["help"] = "No help available" # Get the completion help strings @@ -113,7 +113,7 @@ def get_properties(p): for i in paths: comp_exprs.append("/bin/cli-shell-api listNodes {0}".format(i.text)) for i in scripts: - comp_exprs.append("sh -c \"{0}\"".format(i.text)) + comp_exprs.append("{0}".format(i.text)) comp_help = " && ".join(comp_exprs) props["comp_help"] = comp_help except: @@ -128,14 +128,6 @@ def make_node_def(props, command): node_def = "" - if "tag" in props: - node_def += "tag:\n" - - - if "type" in props: - node_def += "type: {0}\n".format(props["type"]) - - if "help" in props: node_def += "help: {0}\n".format(props["help"]) @@ -145,7 +137,7 @@ def make_node_def(props, command): if command is not None: - node_def += "run: sudo sh -c {0}\n".format(command.text) + node_def += "run: sudo sh -c \"{0}\"\n".format(command.text) if debug: @@ -173,32 +165,45 @@ def process_node(n, tmpl_dir): props = get_properties(props_elem) - # Type should not be set for non-tag, non-leaf nodes - if node_type != "node": - props["type"] = "txt" + if node_type == "node": + if debug: + print("Processing node {}".format(name)) + + with open(os.path.join(make_path(my_tmpl_dir), "node.def"), "w") as f: + f.write(make_node_def(props, command)) + + if children is not None: + inner_nodes = children.iterfind("*") + for inner_n in inner_nodes: + process_node(inner_n, my_tmpl_dir) if node_type == "tagNode": - props["tag"] = "True" - + if debug: + print("Processing tag node {}".format(name)) - with open(os.path.join(make_path(my_tmpl_dir), "node.def"), "w") as f: - f.write(make_node_def(props, command)) + os.makedirs(make_path(my_tmpl_dir), exist_ok=True) + with open(os.path.join(make_path(my_tmpl_dir), "node.def"), "w") as f: + f.write('help: {0}\0'.format(props['help'])) - if node_type == "node": - inner_nodes = children.iterfind("*") - for inner_n in inner_nodes: - process_node(inner_n, my_tmpl_dir) - if node_type == "tagNode": my_tmpl_dir.append("node.tag") - if debug: - print("Created path for the tagNode:", end="") os.makedirs(make_path(my_tmpl_dir), exist_ok=True) - inner_nodes = children.iterfind("*") - for inner_n in inner_nodes: - process_node(inner_n, my_tmpl_dir) + if debug: + print("Created path for the tagNode: {}".format(make_path(my_tmpl_dir)), end="") + + with open(os.path.join(make_path(my_tmpl_dir), "node.def"), "w") as f: + f.write(make_node_def(props, command)) + + if children is not None: + inner_nodes = children.iterfind("*") + for inner_n in inner_nodes: + process_node(inner_n, my_tmpl_dir) else: # This is a leaf node - pass + if debug: + print("Processing leaf node {}".format(name)) + + with open(os.path.join(make_path(my_tmpl_dir), "node.def"), "w") as f: + f.write(make_node_def(props, command)) root = xml.getroot() diff --git a/scripts/build-command-templates b/scripts/build-command-templates index d1871c1c8..9f51c00cd 100755 --- a/scripts/build-command-templates +++ b/scripts/build-command-templates @@ -49,6 +49,8 @@ schema_file = args.SCHEMA_FILE output_dir = args.OUTPUT_DIR debug = args.debug +debug = True + ## Load and validate the inputs try: @@ -114,27 +116,44 @@ def get_properties(p): except: pass - vce = p.findall("constraint") + vce = p.find("constraint") vc = [] - for v in vce: - if v.find("regex") is not None: - vc.append("pattern $VAR(@) \"{0}\"; {1}".format(v.find("regex").text, error_msg)) - else: - validator = v.find("validator") - v_name = validator.get("name") + + # The old backend doesn't support multiple validators in OR mode + # so we emulate it + + regex_elements = vce.findall("regex") + regexes = [] + if regex_elements is not None: + regexes = list(map(lambda e: e.text, regex_elements)) + + validator_elements = vce.findall("validator") + validators = [] + if validator_elements is not None: + for v in validator_elements: + v_name = os.path.join(validator_dir, v.get("name")) # XXX: lxml returns None for empty arguments v_argument = None try: - v_argument = validator.get("argument") + v_argument = v.get("argument") except: pass if v_argument is None: v_argument = "" - vc.append("exec {0}/{1} {2} $VAR(@); {3}".format(validator_dir, v_name, v_argument, error_msg)) - props["constraints"] = vc - except: - props["constraints"] = [] + + validators.append("{0} {1}".format(v_name, v_argument)) + + + regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes)) + validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators)) + validator_script = '${vyos_libexecdir}/validate-value.py' + validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, regex_args, validator_args, error_msg) + + props["constraint"] = validator_string + except Exception as exn: + print(exn) + pass # Get the completion help strings try: @@ -165,16 +184,14 @@ def get_properties(p): except: pass - # Get type - try: - props["type"] = p.find("type").text - except: - pass - # Get "multi" if p.find("multi") is not None: props["multi"] = True + # Get "valueless" + if p.find("valueless") is not None: + props["valueless"] = True + return props def make_node_def(props): @@ -190,6 +207,7 @@ def make_node_def(props): node_def += "multi:\n" if "type" in props: + # Will always be txt in practice if it's set node_def += "type: {0}\n".format(props["type"]) if "priority" in props: @@ -205,9 +223,8 @@ def make_node_def(props): if "comp_help" in props: node_def += "allowed: {0}\n".format(props["comp_help"]) - if "constraints" in props: - for c in props["constraints"]: - node_def += "syntax:expression: {0}\n".format(c) + if "constraint" in props: + node_def += "syntax:expression: {0}\n".format(props["constraint"]) if "owner" in props: node_def += "end: sudo sh -c \"{0}\"\n".format(props["owner"]) @@ -238,8 +255,10 @@ def process_node(n, tmpl_dir): if owner: props["owner"] = owner # Type should not be set for non-tag, non-leaf nodes + # For non-valueless leaf nodes, set the type to txt: to make them have some type, + # actual value validation is handled by constraints translated to syntax:expression: if node_type != "node": - if "type" not in props.keys(): + if "valueless" not in props.keys(): props["type"] = "txt" if node_type == "tagNode": props["tag"] = "True" |