2021-02-25 01:02:25 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-08-14 01:41:30 +02:00
|
|
|
#
|
|
|
|
# Copyright (C) 2018 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Parses a input care_map.txt in plain text format; converts it into the proto
|
|
|
|
buf message; and writes the result to the output file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import care_map_pb2
|
|
|
|
|
|
|
|
|
2018-09-11 02:36:11 +02:00
|
|
|
def GenerateCareMapProtoFromLegacyFormat(lines, fingerprint_enabled):
|
2018-08-14 01:41:30 +02:00
|
|
|
"""Constructs a care map proto message from the lines of the input file."""
|
|
|
|
|
|
|
|
# Expected format of the legacy care_map.txt:
|
|
|
|
# system
|
|
|
|
# system's care_map ranges
|
2018-09-11 02:36:11 +02:00
|
|
|
# [system's fingerprint property id]
|
|
|
|
# [system's fingerprint]
|
2018-08-14 01:41:30 +02:00
|
|
|
# [vendor]
|
|
|
|
# [vendor's care_map ranges]
|
2018-09-11 02:36:11 +02:00
|
|
|
# [vendor's fingerprint property id]
|
|
|
|
# [vendor's fingerprint]
|
2018-08-14 01:41:30 +02:00
|
|
|
# ...
|
2018-09-11 02:36:11 +02:00
|
|
|
|
|
|
|
step = 4 if fingerprint_enabled else 2
|
|
|
|
assert len(lines) % step == 0, \
|
|
|
|
"line count must be multiple of {}: {}".format(step, len(lines))
|
2018-08-14 01:41:30 +02:00
|
|
|
|
|
|
|
care_map_proto = care_map_pb2.CareMap()
|
2018-09-11 02:36:11 +02:00
|
|
|
for index in range(0, len(lines), step):
|
2018-08-14 01:41:30 +02:00
|
|
|
info = care_map_proto.partitions.add()
|
|
|
|
info.name = lines[index]
|
|
|
|
info.ranges = lines[index + 1]
|
2018-09-11 02:36:11 +02:00
|
|
|
if fingerprint_enabled:
|
|
|
|
info.id = lines[index + 2]
|
|
|
|
info.fingerprint = lines[index + 3]
|
|
|
|
logging.info("Care map info: name %s, ranges %s, id %s, fingerprint %s",
|
|
|
|
info.name, info.ranges, info.id, info.fingerprint)
|
2018-08-14 01:41:30 +02:00
|
|
|
|
|
|
|
return care_map_proto
|
|
|
|
|
|
|
|
|
2018-09-11 02:36:11 +02:00
|
|
|
def ParseProtoMessage(message, fingerprint_enabled):
|
2018-08-14 01:41:30 +02:00
|
|
|
"""Parses the care_map proto message and returns its text representation.
|
|
|
|
Args:
|
2018-09-11 02:36:11 +02:00
|
|
|
message: Care_map in protobuf format.
|
|
|
|
fingerprint_enabled: Input protobuf message contains the fields 'id' and
|
|
|
|
'fingerprint'.
|
2018-08-14 01:41:30 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string of the care_map information, similar to the care_map legacy
|
|
|
|
format.
|
|
|
|
"""
|
|
|
|
care_map_proto = care_map_pb2.CareMap()
|
|
|
|
care_map_proto.MergeFromString(message)
|
|
|
|
|
|
|
|
info_list = []
|
|
|
|
for info in care_map_proto.partitions:
|
|
|
|
assert info.name, "partition name is required in care_map"
|
|
|
|
assert info.ranges, "source range is required in care_map"
|
|
|
|
info_list += [info.name, info.ranges]
|
2018-09-11 02:36:11 +02:00
|
|
|
if fingerprint_enabled:
|
|
|
|
assert info.id, "property id is required in care_map"
|
|
|
|
assert info.fingerprint, "fingerprint is required in care_map"
|
|
|
|
info_list += [info.id, info.fingerprint]
|
2018-08-14 01:41:30 +02:00
|
|
|
|
|
|
|
return '\n'.join(info_list)
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
|
|
parser.add_argument("input_care_map",
|
|
|
|
help="Path to the legacy care_map file (or path to"
|
|
|
|
" care_map in protobuf format if --parse_proto is"
|
|
|
|
" specified).")
|
|
|
|
parser.add_argument("output_file",
|
|
|
|
help="Path to output file to write the result.")
|
2018-09-11 02:36:11 +02:00
|
|
|
parser.add_argument("--no_fingerprint", action="store_false",
|
|
|
|
dest="fingerprint_enabled",
|
|
|
|
help="The 'id' and 'fingerprint' fields are disabled in"
|
|
|
|
" the caremap.")
|
2018-08-14 01:41:30 +02:00
|
|
|
parser.add_argument("--parse_proto", "-p", action="store_true",
|
|
|
|
help="Parses the input as proto message, and outputs"
|
|
|
|
" the care_map in plain text.")
|
|
|
|
parser.add_argument("--verbose", "-v", action="store_true")
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
logging_format = '%(filename)s %(levelname)s: %(message)s'
|
|
|
|
logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING,
|
|
|
|
format=logging_format)
|
|
|
|
|
2021-10-12 23:43:40 +02:00
|
|
|
with open(args.input_care_map, 'rb') as input_care_map:
|
2018-08-14 01:41:30 +02:00
|
|
|
content = input_care_map.read()
|
|
|
|
|
|
|
|
if args.parse_proto:
|
2021-02-25 01:02:25 +01:00
|
|
|
result = ParseProtoMessage(content, args.fingerprint_enabled).encode()
|
2018-08-14 01:41:30 +02:00
|
|
|
else:
|
|
|
|
care_map_proto = GenerateCareMapProtoFromLegacyFormat(
|
2021-10-12 23:43:40 +02:00
|
|
|
content.decode().rstrip().splitlines(), args.fingerprint_enabled)
|
2018-08-14 01:41:30 +02:00
|
|
|
result = care_map_proto.SerializeToString()
|
|
|
|
|
2021-02-25 01:02:25 +01:00
|
|
|
with open(args.output_file, 'wb') as output:
|
2018-08-14 01:41:30 +02:00
|
|
|
output.write(result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|