diff options
author | 2023-03-08 05:44:17 +0900 | |
---|---|---|
committer | 2023-03-07 21:18:17 +0000 | |
commit | 3397b6a2aadf8c2eb808fa38f7d6c07d5c82f320 (patch) | |
tree | 57d6c7457de8f8aa636b936be583a8b3baf7aab4 /scripts/conv_linker_config.py | |
parent | edec71adae47232e967d3ba8f1a6a1c78afcee90 (diff) |
conv_linker_config handling existing output file
`proto` sub command now fails when the output file exists.
This is to avoid accidental overwrite.
To handle the case properly, it support --force and --append.
This is to support amending /{partition}/etc/linker.config.pb in the
build process.
Bug: 244531518
Test: manual testing
Change-Id: I0af8c83015e485f2c7533221cae8caf6143403c8
Diffstat (limited to 'scripts/conv_linker_config.py')
-rw-r--r-- | scripts/conv_linker_config.py | 37 |
1 files changed, 35 insertions, 2 deletions
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__': |