diff --git a/scripts/conv_linker_config.py b/scripts/conv_linker_config.py index 2ce0ee2be..784a92f4c 100644 --- a/scripts/conv_linker_config.py +++ b/scripts/conv_linker_config.py @@ -19,6 +19,7 @@ import argparse import collections import json import os +import sys import linker_config_pb2 #pylint: disable=import-error from google.protobuf.descriptor import FieldDescriptor @@ -27,7 +28,24 @@ from google.protobuf.text_format import MessageToString def Proto(args): + """ + Merges input json files (--source) into a protobuf message (--output). + Fails if the output file exists. Set --force or --append to deal with the existing + output file. + --force to overwrite the output file with the input (.json files). + --append to append the input to the output file. + """ pb = linker_config_pb2.LinkerConfig() + if os.path.isfile(args.output): + if args.force: + pass + elif args.append: + with open(args.output, 'rb') as f: + pb.ParseFromString(f.read()) + else: + sys.stderr.write(f'Error: {args.output} exists. Use --force or --append.\n') + sys.exit(1) + if args.source: for input in args.source.split(':'): json_content = '' @@ -114,6 +132,17 @@ def GetArgParser(): required=True, type=str, help='Target path to create protobuf file.') + option_for_existing_output = parser_proto.add_mutually_exclusive_group() + option_for_existing_output.add_argument( + '-f', + '--force', + action='store_true', + help='Overwrite if the output file exists.') + option_for_existing_output.add_argument( + '-a', + '--append', + action='store_true', + help='Append the input to the output file if the output file exists.') parser_proto.set_defaults(func=Proto) print_proto = subparsers.add_parser( @@ -195,8 +224,12 @@ def GetArgParser(): def main(): - args = GetArgParser().parse_args() - args.func(args) + parser = GetArgParser() + args = parser.parse_args() + if 'func' in args: + args.func(args) + else: + parser.print_help() if __name__ == '__main__':