blob: 5057ffea7d2e86fda9a55b060a162bd9ccedfa2c [file] [log] [blame]
Tianjie Xu7e520d22018-08-13 16:41:30 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2018 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
17"""
18Parses a input care_map.txt in plain text format; converts it into the proto
19buf message; and writes the result to the output file.
20
21"""
22
23import argparse
24import logging
25import sys
26
27import care_map_pb2
28
29
30def GenerateCareMapProtoFromLegacyFormat(lines):
31 """Constructs a care map proto message from the lines of the input file."""
32
33 # Expected format of the legacy care_map.txt:
34 # system
35 # system's care_map ranges
36 # [vendor]
37 # [vendor's care_map ranges]
38 # ...
39 assert len(lines) % 2 == 0, "line count must be even: {}".format(len(lines))
40
41 care_map_proto = care_map_pb2.CareMap()
42 for index in range(0, len(lines), 2):
43 info = care_map_proto.partitions.add()
44 info.name = lines[index]
45 info.ranges = lines[index + 1]
46
47 logging.info("Adding '%s': '%s' to care map", info.name, info.ranges)
48
49 return care_map_proto
50
51
52def ParseProtoMessage(message):
53 """Parses the care_map proto message and returns its text representation.
54 Args:
55 message: care_map in protobuf message
56
57 Returns:
58 A string of the care_map information, similar to the care_map legacy
59 format.
60 """
61 care_map_proto = care_map_pb2.CareMap()
62 care_map_proto.MergeFromString(message)
63
64 info_list = []
65 for info in care_map_proto.partitions:
66 assert info.name, "partition name is required in care_map"
67 assert info.ranges, "source range is required in care_map"
68 info_list += [info.name, info.ranges]
69
70 # TODO(xunchang) add a flag to output id & fingerprint also.
71 return '\n'.join(info_list)
72
73
74def main(argv):
75 parser = argparse.ArgumentParser(
76 description=__doc__,
77 formatter_class=argparse.RawDescriptionHelpFormatter)
78 parser.add_argument("input_care_map",
79 help="Path to the legacy care_map file (or path to"
80 " care_map in protobuf format if --parse_proto is"
81 " specified).")
82 parser.add_argument("output_file",
83 help="Path to output file to write the result.")
84 parser.add_argument("--parse_proto", "-p", action="store_true",
85 help="Parses the input as proto message, and outputs"
86 " the care_map in plain text.")
87 parser.add_argument("--verbose", "-v", action="store_true")
88
89 args = parser.parse_args(argv)
90
91 logging_format = '%(filename)s %(levelname)s: %(message)s'
92 logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING,
93 format=logging_format)
94
95 with open(args.input_care_map, 'r') as input_care_map:
96 content = input_care_map.read()
97
98 if args.parse_proto:
99 result = ParseProtoMessage(content)
100 else:
101 care_map_proto = GenerateCareMapProtoFromLegacyFormat(
102 content.rstrip().splitlines())
103 result = care_map_proto.SerializeToString()
104
105 with open(args.output_file, 'w') as output:
106 output.write(result)
107
108
109if __name__ == '__main__':
110 main(sys.argv[1:])