diff options
author | 2020-10-06 17:16:44 +0900 | |
---|---|---|
committer | 2020-10-15 23:09:35 +0900 | |
commit | 62abd12ba573d123cda34515a75f6e228f6cadc4 (patch) | |
tree | 7390c5c080d2d429120e50c43bcceb217058494f /scripts/conv_linker_config.py | |
parent | 6ddb81b0dbc50a80ea39016e804a64073d82710e (diff) |
Handle new filetype 'linker_config'
Handle new filetype 'linker_config' which is configuration for
linkerconfig in json type and convert into protobuf in build time.
Bug: 169634881
Test: Build succeeded and cuttlefish boot succeeded
Change-Id: I56555fc738e6d6600d15a191a24f79a2ee747f52
Diffstat (limited to 'scripts/conv_linker_config.py')
-rw-r--r-- | scripts/conv_linker_config.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/scripts/conv_linker_config.py b/scripts/conv_linker_config.py new file mode 100644 index 000000000..86f788d81 --- /dev/null +++ b/scripts/conv_linker_config.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# +# Copyright (C) 2020 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" A tool to convert json file into pb with linker config format.""" + +import argparse +import collections +import json + +import linker_config_pb2 +from google.protobuf.json_format import ParseDict +from google.protobuf.text_format import MessageToString + + +def Proto(args): + with open(args.source) as f: + obj = json.load(f, object_pairs_hook=collections.OrderedDict) + pb = ParseDict(obj, linker_config_pb2.LinkerConfig()) + with open(args.output, 'wb') as f: + f.write(pb.SerializeToString()) + + +def Print(args): + with open(args.source, 'rb') as f: + pb = linker_config_pb2.LinkerConfig() + pb.ParseFromString(f.read()) + print(MessageToString(pb)) + + +def GetArgParser(): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + + parser_proto = subparsers.add_parser( + 'proto', help='Convert the input JSON configuration file into protobuf.') + parser_proto.add_argument( + '-s', + '--source', + required=True, + type=str, + help='Source linker configuration file in JSON.') + parser_proto.add_argument( + '-o', + '--output', + required=True, + type=str, + help='Target path to create protobuf file.') + parser_proto.set_defaults(func=Proto) + + print_proto = subparsers.add_parser( + 'print', help='Print configuration in human-readable text format.') + print_proto.add_argument( + '-s', + '--source', + required=True, + type=str, + help='Source linker configuration file in protobuf.') + print_proto.set_defaults(func=Print) + + return parser + + +def main(): + args = GetArgParser().parse_args() + args.func(args) + + +if __name__ == '__main__': + main() |