|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +set -e |
| 4 | + |
3 | 5 | #Uncomment to run hack |
4 | 6 | #gsed -i 's/import "\//import ".\//g' ./protobufs/meshtastic/* |
5 | 7 | #gsed -i 's/package meshtastic;//g' ./protobufs/meshtastic/* |
6 | 8 |
|
7 | 9 | # protoc looks for mypy plugin in the python path |
8 | 10 | source $(poetry env info --path)/bin/activate |
9 | 11 |
|
10 | | -./nanopb-0.4.8/generator-bin/protoc -I=protobufs --python_out ./ --mypy_out ./ ./protobufs/meshtastic/*.proto |
11 | | -./nanopb-0.4.8/generator-bin/protoc -I=protobufs --python_out ./meshtastic/ --mypy_out ./meshtastic/ ./protobufs/nanopb.proto |
| 12 | +# Put our temp files in the poetry build directory |
| 13 | +TMPDIR=./build/meshtastic/protofixup |
| 14 | +echo "Fixing up protobuf paths in ${TMPDIR} temp directory" |
| 15 | + |
| 16 | + |
| 17 | +# Ensure a clean build |
| 18 | +rm -r "${TMPDIR}" |
12 | 19 |
|
13 | | -# workaround for import bug in protoc https://github.com/protocolbuffers/protobuf/issues/1491#issuecomment-690618628 |
| 20 | +INDIR=${TMPDIR}/in/meshtastic/protobuf |
| 21 | +OUTDIR=${TMPDIR}/out |
| 22 | +PYIDIR=${TMPDIR}/out |
| 23 | +mkdir -p "${OUTDIR}" "${INDIR}" "${PYIDIR}" |
| 24 | +cp ./protobufs/meshtastic/*.proto "${INDIR}" |
14 | 25 |
|
| 26 | +# OS-X sed is apparently a little different and expects an arg for -i |
15 | 27 | if [[ $OSTYPE == 'darwin'* ]]; then |
16 | | - sed -i '' -E 's/^(import.*_pb2)/from . \1/' meshtastic/*.py |
17 | | - # automate the current workaround (may be related to Meshtastic-protobufs issue #27 https://github.com/meshtastic/protobufs/issues/27) |
18 | | - sed -i '' -E "s/^None = 0/globals()['None'] = 0/" meshtastic/mesh_pb2.py |
| 28 | + SEDCMD="sed -i '' -E" |
19 | 29 | else |
20 | | - sed -i -e 's/^import.*_pb2/from . \0/' meshtastic/*.py |
21 | | - # automate the current workaround (may be related to Meshtastic-protobufs issue #27 https://github.com/meshtastic/protobufs/issues/27) |
22 | | - sed -i -e "s/^None = 0/globals()['None'] = 0/" meshtastic/mesh_pb2.py |
| 30 | + SEDCMD="sed -i -E" |
23 | 31 | fi |
| 32 | + |
| 33 | + |
| 34 | +# change the package names to meshtastic.protobuf |
| 35 | +$SEDCMD 's/^package meshtastic;/package meshtastic.protobuf;/' "${INDIR}/"*.proto |
| 36 | +# fix the imports to match |
| 37 | +$SEDCMD 's/^import "meshtastic\//import "meshtastic\/protobuf\//' "${INDIR}/"*.proto |
| 38 | + |
| 39 | +# Generate the python files |
| 40 | +./nanopb-0.4.8/generator-bin/protoc -I=$TMPDIR/in --python_out "${OUTDIR}" "--mypy_out=${PYIDIR}" $INDIR/*.proto |
| 41 | + |
| 42 | +# Change "from meshtastic.protobuf import" to "from . import" |
| 43 | +$SEDCMD 's/^from meshtastic.protobuf import/from . import/' "${OUTDIR}"/meshtastic/protobuf/*pb2*.py[i] |
| 44 | + |
| 45 | +# Create a __init__.py in the out directory |
| 46 | +touch "${OUTDIR}/meshtastic/protobuf/__init__.py" |
| 47 | + |
| 48 | +# Copy to the source controlled tree |
| 49 | +mkdir -p meshtastic/protobuf |
| 50 | +rm -rf meshtastic/protobuf/*pb2*.py |
| 51 | +cp "${OUTDIR}/meshtastic/protobuf"/* meshtastic/protobuf |
| 52 | + |
| 53 | +exit 0 |
0 commit comments