2017-12-12 15:40:11 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Converts Google's protobuf python definitions of TREZOR wire messages
|
|
|
|
# to plain-python objects as used in TREZOR Core and python-trezor
|
|
|
|
|
|
|
|
import sys
|
2017-12-17 13:39:18 +00:00
|
|
|
import importlib
|
2017-12-12 15:40:11 +00:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
2017-12-17 13:39:18 +00:00
|
|
|
def import_pb2(name):
|
2017-12-24 12:04:36 +00:00
|
|
|
return importlib.import_module("%s_pb2" % name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def create_message_import(name):
|
|
|
|
return "from .%s import %s" % (name, name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def create_const(name, value, is_upy):
|
|
|
|
if is_upy:
|
|
|
|
return "%s = const(%s)" % (name, value)
|
|
|
|
else:
|
|
|
|
return "%s = %s" % (name, value)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def remove_from_start(s, prefix):
|
|
|
|
if s.startswith(prefix):
|
|
|
|
return s[len(prefix):]
|
|
|
|
else:
|
|
|
|
return s
|
2018-01-30 14:04:24 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def process_message(descriptor, protobuf_module, msg_id, indexfile, is_upy):
|
|
|
|
print(" * type %s" % descriptor.name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
imports = []
|
|
|
|
out = ["", "", "class %s(p.MessageType):" % descriptor.name, ]
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
if descriptor.fields_by_number:
|
|
|
|
out.append(" FIELDS = {")
|
|
|
|
elif msg_id is None:
|
|
|
|
out.append(" pass")
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
for number, field in descriptor.fields_by_number.items():
|
|
|
|
field_name = field.name
|
|
|
|
field_type = None
|
|
|
|
repeated = (field.label == field.LABEL_REPEATED)
|
|
|
|
required = (field.label == field.LABEL_REQUIRED)
|
|
|
|
|
|
|
|
types = {
|
|
|
|
field.TYPE_UINT64: 'p.UVarintType',
|
|
|
|
field.TYPE_UINT32: 'p.UVarintType',
|
|
|
|
field.TYPE_ENUM: 'p.UVarintType',
|
|
|
|
field.TYPE_SINT32: 'p.Sint32Type',
|
2018-03-15 18:28:34 +00:00
|
|
|
field.TYPE_SINT64: 'p.Sint64Type',
|
2017-12-17 15:12:39 +00:00
|
|
|
field.TYPE_STRING: 'p.UnicodeType',
|
|
|
|
field.TYPE_BOOL: 'p.BoolType',
|
|
|
|
field.TYPE_BYTES: 'p.BytesType'
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.type == field.TYPE_MESSAGE:
|
|
|
|
field_type = field.message_type.name
|
|
|
|
imports.append(create_message_import(field_type))
|
2017-12-12 15:40:11 +00:00
|
|
|
else:
|
2017-12-17 15:12:39 +00:00
|
|
|
try:
|
|
|
|
field_type = types[field.type]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError("Unknown field type %d for field %s" % (field.type, field_name))
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
comments = []
|
2017-12-12 15:40:11 +00:00
|
|
|
if required:
|
2017-12-17 15:12:39 +00:00
|
|
|
comments.append('required')
|
|
|
|
if field.has_default_value:
|
|
|
|
comments.append("default=%s" % repr(field.default_value))
|
|
|
|
|
|
|
|
if comments:
|
|
|
|
comment = " # %s" % ' '.join(comments)
|
2017-12-12 15:40:11 +00:00
|
|
|
else:
|
|
|
|
comment = ''
|
|
|
|
|
|
|
|
if repeated:
|
|
|
|
flags = 'p.FLAG_REPEATED'
|
|
|
|
else:
|
|
|
|
flags = '0'
|
|
|
|
|
|
|
|
out.append(" %d: ('%s', %s, %s),%s" %
|
2017-12-17 15:12:39 +00:00
|
|
|
(number, field_name, field_type, flags, comment))
|
|
|
|
|
|
|
|
if descriptor.fields_by_name:
|
2017-12-12 15:40:11 +00:00
|
|
|
out.append(" }")
|
|
|
|
|
|
|
|
if msg_id is not None:
|
|
|
|
out.append(" MESSAGE_WIRE_TYPE = %d" % msg_id)
|
|
|
|
if indexfile is not None:
|
2017-12-17 15:12:39 +00:00
|
|
|
indexfile.write(create_const(t, msg_id, is_upy))
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
# Remove duplicate imports
|
2018-01-12 11:58:39 +00:00
|
|
|
imports = sorted(list(set(imports)))
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
if is_upy:
|
|
|
|
imports = ['import protobuf as p'] + imports
|
|
|
|
else:
|
2018-03-15 18:28:34 +00:00
|
|
|
imports = ['from .. import protobuf as p'] + imports
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
return imports + out
|
|
|
|
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def process_enum(descriptor, is_upy):
|
2017-12-12 15:40:11 +00:00
|
|
|
out = []
|
|
|
|
|
|
|
|
if is_upy:
|
|
|
|
out += ("from micropython import const", "")
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
print(" * enum %s" % descriptor.name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
for name, value in descriptor.values_by_name.items():
|
2017-12-12 15:40:11 +00:00
|
|
|
# Remove type name from the beginning of the constant
|
|
|
|
# For example "PinMatrixRequestType_Current" -> "Current"
|
2017-12-17 15:12:39 +00:00
|
|
|
enum_prefix = descriptor.name
|
|
|
|
name = remove_from_start(name, "%s_" % enum_prefix)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
# If type ends with *Type, but constant use type name without *Type, remove it too :)
|
|
|
|
# For example "ButtonRequestType & ButtonRequest_Other" => "Other"
|
2017-12-17 15:12:39 +00:00
|
|
|
if enum_prefix.endswith("Type"):
|
|
|
|
enum_prefix, _ = enum_prefix.rsplit("Type", 1)
|
|
|
|
name = remove_from_start(name, "%s_" % enum_prefix)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
out.append(create_const(name, value.number, is_upy))
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
def process_file(descriptor, protobuf_module, genpath, indexfile, modlist, is_upy):
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
print("Processing module %s" % descriptor.name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
msg_types = import_pb2('messages').MessageType
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2018-03-15 18:55:26 +00:00
|
|
|
for name, message_descriptor in sorted(descriptor.message_types_by_name.items()):
|
2017-12-12 15:40:11 +00:00
|
|
|
# Find message type for given class
|
2017-12-17 15:12:39 +00:00
|
|
|
try:
|
|
|
|
msg_id = msg_types.Value("MessageType_%s" % name)
|
|
|
|
except ValueError:
|
|
|
|
msg_id = None
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
out = process_message(message_descriptor, protobuf_module, msg_id, indexfile, is_upy)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
write_to_file(genpath, name, out)
|
2017-12-12 15:40:11 +00:00
|
|
|
if modlist:
|
2017-12-17 15:12:39 +00:00
|
|
|
modlist.write(create_message_import(name) + "\n")
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-17 15:12:39 +00:00
|
|
|
for name, enum_descriptor in descriptor.enum_types_by_name.items():
|
|
|
|
out = process_enum(enum_descriptor, is_upy)
|
|
|
|
write_to_file(genpath, name, out)
|
2017-12-12 15:40:11 +00:00
|
|
|
if modlist:
|
2017-12-17 15:12:39 +00:00
|
|
|
modlist.write("from . import %s\n" % name)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_to_file(genpath, t, out):
|
|
|
|
# Write generated sourcecode to given file
|
|
|
|
f = open(os.path.join(genpath, "%s.py" % t), 'w')
|
|
|
|
out = ["# Automatically generated by pb2py"] + out
|
|
|
|
|
|
|
|
data = "\n".join(out) + "\n"
|
|
|
|
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
2017-12-24 12:04:36 +00:00
|
|
|
parser.add_argument('module', help="Name of module to generate")
|
|
|
|
parser.add_argument('genpath', help="Directory for generated source code")
|
2017-12-17 15:12:39 +00:00
|
|
|
parser.add_argument('-P', '--protobuf-module', default="protobuf", help="Name of protobuf module")
|
2017-12-17 13:39:18 +00:00
|
|
|
parser.add_argument('-i', '--indexfile', type=argparse.FileType('a'), help="Generate index file of wire types")
|
|
|
|
parser.add_argument('-l', '--modlist', type=argparse.FileType('a'), help="Generate list of modules")
|
|
|
|
parser.add_argument('-p', '--protopath', type=str, help="Path to search for pregenerated Google's python sources")
|
2017-12-12 15:40:11 +00:00
|
|
|
parser.add_argument('-m', '--micropython', action='store_true', help="Use micropython-favoured source code")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.protopath:
|
|
|
|
sys.path.append(args.protopath)
|
|
|
|
|
2017-12-24 12:04:36 +00:00
|
|
|
# This must be done after sys.path.append
|
|
|
|
module = import_pb2(args.module)
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-24 12:04:36 +00:00
|
|
|
process_file(module.DESCRIPTOR, args.protobuf_module, args.genpath, args.indexfile, args.modlist, args.micropython)
|