summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2025-07-30 13:55:42 +0100
committerDaniil Baturin <daniil@baturin.org>2025-07-30 13:55:42 +0100
commit7b2bc979e07f0d554056be38990dd7a2b564d420 (patch)
tree4864ee60833924a7ed6a9565e7fd10be74949ff1 /python
parentb6c9340417aaa691ef15d0346f903b6612f3fa64 (diff)
downloadvyos-1x-7b2bc979e07f0d554056be38990dd7a2b564d420.tar.gz
vyos-1x-7b2bc979e07f0d554056be38990dd7a2b564d420.zip
op-mode: T7669: Make the path ambiguity check and JSON export capability mandatory
in the op mode cache generator
Diffstat (limited to 'python')
-rwxr-xr-xpython/vyos/xml_ref/generate_op_cache.py47
1 files changed, 24 insertions, 23 deletions
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]
@@ -263,16 +262,17 @@ def main():
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__':