2016-04-26 23:32:57 +00:00
|
|
|
#!/bin/bash
|
2018-05-09 11:27:39 +00:00
|
|
|
set -e
|
|
|
|
|
|
|
|
IS_CORE=""
|
|
|
|
|
|
|
|
if [ "$1" == "--core" ]; then
|
|
|
|
shift
|
|
|
|
IS_CORE=yes
|
|
|
|
elif [ "$1" == "--no-core" ]; then
|
|
|
|
shift
|
|
|
|
elif echo $PWD | grep -q "trezor-core"; then
|
|
|
|
IS_CORE=yes
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
OUTDIR=`readlink -f "$1"`
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
|
|
|
|
# set up paths
|
|
|
|
INDEX="__init__.py"
|
|
|
|
GENPATH="${OUTDIR:-../src/trezor/messages}"
|
2018-05-09 12:11:43 +00:00
|
|
|
PROTO_PATH="../vendor/trezor-common/protob"
|
2018-05-09 11:27:39 +00:00
|
|
|
PROTO_FILES="types messages"
|
|
|
|
|
|
|
|
# set up temporary directory & cleanup
|
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
function cleanup {
|
|
|
|
rm -r $TMPDIR
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# set up pb2 outdir
|
|
|
|
PB2_OUT="$TMPDIR/pb2"
|
|
|
|
mkdir -p "$PB2_OUT"
|
|
|
|
|
|
|
|
# compile .proto files to python2 modules using google protobuf library
|
|
|
|
for file in $PROTO_FILES; do
|
|
|
|
protoc --python_out="$PB2_OUT" -I/usr/include -I"$PROTO_PATH" "$PROTO_PATH/$file.proto"
|
2016-10-03 16:16:15 +00:00
|
|
|
done
|
2016-04-26 23:32:57 +00:00
|
|
|
|
2017-09-25 14:01:22 +00:00
|
|
|
|
2018-05-09 11:27:39 +00:00
|
|
|
if [ -n "$IS_CORE" ]; then
|
|
|
|
# generate for micropython
|
|
|
|
PB2PY_OPTS="-m"
|
|
|
|
else
|
|
|
|
# create index (__init__.py)
|
|
|
|
echo "# Automatically generated by pb2py" > $TMPDIR/$INDEX
|
|
|
|
echo >> $TMPDIR/$INDEX
|
|
|
|
PB2PY_OPTS="-l $TMPDIR/$INDEX"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# convert google protobuf library to trezor's internal format
|
|
|
|
for file in $PROTO_FILES; do
|
|
|
|
./pb2py $PB2PY_OPTS -P "trezorlib.protobuf" -p "$PB2_OUT" "$file" "$TMPDIR"
|
2016-04-26 23:32:57 +00:00
|
|
|
done
|
2016-10-03 16:16:15 +00:00
|
|
|
|
2018-05-09 11:27:39 +00:00
|
|
|
if [ -n "$IS_CORE" ]; then
|
|
|
|
cp "$TMPDIR/MessageType.py" "$TMPDIR/wire_types.py"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|