diff --git a/doc/admin-guide/tools/converting-records-to-yaml.en.rst b/doc/admin-guide/tools/converting-records-to-yaml.en.rst index 14e5fb89e25..3e2792169fa 100644 --- a/doc/admin-guide/tools/converting-records-to-yaml.en.rst +++ b/doc/admin-guide/tools/converting-records-to-yaml.en.rst @@ -145,7 +145,7 @@ Converting a file with a detailed output. .. code-block:: bash :linenos: - $ python3 convert2yaml.py -f records.config -S records.yaml -y + $ python3 convert2yaml.py -f records.config -o records.yaml [████████████████████████████████████████] 494/494 ┌■ 8 Renamed records: @@ -171,13 +171,12 @@ Converting a file with no output. If any, errors are displayed. .. code-block:: bash - $ convert2yaml.py -f records.config -S records.yaml -y -m + $ convert2yaml.py -f records.config -o records.yaml -m .. note:: Use -m, --mute to mute the output. - Non core records ================ @@ -221,7 +220,7 @@ non core records, for instance: .. code-block:: bash - $ convert2yaml.py -f records.config -S records.yaml -y -t float,int + $ convert2yaml.py -f records.config -o records.yaml -t float,int $ cat records.yaml ts: diff --git a/tests/gold_tests/records/records_config_to_yaml.test.py b/tests/gold_tests/records/records_config_to_yaml.test.py index 264e0ea5049..0f7f55e27d2 100644 --- a/tests/gold_tests/records/records_config_to_yaml.test.py +++ b/tests/gold_tests/records/records_config_to_yaml.test.py @@ -28,7 +28,7 @@ tr.Setup.Copy(os.path.join(Test.Variables.BuildRoot, "tools/records/convert2yaml.py")) tr.Setup.Copy('legacy_config/full_records.config') -tr.Processes.Default.Command = f'python3 convert2yaml.py -f full_records.config --save2 generated{file_suffix}.yaml --yaml --mute' +tr.Processes.Default.Command = f'python3 convert2yaml.py -f full_records.config --output generated{file_suffix}.yaml --yaml --mute' f = tr.Disk.File(f"generated{file_suffix}.yaml") f.Content = "gold/full_records.yaml" @@ -37,7 +37,7 @@ tr = Test.AddTestRun("Test records to yaml convert script -only renamed records.") tr.Setup.Copy(os.path.join(Test.Variables.BuildRoot, "tools/records/convert2yaml.py")) tr.Setup.Copy('legacy_config/old_records.config') -tr.Processes.Default.Command = f'python3 convert2yaml.py -f old_records.config --save2 generated{file_suffix}.yaml --yaml' +tr.Processes.Default.Command = f'python3 convert2yaml.py -f old_records.config --output generated{file_suffix}.yaml --yaml' tr.Processes.Default.Stream = 'gold/renamed_records.out' f = tr.Disk.File(f"generated{file_suffix}.yaml") f.Content = "gold/renamed_records.yaml" @@ -46,7 +46,7 @@ tr = Test.AddTestRun("Test errors when trying to override values ") tr.Setup.Copy(os.path.join(Test.Variables.BuildRoot, "tools/records/convert2yaml.py")) tr.Setup.Copy('legacy_config/override_value.config') -tr.Processes.Default.Command = f'python3 convert2yaml.py -f override_value.config --save2 generated{file_suffix}.yaml --yaml -m' +tr.Processes.Default.Command = f'python3 convert2yaml.py -f override_value.config --output generated{file_suffix}.yaml --yaml -m' tr.Processes.Default.Streams.stdout += Testers.ContainsExpression( "We cannot continue with 'proxy.config.ssl.client.verify.server.policy' at line '3' as a value node will be overridden", "Error should be present") @@ -54,7 +54,7 @@ tr = Test.AddTestRun("Test errors when trying to override maps") tr.Setup.Copy(os.path.join(Test.Variables.BuildRoot, "tools/records/convert2yaml.py")) tr.Setup.Copy('legacy_config/override_map.config') -tr.Processes.Default.Command = f'python3 convert2yaml.py -f override_map.config --save2 generated{file_suffix}.yaml --yaml -m' +tr.Processes.Default.Command = f'python3 convert2yaml.py -f override_map.config --output generated{file_suffix}.yaml --yaml -m' tr.Processes.Default.Streams.stdout += Testers.ContainsExpression( "We cannot continue with 'proxy.config.ssl.client.verify.server' at line '3' as an existing YAML map will be overridden.", "Error should be present") diff --git a/tools/records/convert2yaml.py b/tools/records/convert2yaml.py old mode 100644 new mode 100755 index 8407e27a118..da596033de4 --- a/tools/records/convert2yaml.py +++ b/tools/records/convert2yaml.py @@ -24,7 +24,8 @@ import yaml import tempfile import fileinput - +import time +import os.path import io from io import StringIO @@ -135,6 +136,10 @@ def bool_representer(dumper, value): def null_representer(dumper, value): return dumper.represent_scalar(u'tag:yaml.org,2002:null', str(value), style="'") + # Make sure we do not wipe out an existing file. + if os.path.exists(filename): + os.popen(f'cp {filename} {filename}.{int(time.time())}') + with open(filename, 'w') as f: if is_json: json.dump(data, f, indent=4, sort_keys=True) @@ -235,9 +240,6 @@ def fix_record_names(file): def handle_file_input(args): - if args.file is None: - raise Exception("Hey!! there is no file!") - config = {} # Fix the names first. @@ -276,7 +278,7 @@ def handle_file_input(args): if err: raise Exception("Schema failed.") - save_to_file(args.save2, args.json, args.typerepr, ts) + save_to_file(args.output, args.json, args.typerepr, ts) f.close() return @@ -284,11 +286,11 @@ def handle_file_input(args): if __name__ == '__main__': parser = argparse.ArgumentParser(description='records.config to YAML/JSON convert tool') - parser.add_argument('-f', '--file', help='records.config input file.') + parser.add_argument('-f', '--file', help='records.config input file.', required=False, default="records.config") parser.add_argument('-n', '--node', help="Include 'proxy.node' variables in the parser.", action='store_true') parser.add_argument('-t', '--typerepr', help="Use type representer (list)", required=False, default=['']) parser.add_argument('-s', '--schema', help="Validate the output using a json schema file.") - parser.add_argument('-S', '--save2', help="Save to file.", required=True) + parser.add_argument('-o', '--output', help="Save to output file.", required=False, default="records.yaml") parser.add_argument( '-m', '--mute', @@ -296,9 +298,9 @@ def handle_file_input(args): required=False, action='store_true') parser.add_argument('-e', '--error', help="Show traceback", required=False, action='store_true', default=False) - kk = parser.add_mutually_exclusive_group(required=True) - kk.add_argument('-j', '--json', help="Output as json", action='store_true') - kk.add_argument('-y', '--yaml', help="Output as yaml", action='store_true') + kk = parser.add_mutually_exclusive_group(required=False) + kk.add_argument('-j', '--json', help="Output as json", action='store_true', default=False) + kk.add_argument('-y', '--yaml', help="Output as yaml", action='store_true', default=True) parser.set_defaults(func=handle_file_input) args = parser.parse_args()