blob: 86f788d81787f2d96b9e71f94bf4739a5214c3f4 [file] [log] [blame]
Kiyoung Kim62abd122020-10-06 17:16:44 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16""" A tool to convert json file into pb with linker config format."""
17
18import argparse
19import collections
20import json
21
22import linker_config_pb2
23from google.protobuf.json_format import ParseDict
24from google.protobuf.text_format import MessageToString
25
26
27def Proto(args):
28 with open(args.source) as f:
29 obj = json.load(f, object_pairs_hook=collections.OrderedDict)
30 pb = ParseDict(obj, linker_config_pb2.LinkerConfig())
31 with open(args.output, 'wb') as f:
32 f.write(pb.SerializeToString())
33
34
35def Print(args):
36 with open(args.source, 'rb') as f:
37 pb = linker_config_pb2.LinkerConfig()
38 pb.ParseFromString(f.read())
39 print(MessageToString(pb))
40
41
42def GetArgParser():
43 parser = argparse.ArgumentParser()
44 subparsers = parser.add_subparsers()
45
46 parser_proto = subparsers.add_parser(
47 'proto', help='Convert the input JSON configuration file into protobuf.')
48 parser_proto.add_argument(
49 '-s',
50 '--source',
51 required=True,
52 type=str,
53 help='Source linker configuration file in JSON.')
54 parser_proto.add_argument(
55 '-o',
56 '--output',
57 required=True,
58 type=str,
59 help='Target path to create protobuf file.')
60 parser_proto.set_defaults(func=Proto)
61
62 print_proto = subparsers.add_parser(
63 'print', help='Print configuration in human-readable text format.')
64 print_proto.add_argument(
65 '-s',
66 '--source',
67 required=True,
68 type=str,
69 help='Source linker configuration file in protobuf.')
70 print_proto.set_defaults(func=Print)
71
72 return parser
73
74
75def main():
76 args = GetArgParser().parse_args()
77 args.func(args)
78
79
80if __name__ == '__main__':
81 main()