Skip to content

Commit f6d1b4b

Browse files
authored
Merge pull request #613 from geeksville/pr-moveproto
Move protobuf python glue from meshtastic to meshtastic.protobuf
2 parents f5febc5 + 62ce8ea commit f6d1b4b

78 files changed

Lines changed: 1736 additions & 1966 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[run]
2-
omit = meshtastic/*_pb2.py,meshtastic/tests/*.py,meshtastic/test.py
2+
omit = meshtastic/protobuf/*_pb2.py,meshtastic/tests/*.py,meshtastic/test.py
33

44
[report]
55
exclude_lines =

.github/workflows/update_protobufs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ jobs:
3737
git config --global user.email 'bot@noreply.github.com'
3838
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
3939
git add protobufs
40-
git add meshtastic
40+
git add meshtastic/protobuf
4141
git commit -m "Update protobuf submodule" && git push || echo "No changes to commit"

bin/regen-protobufs.sh

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
#!/bin/bash
22

3+
set -e
4+
35
#Uncomment to run hack
46
#gsed -i 's/import "\//import ".\//g' ./protobufs/meshtastic/*
57
#gsed -i 's/package meshtastic;//g' ./protobufs/meshtastic/*
68

79
# protoc looks for mypy plugin in the python path
810
source $(poetry env info --path)/bin/activate
911

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}"
1219

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}"
1425

26+
# OS-X sed is apparently a little different and expects an arg for -i
1527
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"
1929
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"
2331
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

meshtastic/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect
8181
from pubsub import pub # type: ignore[import-untyped]
8282
from tabulate import tabulate
8383

84-
from meshtastic import (
84+
from meshtastic.node import Node
85+
from meshtastic.util import DeferredExecution, Timeout, catchAndIgnore, fixme, stripnl
86+
87+
from .protobuf import (
8588
admin_pb2,
8689
apponly_pb2,
8790
channel_pb2,
@@ -93,10 +96,10 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect
9396
remote_hardware_pb2,
9497
storeforward_pb2,
9598
telemetry_pb2,
99+
)
100+
from . import (
96101
util,
97102
)
98-
from meshtastic.node import Node
99-
from meshtastic.util import DeferredExecution, Timeout, catchAndIgnore, fixme, stripnl
100103

101104
# Note: To follow PEP224, comments should be after the module variable.
102105

meshtastic/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import meshtastic.test
1818
import meshtastic.util
1919
from meshtastic import mt_config
20-
from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR
20+
from meshtastic.protobuf import channel_pb2, config_pb2, portnums_pb2
21+
from meshtastic import remote_hardware, BROADCAST_ADDR
2122
from meshtastic.version import get_active_version
2223
from meshtastic.ble_interface import BLEInterface
2324
from meshtastic.mesh_interface import MeshInterface

meshtastic/admin_pb2.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

meshtastic/apponly_pb2.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

meshtastic/atak_pb2.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

meshtastic/channel_pb2.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

meshtastic/clientonly_pb2.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)