1# Copyright (C) 2016 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15from google.protobuf import descriptor_pb2 16from google.protobuf import text_format 17import hashlib 18 19 20def parse_proto_to_ascii(binary_proto_msg): 21 """ Parses binary protobuf message to human readable ascii string. 22 23 Args: 24 binary_proto_msg: The binary format of the proto message. 25 Returns: 26 The ascii format of the proto message. 27 """ 28 return text_format.MessageToString(binary_proto_msg) 29 30 31def to_descriptor_proto(proto): 32 """Retrieves the descriptor proto for the given protobuf message. 33 34 Args: 35 proto: the original message. 36 Returns: 37 The descriptor proto for the input message. 38 """ 39 descriptor_proto = descriptor_pb2.DescriptorProto() 40 proto.DESCRIPTOR.CopyToProto(descriptor_proto) 41 return descriptor_proto 42 43 44def md5_proto(proto): 45 """ Obtains an md5 checksum of a proto's content.""" 46 encoded = parse_proto_to_ascii(proto).encode() 47 return hashlib.md5(encoded).hexdigest() 48