Skip to content

Commit

Permalink
Fix protoc in Windows binary build (#1051)
Browse files Browse the repository at this point in the history
With grpcio-tools >= 1.69, the grpc_tools.grpc_version import
is not automatically detected by pyinstaller.

This commit also fixes a bug where the failed import resulted
in recursive self-call on Windows, and cleans up other PyInstaller-specific
imports.
  • Loading branch information
PetteriAimonen committed Jan 10, 2025
1 parent 3aeb525 commit fc7d53c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
14 changes: 0 additions & 14 deletions generator/nanopb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
os.putenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python")
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"

try:
# Make sure grpc_tools gets included in binary package if it is available
import grpc_tools.protoc
except:
pass

try:
import google.protobuf.text_format as text_format
import google.protobuf.descriptor_pb2 as descriptor
Expand Down Expand Up @@ -75,14 +69,6 @@
# by env variable NANOPB_PB2_NO_REBUILD
nanopb_pb2 = proto.load_nanopb_pb2()

try:
# Add some dummy imports to keep packaging tools happy.
import google # bbfreeze seems to need these
from proto import nanopb_pb2 # pyinstaller seems to need this
except:
# Don't care, we will error out later if it is actually important.
pass

# ---------------------------------------------------------------------------
# Generation of single fields
# ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions generator/proto/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import traceback

try:
# For PyInstaller compatibility with grpcio-tools >= 1.69
import grpc_tools.grpc_version
except ImportError:
pass

def has_grpcio_protoc(verbose = False):
# type: () -> bool
""" checks if grpcio-tools protoc is installed"""
Expand Down
8 changes: 6 additions & 2 deletions generator/protoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import os.path
# Depending on how this script is run, we may or may not have PEP366 package name
# available for relative imports.
if not __package__:
from proto._utils import invoke_protoc
from proto._utils import has_grpcio_protoc, invoke_protoc
else:
from .proto._utils import invoke_protoc
from .proto._utils import has_grpcio_protoc, invoke_protoc

if __name__ == '__main__':
# Get path of the directory where this script is stored.
if getattr(sys, 'frozen', False):
mypath = os.path.dirname(sys.executable) # For pyInstaller

# The whole point of the binary package is to include grpcio,
# so better to report it as error if it the import fails.
assert has_grpcio_protoc(verbose = True)
else:
mypath = os.path.dirname(__file__)

Expand Down

0 comments on commit fc7d53c

Please sign in to comment.