1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +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
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

7
emu.sh
View File

@ -1,5 +1,10 @@
#!/bin/bash
cd `dirname $0`/src
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.*

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)
WHITE = rgbcolor(0xFF, 0xFF, 0xFF)
MONO = 0
NORMAL = 1
BOLD = 2
MONO = const(0)
NORMAL = const(1)
BOLD = const(2)

View File

@ -1,15 +1,12 @@
#!/bin/bash
CURDIR=$(pwd)
for i in messages types storage ; do
for i in types messages storage ; do
# Compile .proto files to python2 modules using google protobuf library
cd $CURDIR/../../trezor-common/protob
protoc --python_out=$CURDIR/pb2/ -I/usr/include -I. $i.proto
# Convert google protobuf library to trezor's internal format
cd $CURDIR
./pb2py $i ../src/trezor/messages/
./pb2py $i ../src/trezor/messages/
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):
imports = ["from protobuf import protobuf as p",]
out = ["t = p.MessageType()", ]
print("Processing type %s" % t)
print(" * type %s" % t)
TYPE_STRING = 9
TYPE_BYTES = 12
TYPE_MESSAGE = 11
for k, v in cls.DESCRIPTOR.fields_by_name.items():
#print k
TYPE_MESSAGE = 11
for v in sorted(cls.DESCRIPTOR.fields_by_name.values(), key=lambda x: x.number):
number = v.number
fieldname = k
fieldname = v.name
type = None
repeated = v.label == 3
required = v.label == 2
#print v.has_default_value, v.default_value
if v.type in (4, 13, 14):
# TYPE_UINT64 = 4
# TYPE_UINT32 = 13
# TYPE_ENUM = 14
type = 'p.UVarintType'
elif v.type == 9:
# TYPE_STRING = 9
type = 'p.UnicodeType'
@ -42,7 +39,7 @@ def process_type(t, cls):
elif v.type == 8:
# TYPE_BOOL = 8
type = 'p.BoolType'
elif v.type == 12:
# TYPE_BYTES = 12
type = 'p.BytesType'
@ -69,7 +66,7 @@ def process_type(t, cls):
out.append("t.add_field(%d, '%s', %s%s%s)" % \
(number, fieldname, type, flags, default))
#print fieldname, number, type, repeated, default
#print v.__dict__
#print v.CPPTYPE_STRING
@ -81,36 +78,38 @@ def process_type(t, cls):
out.append("%s = t" % t)
return imports + out
def process_enum(t, cls):
out = []
print("Processing enum %s" % t)
print(" * enum %s" % t)
for k, v in cls.items():
# Remove type name from the beginning of the constant
# For example "PinMatrixRequestType_Current" -> "Current"
# For example "PinMatrixRequestType_Current" -> "Current"
if k.startswith("%s_" % t):
k = k.replace("%s_" % t, '')
# If type ends with *Type, but constant use type name without *Type, remove it too :)
# For example "ButtonRequestType & ButtonRequest_Other" => "Other"
if t.endswith("Type") and k.startswith("%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
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)])
for t, cls in types.iteritems():
out = process_type(t, cls)
write_to_file(genpath, t, out)
enums = dict([(name, cls) for name, cls in mod.__dict__.items() if isinstance(cls, EnumTypeWrapper)])
for t, cls in enums.iteritems():
out = process_enum(t, cls)
write_to_file(genpath, t, out)
@ -118,13 +117,13 @@ def process_module(mod, genpath):
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
out = ["# Automatically generated by pb2py"] + out
data = "\n".join(out)
f.write(data)
f.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: ./pb2py modulename genpath")