Merge "Enable fingerprint in care_map"
This commit is contained in:
@@ -27,32 +27,44 @@ import sys
|
|||||||
import care_map_pb2
|
import care_map_pb2
|
||||||
|
|
||||||
|
|
||||||
def GenerateCareMapProtoFromLegacyFormat(lines):
|
def GenerateCareMapProtoFromLegacyFormat(lines, fingerprint_enabled):
|
||||||
"""Constructs a care map proto message from the lines of the input file."""
|
"""Constructs a care map proto message from the lines of the input file."""
|
||||||
|
|
||||||
# Expected format of the legacy care_map.txt:
|
# Expected format of the legacy care_map.txt:
|
||||||
# system
|
# system
|
||||||
# system's care_map ranges
|
# system's care_map ranges
|
||||||
|
# [system's fingerprint property id]
|
||||||
|
# [system's fingerprint]
|
||||||
# [vendor]
|
# [vendor]
|
||||||
# [vendor's care_map ranges]
|
# [vendor's care_map ranges]
|
||||||
|
# [vendor's fingerprint property id]
|
||||||
|
# [vendor's fingerprint]
|
||||||
# ...
|
# ...
|
||||||
assert len(lines) % 2 == 0, "line count must be even: {}".format(len(lines))
|
|
||||||
|
step = 4 if fingerprint_enabled else 2
|
||||||
|
assert len(lines) % step == 0, \
|
||||||
|
"line count must be multiple of {}: {}".format(step, len(lines))
|
||||||
|
|
||||||
care_map_proto = care_map_pb2.CareMap()
|
care_map_proto = care_map_pb2.CareMap()
|
||||||
for index in range(0, len(lines), 2):
|
for index in range(0, len(lines), step):
|
||||||
info = care_map_proto.partitions.add()
|
info = care_map_proto.partitions.add()
|
||||||
info.name = lines[index]
|
info.name = lines[index]
|
||||||
info.ranges = lines[index + 1]
|
info.ranges = lines[index + 1]
|
||||||
|
if fingerprint_enabled:
|
||||||
logging.info("Adding '%s': '%s' to care map", info.name, info.ranges)
|
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)
|
||||||
|
|
||||||
return care_map_proto
|
return care_map_proto
|
||||||
|
|
||||||
|
|
||||||
def ParseProtoMessage(message):
|
def ParseProtoMessage(message, fingerprint_enabled):
|
||||||
"""Parses the care_map proto message and returns its text representation.
|
"""Parses the care_map proto message and returns its text representation.
|
||||||
Args:
|
Args:
|
||||||
message: care_map in protobuf message
|
message: Care_map in protobuf format.
|
||||||
|
fingerprint_enabled: Input protobuf message contains the fields 'id' and
|
||||||
|
'fingerprint'.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A string of the care_map information, similar to the care_map legacy
|
A string of the care_map information, similar to the care_map legacy
|
||||||
@@ -66,8 +78,11 @@ def ParseProtoMessage(message):
|
|||||||
assert info.name, "partition name is required in care_map"
|
assert info.name, "partition name is required in care_map"
|
||||||
assert info.ranges, "source range is required in care_map"
|
assert info.ranges, "source range is required in care_map"
|
||||||
info_list += [info.name, info.ranges]
|
info_list += [info.name, info.ranges]
|
||||||
|
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]
|
||||||
|
|
||||||
# TODO(xunchang) add a flag to output id & fingerprint also.
|
|
||||||
return '\n'.join(info_list)
|
return '\n'.join(info_list)
|
||||||
|
|
||||||
|
|
||||||
@@ -81,6 +96,10 @@ def main(argv):
|
|||||||
" specified).")
|
" specified).")
|
||||||
parser.add_argument("output_file",
|
parser.add_argument("output_file",
|
||||||
help="Path to output file to write the result.")
|
help="Path to output file to write the result.")
|
||||||
|
parser.add_argument("--no_fingerprint", action="store_false",
|
||||||
|
dest="fingerprint_enabled",
|
||||||
|
help="The 'id' and 'fingerprint' fields are disabled in"
|
||||||
|
" the caremap.")
|
||||||
parser.add_argument("--parse_proto", "-p", action="store_true",
|
parser.add_argument("--parse_proto", "-p", action="store_true",
|
||||||
help="Parses the input as proto message, and outputs"
|
help="Parses the input as proto message, and outputs"
|
||||||
" the care_map in plain text.")
|
" the care_map in plain text.")
|
||||||
@@ -96,10 +115,10 @@ def main(argv):
|
|||||||
content = input_care_map.read()
|
content = input_care_map.read()
|
||||||
|
|
||||||
if args.parse_proto:
|
if args.parse_proto:
|
||||||
result = ParseProtoMessage(content)
|
result = ParseProtoMessage(content, args.fingerprint_enabled)
|
||||||
else:
|
else:
|
||||||
care_map_proto = GenerateCareMapProtoFromLegacyFormat(
|
care_map_proto = GenerateCareMapProtoFromLegacyFormat(
|
||||||
content.rstrip().splitlines())
|
content.rstrip().splitlines(), args.fingerprint_enabled)
|
||||||
result = care_map_proto.SerializeToString()
|
result = care_map_proto.SerializeToString()
|
||||||
|
|
||||||
with open(args.output_file, 'w') as output:
|
with open(args.output_file, 'w') as output:
|
||||||
|
|||||||
Reference in New Issue
Block a user