diff options
Diffstat (limited to 'generator/proto/_utils.py')
-rw-r--r-- | generator/proto/_utils.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/generator/proto/_utils.py b/generator/proto/_utils.py index f9c8c94..1d0d2b1 100644 --- a/generator/proto/_utils.py +++ b/generator/proto/_utils.py @@ -12,6 +12,29 @@ def has_grpcio_protoc(): return False return True +def get_proto_builtin_include_path(): + """Find include path for standard google/protobuf includes and for + nanopb.proto. + """ + + if getattr(sys, 'frozen', False): + # Pyinstaller package + paths = [ + os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'proto'), + os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'grpc_tools', '_proto') + ] + + else: + # Stand-alone script + paths = [ + os.path.dirname(os.path.abspath(__file__)) + ] + + if has_grpcio_protoc(): + import pkg_resources + paths.append(pkg_resources.resource_filename('grpc_tools', '_proto')) + + return paths def invoke_protoc(argv): # type: (list) -> typing.Any @@ -30,15 +53,11 @@ def invoke_protoc(argv): argv.append("-I.") # Add default protoc include paths - nanopb_include = os.path.dirname(os.path.abspath(__file__)) - argv.append('-I' + nanopb_include) + for incpath in get_proto_builtin_include_path(): + argv.append('-I' + incpath) if has_grpcio_protoc(): import grpc_tools.protoc as protoc - import pkg_resources - proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') - argv.append('-I' + proto_include) - return protoc.main(argv) else: return subprocess.call(argv) @@ -56,6 +75,11 @@ def print_versions(): sys.stderr.write("Failed to determine protoc version: " + str(e) + "\n") try: + sys.stderr.write("protoc builtin include path: " + str(get_proto_builtin_include_path()) + "\n") + except Exception as e: + sys.stderr.write("Failed to construct protoc include path: " + str(e) + "\n") + + try: import google.protobuf sys.stderr.write("Python version " + sys.version + "\n") sys.stderr.write("Using python-protobuf from " + google.protobuf.__file__ + "\n") |