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
diff --git a/scripts/conv_linker_config.py b/scripts/conv_linker_config.py
index 2ce0ee2..784a92f 100644
--- a/scripts/conv_linker_config.py
+++ b/scripts/conv_linker_config.py
@@ -19,6 +19,7 @@
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 @@
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 @@
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 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__':