blob: 81425fb3516c19c97b0107a09a8f8dfc0e011122 [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):
Kiyoung Kime52c6652020-10-23 11:00:25 +090028 json_content = ''
Kiyoung Kim62abd122020-10-06 17:16:44 +090029 with open(args.source) as f:
Kiyoung Kime52c6652020-10-23 11:00:25 +090030 for line in f:
31 if not line.lstrip().startswith('//'):
32 json_content += line
33 obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
Kiyoung Kim62abd122020-10-06 17:16:44 +090034 pb = ParseDict(obj, linker_config_pb2.LinkerConfig())
35 with open(args.output, 'wb') as f:
36 f.write(pb.SerializeToString())
37
38
39def Print(args):
40 with open(args.source, 'rb') as f:
41 pb = linker_config_pb2.LinkerConfig()
42 pb.ParseFromString(f.read())
43 print(MessageToString(pb))
44
45
46def GetArgParser():
47 parser = argparse.ArgumentParser()
48 subparsers = parser.add_subparsers()
49
50 parser_proto = subparsers.add_parser(
51 'proto', help='Convert the input JSON configuration file into protobuf.')
52 parser_proto.add_argument(
53 '-s',
54 '--source',
55 required=True,
56 type=str,
57 help='Source linker configuration file in JSON.')
58 parser_proto.add_argument(
59 '-o',
60 '--output',
61 required=True,
62 type=str,
63 help='Target path to create protobuf file.')
64 parser_proto.set_defaults(func=Proto)
65
66 print_proto = subparsers.add_parser(
67 'print', help='Print configuration in human-readable text format.')
68 print_proto.add_argument(
69 '-s',
70 '--source',
71 required=True,
72 type=str,
73 help='Source linker configuration file in protobuf.')
74 print_proto.set_defaults(func=Print)
75
76 return parser
77
78
79def main():
80 args = GetArgParser().parse_args()
81 args.func(args)
82
83
84if __name__ == '__main__':
85 main()