1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-16 17:42:02 +00:00

cleanup pb2py, add debug to emu.sh

This commit is contained in:
Pavol Rusnak 2016-04-27 22:44:37 +02:00
parent 0a8870f110
commit ee3614ae6a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
7 changed files with 44 additions and 37 deletions

View File

@ -41,5 +41,8 @@ gdb: ## start remote gdb session which connects to the openocd
load: ## load contents of src into mass storage of trezor load: ## load contents of src into mass storage of trezor
rm -rf /run/media/${USER}/PYBFLASH/* rm -rf /run/media/${USER}/PYBFLASH/*
cp -a src/* /run/media/${USER}/PYBFLASH/ cp -a src/lib /run/media/${USER}/PYBFLASH/
cp -a src/playground /run/media/${USER}/PYBFLASH/
cp -a src/trezor /run/media/${USER}/PYBFLASH/
cp -a src/*.py /run/media/${USER}/PYBFLASH/
sync sync

7
emu.sh
View File

@ -1,5 +1,10 @@
#!/bin/bash #!/bin/bash
cd `dirname $0`/src cd `dirname $0`/src
rm -f ../pipe.* rm -f ../pipe.*
../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py if [ "$1" == -d ]; then
shift
gdb --args ../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py
else
../vendor/micropython/unix/micropython $* -O0 -X heapsize=100000 main.py
fi
rm -f ../pipe.* rm -f ../pipe.*

1
src/trezor/messages/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*.py

View File

@ -28,6 +28,6 @@ BLUE_GRAY = rgbcolor(0x60, 0x7D, 0x8B)
BLACK = rgbcolor(0x00, 0x00, 0x00) BLACK = rgbcolor(0x00, 0x00, 0x00)
WHITE = rgbcolor(0xFF, 0xFF, 0xFF) WHITE = rgbcolor(0xFF, 0xFF, 0xFF)
MONO = 0 MONO = const(0)
NORMAL = 1 NORMAL = const(1)
BOLD = 2 BOLD = const(2)

View File

@ -1,15 +1,12 @@
#!/bin/bash #!/bin/bash
CURDIR=$(pwd) CURDIR=$(pwd)
for i in types messages storage ; do
for i in messages types storage ; do
# Compile .proto files to python2 modules using google protobuf library # Compile .proto files to python2 modules using google protobuf library
cd $CURDIR/../../trezor-common/protob cd $CURDIR/../../trezor-common/protob
protoc --python_out=$CURDIR/pb2/ -I/usr/include -I. $i.proto protoc --python_out=$CURDIR/pb2/ -I/usr/include -I. $i.proto
# Convert google protobuf library to trezor's internal format # Convert google protobuf library to trezor's internal format
cd $CURDIR cd $CURDIR
./pb2py $i ../src/trezor/messages/ ./pb2py $i ../src/trezor/messages/
done done

2
tools/pb2/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*_pb2.py
*.pyc

View File

@ -7,34 +7,31 @@ from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
def process_type(t, cls): def process_type(t, cls):
imports = ["from protobuf import protobuf as p",] imports = ["from protobuf import protobuf as p",]
out = ["t = p.MessageType()", ] out = ["t = p.MessageType()", ]
print("Processing type %s" % t) print(" * type %s" % t)
TYPE_STRING = 9 TYPE_STRING = 9
TYPE_BYTES = 12 TYPE_BYTES = 12
TYPE_MESSAGE = 11
for k, v in cls.DESCRIPTOR.fields_by_name.items(): TYPE_MESSAGE = 11
#print k for v in sorted(cls.DESCRIPTOR.fields_by_name.values(), key=lambda x: x.number):
number = v.number number = v.number
fieldname = k fieldname = v.name
type = None type = None
repeated = v.label == 3 repeated = v.label == 3
required = v.label == 2 required = v.label == 2
#print v.has_default_value, v.default_value #print v.has_default_value, v.default_value
if v.type in (4, 13, 14): if v.type in (4, 13, 14):
# TYPE_UINT64 = 4 # TYPE_UINT64 = 4
# TYPE_UINT32 = 13 # TYPE_UINT32 = 13
# TYPE_ENUM = 14 # TYPE_ENUM = 14
type = 'p.UVarintType' type = 'p.UVarintType'
elif v.type == 9: elif v.type == 9:
# TYPE_STRING = 9 # TYPE_STRING = 9
type = 'p.UnicodeType' type = 'p.UnicodeType'
@ -42,7 +39,7 @@ def process_type(t, cls):
elif v.type == 8: elif v.type == 8:
# TYPE_BOOL = 8 # TYPE_BOOL = 8
type = 'p.BoolType' type = 'p.BoolType'
elif v.type == 12: elif v.type == 12:
# TYPE_BYTES = 12 # TYPE_BYTES = 12
type = 'p.BytesType' type = 'p.BytesType'
@ -69,7 +66,7 @@ def process_type(t, cls):
out.append("t.add_field(%d, '%s', %s%s%s)" % \ out.append("t.add_field(%d, '%s', %s%s%s)" % \
(number, fieldname, type, flags, default)) (number, fieldname, type, flags, default))
#print fieldname, number, type, repeated, default #print fieldname, number, type, repeated, default
#print v.__dict__ #print v.__dict__
#print v.CPPTYPE_STRING #print v.CPPTYPE_STRING
@ -81,36 +78,38 @@ def process_type(t, cls):
out.append("%s = t" % t) out.append("%s = t" % t)
return imports + out return imports + out
def process_enum(t, cls): def process_enum(t, cls):
out = [] out = []
print("Processing enum %s" % t) print(" * enum %s" % t)
for k, v in cls.items(): for k, v in cls.items():
# Remove type name from the beginning of the constant # Remove type name from the beginning of the constant
# For example "PinMatrixRequestType_Current" -> "Current" # For example "PinMatrixRequestType_Current" -> "Current"
if k.startswith("%s_" % t): if k.startswith("%s_" % t):
k = k.replace("%s_" % t, '') k = k.replace("%s_" % t, '')
# If type ends with *Type, but constant use type name without *Type, remove it too :) # If type ends with *Type, but constant use type name without *Type, remove it too :)
# For example "ButtonRequestType & ButtonRequest_Other" => "Other" # For example "ButtonRequestType & ButtonRequest_Other" => "Other"
if t.endswith("Type") and k.startswith("%s_" % t.replace("Type", '')): if t.endswith("Type") and k.startswith("%s_" % t.replace("Type", '')):
k = k.replace("%s_" % t.replace("Type", ''), '') k = k.replace("%s_" % t.replace("Type", ''), '')
out.append("%s = %s" % (k, v)) out.append("%s = const(%s)" % (k, v))
return out return out
def process_module(mod, genpath): def process_module(mod, genpath):
print("Processing module %s" % mod.__name__)
types = dict([(name, cls) for name, cls in mod.__dict__.items() if isinstance(cls, type)]) types = dict([(name, cls) for name, cls in mod.__dict__.items() if isinstance(cls, type)])
for t, cls in types.iteritems(): for t, cls in types.iteritems():
out = process_type(t, cls) out = process_type(t, cls)
write_to_file(genpath, t, out) write_to_file(genpath, t, out)
enums = dict([(name, cls) for name, cls in mod.__dict__.items() if isinstance(cls, EnumTypeWrapper)]) enums = dict([(name, cls) for name, cls in mod.__dict__.items() if isinstance(cls, EnumTypeWrapper)])
for t, cls in enums.iteritems(): for t, cls in enums.iteritems():
out = process_enum(t, cls) out = process_enum(t, cls)
write_to_file(genpath, t, out) write_to_file(genpath, t, out)
@ -118,13 +117,13 @@ def process_module(mod, genpath):
def write_to_file(genpath, t, out): def write_to_file(genpath, t, out):
# Write generated sourcecode to given file # Write generated sourcecode to given file
f = open(os.path.join(genpath, "%s.py" % t), 'w') f = open(os.path.join(genpath, "%s.py" % t), 'w')
out = ["# Automatically generated by ./pb2py"] + out out = ["# Automatically generated by pb2py"] + out
data = "\n".join(out) data = "\n".join(out)
f.write(data) f.write(data)
f.close() f.close()
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: ./pb2py modulename genpath") print("Usage: ./pb2py modulename genpath")