2017-12-12 15:40:11 +00:00
|
|
|
#!/bin/bash
|
2017-12-24 12:14:29 +00:00
|
|
|
set -e
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2017-12-24 12:14:29 +00:00
|
|
|
cd "$(dirname "$0")"
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# set up paths
|
|
|
|
INDEX="__init__.py"
|
2017-12-24 12:14:29 +00:00
|
|
|
GENPATH="../trezorlib/messages"
|
2018-03-26 14:17:45 +00:00
|
|
|
PROTO_PATH="../vendor/trezor-common/protob"
|
2018-03-15 18:28:34 +00:00
|
|
|
PROTO_FILES="types messages"
|
2017-12-12 15:40:11 +00:00
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# set up temporary directory & cleanup
|
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
function cleanup {
|
|
|
|
rm -r $TMPDIR
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
2017-12-24 12:14:29 +00:00
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# set up pb2 outdir
|
|
|
|
PB2_OUT="$TMPDIR/pb2"
|
2017-12-24 12:14:29 +00:00
|
|
|
mkdir -p "$PB2_OUT"
|
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# compile .proto files to python2 modules using google protobuf library
|
2018-03-15 18:28:34 +00:00
|
|
|
for file in $PROTO_FILES; do
|
2018-03-15 18:55:06 +00:00
|
|
|
protoc --python_out="$PB2_OUT" -I/usr/include -I"$PROTO_PATH" "$PROTO_PATH/$file.proto"
|
2017-12-12 15:40:11 +00:00
|
|
|
done
|
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# create index (__init__.py)
|
|
|
|
cat > "$TMPDIR/$INDEX" << EOF
|
|
|
|
# Automatically generated by pb2py
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# convert google protobuf library to trezor's internal format
|
2018-03-15 18:28:34 +00:00
|
|
|
for file in $PROTO_FILES; do
|
2018-03-26 16:55:45 +00:00
|
|
|
./pb2py -P "trezorlib.protobuf" -p "$PB2_OUT" -l "$TMPDIR/$INDEX" "$file" "$TMPDIR"
|
2017-12-12 15:40:11 +00:00
|
|
|
done
|
|
|
|
|
2018-03-26 16:55:45 +00:00
|
|
|
# ensure $GENPATH exists and is empty of messages
|
|
|
|
mkdir -p "$GENPATH"
|
|
|
|
# only remove messages - there could possibly be other files not starting with capital letter
|
|
|
|
rm -f "$GENPATH"/[A-Z]*.py
|
|
|
|
|
|
|
|
# move generated files to the destination
|
|
|
|
# (this assumes $INDEX is *.py, otherwise we'd have to add $INDEX separately)
|
|
|
|
mv "$TMPDIR"/*.py "$GENPATH"
|
|
|
|
|
|
|
|
# the exit trap handles removing the tmp directory
|