From 7b2bc979e07f0d554056be38990dd7a2b564d420 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 13:55:42 +0100 Subject: op-mode: T7669: Make the path ambiguity check and JSON export capability mandatory in the op mode cache generator --- python/vyos/xml_ref/generate_op_cache.py | 47 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index fec030b3d..ad1e785d0 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -49,7 +49,6 @@ from defaults import directories # noqa: E402 op_ref_cache = abspath(join(_here, 'op_cache.py')) -op_ref_json = abspath(join(_here, 'op_cache.json')) OptElement: TypeAlias = Optional[Element] @@ -262,17 +261,18 @@ def main(): action='store_true', help='check consistency of node data across files', ) - parser.add_argument( - '--check-path-ambiguity', - action='store_true', - help='attempt to reduce to unique paths, reporting if error', - ) parser.add_argument( '--select', type=str, help='limit cache to a subset of XML files: "power_ctl | multicast-group | ..."', ) + parser.add_argument( + '--export-json', + type=str, + help='Export a JSON version of the cache to a file', + ) + args = vars(parser.parse_args()) if args['check_xml_consistency']: @@ -281,7 +281,7 @@ def main(): xml_dir = abspath(args['xml_dir']) - d = {} + op_mode_data = {} select = args['select'] if select: @@ -290,25 +290,26 @@ def main(): for fname in sorted(glob.glob(f'{xml_dir}/*.xml')): file = os.path.basename(fname) if not select or os.path.splitext(file)[0] in select: - parse_file(fname, d) - - d = sort_op_data(d) - - if args['check_path_ambiguity']: - # when the following passes without error, return value will be the - # full dictionary indexed by str, not tuple - res, out, err = collapse(d) - if not err: - with open(op_ref_json, 'w') as f: - json.dump(res, f, indent=2) - else: - print('Found the following duplicate paths:\n') - print(out) - sys.exit(1) + parse_file(fname, op_mode_data) + + op_mode_data = sort_op_data(op_mode_data) + + res, out, err = collapse(op_mode_data) + if err: + print('Failed to generate operational command definition cache due to duplicate paths.') + print('Found the following duplicate paths:\n') + print(out) + sys.exit(1) + else: + op_mode_data = res with open(op_ref_cache, 'w') as f: f.write('from vyos.xml_ref.op_definition import NodeData\n') - f.write(f'op_reference = {str(d)}') + f.write(f'op_reference = {str(op_mode_data)}') + + if args['export_json']: + with open(args['export_json'], 'w') as f: + json.dump(op_mode_data, f) if __name__ == '__main__': -- cgit v1.2.3