Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 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 | |
Zsolt Haraszti | bae1275 | 2016-10-10 09:55:30 -0700 | [diff] [blame] | 18 | """ |
| 19 | Load a protobuf description file or protoc CodeGeneratorRequest an make |
| 20 | sense of it |
| 21 | """ |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 22 | |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 23 | import os |
| 24 | import inspect |
| 25 | from collections import OrderedDict |
| 26 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 27 | import sys |
| 28 | |
| 29 | from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 30 | from google.protobuf.descriptor import FieldDescriptor, Descriptor |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 31 | from google.protobuf.descriptor_pb2 import FileDescriptorProto, MethodOptions |
| 32 | from google.protobuf.message import Message, DecodeError |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 33 | from simplejson import dumps |
| 34 | |
| 35 | from google.protobuf import descriptor_pb2 |
| 36 | |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 37 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 38 | class InvalidDescriptorError(Exception): pass |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class DescriptorParser(object): |
| 42 | |
| 43 | def __init__(self, ignore_empty_source_code_info=True): |
| 44 | self.ignore_empty_source_code_info = ignore_empty_source_code_info |
| 45 | self.catalog = {} |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 46 | self.meta, blob = self.load_root_descriptor() |
| 47 | self.load_descriptor(blob) |
| 48 | |
| 49 | def load_root_descriptor(self): |
| 50 | """Load descriptor.desc to make things more data driven""" |
| 51 | with open('descriptor.desc', 'r') as f: |
| 52 | blob = f.read() |
| 53 | proto = descriptor_pb2.FileDescriptorSet() |
| 54 | proto.ParseFromString(blob) |
| 55 | assert len(proto.file) == 1 |
| 56 | fdp = proto.file[0] |
| 57 | |
| 58 | # for i, (fd, v) in enumerate(fdp.ListFields()): |
| 59 | # assert isinstance(fd, FieldDescriptor) |
| 60 | # print fd.name, fd.full_name, fd.number, fd.type, fd.label, fd.message_type, type(v) |
| 61 | |
| 62 | return fdp, blob |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 63 | |
| 64 | def get_catalog(self): |
| 65 | return self.catalog |
| 66 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 67 | def load_descriptor(self, descriptor_blob, |
| 68 | fold_comments=True, |
| 69 | type_tag_name='_type'): |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 70 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 71 | # decode file descriptor set or if that is not possible, |
| 72 | # try plugin request |
| 73 | try: |
| 74 | message = descriptor_pb2.FileDescriptorSet() |
| 75 | message.ParseFromString(descriptor_blob) |
| 76 | except DecodeError: |
| 77 | message = CodeGeneratorRequest() |
| 78 | message.ParseFromString(descriptor_blob) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 79 | |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 80 | d = self.parse(message, type_tag_name=type_tag_name) |
| 81 | print d.keys() |
| 82 | for _file in d.get('file', None) or d['proto_file']: |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 83 | if fold_comments: |
| 84 | self.fold_comments_in(_file) |
| 85 | self.catalog[_file['package']] = _file |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 86 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 87 | def parse_message(self, m, type_tag_name=None): |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 88 | assert isinstance(m, Message) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 89 | d = OrderedDict() |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 90 | for fd, v in m.ListFields(): |
| 91 | assert isinstance(fd, FieldDescriptor) |
| 92 | if fd.label in (1, 2): |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 93 | d[fd.name] = self.parse(v, type_tag_name) |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 94 | elif fd.label == 3: |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 95 | d[fd.name] = [self.parse(x, type_tag_name) for x in v] |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 96 | else: |
| 97 | raise InvalidDescriptorError() |
| 98 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 99 | if type_tag_name is not None: |
| 100 | d[type_tag_name] = m.DESCRIPTOR.full_name |
| 101 | |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 102 | return d |
| 103 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 104 | parser_table = { |
| 105 | unicode: lambda x: x, |
| 106 | int: lambda x: x, |
| 107 | bool: lambda x: x, |
| 108 | } |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 109 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 110 | def parse(self, o, type_tag_name=None): |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 111 | if isinstance(o, Message): |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 112 | return self.parse_message(o, type_tag_name) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 113 | else: |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 114 | return self.parser_table[type(o)](o) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 115 | |
| 116 | def fold_comments_in(self, descriptor): |
| 117 | assert isinstance(descriptor, dict) |
| 118 | |
| 119 | locations = descriptor.get('source_code_info', {}).get('location', []) |
| 120 | for location in locations: |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 121 | path = location.get('path', []) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 122 | comments = ''.join([ |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 123 | location.get('leading_comments', '').strip(' '), |
| 124 | location.get('trailing_comments', '').strip(' '), |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 125 | ''.join(block.strip(' ') for block |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 126 | in location.get('leading_detached_comments', '')) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 127 | ]).strip() |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 128 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 129 | # ignore locations with no comments |
| 130 | if not comments: |
| 131 | continue |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 132 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 133 | # we ignore path with odd number of entries, since these do |
| 134 | # not address our schema nodes, but rather the meta schema |
| 135 | if (len(path) % 2 == 0): |
| 136 | node = self.find_node_by_path( |
| 137 | path, self.meta.DESCRIPTOR, descriptor) |
| 138 | assert isinstance(node, dict) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 139 | node['_description'] = comments |
| 140 | |
| 141 | # remove source_code_info |
| 142 | del descriptor['source_code_info'] |
| 143 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 144 | def find_node_by_path(self, path, meta, o): |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 145 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 146 | # stop recursion when path is empty |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 147 | if not path: |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 148 | return o |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 149 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 150 | # sanity check |
| 151 | assert len(path) >= 2 |
| 152 | assert isinstance(meta, Descriptor) |
| 153 | assert isinstance(o, dict) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 154 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 155 | # find field name, then actual field |
| 156 | field_number = path.pop(0) |
| 157 | field_def = meta.fields_by_number[field_number] |
| 158 | field = o[field_def.name] |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 159 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 160 | # field must be a list, extract entry with given index |
| 161 | assert isinstance(field, list) # expected to be a list field |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 162 | index = path.pop(0) |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 163 | child_o = field[index] |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 164 | |
Zsolt Haraszti | d9f400f | 2016-09-25 17:19:49 -0700 | [diff] [blame] | 165 | child_meta = field_def.message_type |
| 166 | return self.find_node_by_path(path, child_meta, child_o) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | if __name__ == '__main__': |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 170 | |
Zsolt Haraszti | 80259ec | 2016-09-25 22:38:03 -0700 | [diff] [blame] | 171 | # try loading voltha descriptor and turn it into JSON data as a preparation |
| 172 | # for generating JSON Schema / swagger file (to be done later) |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 173 | if len(sys.argv) >= 2: |
| 174 | desc_file = sys.argv[1] |
| 175 | else: |
| 176 | desc_dir = os.path.dirname(inspect.getfile(voltha_pb2)) |
| 177 | desc_file = os.path.join(desc_dir, 'voltha.desc') |
| 178 | |
Zsolt Haraszti | 5cd6470 | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 179 | from voltha.protos import voltha_pb2 |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 180 | with open(desc_file, 'rb') as f: |
| 181 | descriptor_blob = f.read() |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 182 | |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 183 | parser = DescriptorParser() |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 184 | parser.save_file_desc = '/tmp/grpc_introspection.out' |
| 185 | |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 186 | parser.load_descriptor(descriptor_blob) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 187 | print dumps(parser.get_catalog(), indent=4) |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 188 | sys.exit(0) |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 189 | |
Zsolt Haraszti | 80259ec | 2016-09-25 22:38:03 -0700 | [diff] [blame] | 190 | # try to see if we can decode binary data into JSON automatically |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 191 | from random import seed, randint |
| 192 | seed(0) |
| 193 | |
| 194 | def make_mc(name, n_children=0): |
| 195 | mc = voltha_pb2.MoreComplex( |
| 196 | name=name, |
| 197 | foo_counter=randint(0, 10000), |
| 198 | health=voltha_pb2.HealthStatus( |
| 199 | state=voltha_pb2.HealthStatus.OVERLOADED |
| 200 | ), |
Zsolt Haraszti | 034db37 | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 201 | address=voltha_pb2.Address( |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 202 | street='1383 N McDowell Blvd', |
| 203 | city='Petaluma', |
| 204 | zip=94954, |
| 205 | state='CA' |
| 206 | ), |
| 207 | children=[make_mc('child%d' % (i + 1)) for i in xrange(n_children)] |
| 208 | ) |
Zsolt Haraszti | 80259ec | 2016-09-25 22:38:03 -0700 | [diff] [blame] | 209 | return mc |
Zsolt Haraszti | 162de17 | 2016-09-25 14:49:30 -0700 | [diff] [blame] | 210 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 211 | mc = make_mc('root', 3) |
Zsolt Haraszti | 80259ec | 2016-09-25 22:38:03 -0700 | [diff] [blame] | 212 | blob = mc.SerializeToString() |
| 213 | print len(blob), 'bytes' |
| 214 | mc2 = voltha_pb2.MoreComplex() |
| 215 | mc2.ParseFromString(blob) |
| 216 | assert mc == mc2 |
| 217 | |
Zsolt Haraszti | 0650d1a | 2016-09-26 17:29:25 -0700 | [diff] [blame] | 218 | print dumps(parser.parse(mc, type_tag_name='_type'), indent=4) |