1#!/usr/bin/env python3 2# This file acts as a drop-in replacement of binary protoc.exe. 3# It will use either Python-based protoc from grpcio-tools package, 4# or if it is not available, protoc.exe from path if found. 5 6import sys 7import os 8import os.path 9 10# Depending on how this script is run, we may or may not have PEP366 package name 11# available for relative imports. 12if not __package__: 13 from proto._utils import invoke_protoc 14else: 15 from .proto._utils import invoke_protoc 16 17if __name__ == '__main__': 18 # Get path of the directory where this script is stored. 19 if getattr(sys, 'frozen', False): 20 mypath = os.path.dirname(sys.executable) # For pyInstaller 21 else: 22 mypath = os.path.dirname(__file__) 23 24 # Avoid recursive calls to self 25 env_paths = os.environ["PATH"].split(os.pathsep) 26 if mypath in env_paths: 27 env_paths.remove(mypath) 28 os.environ["PATH"] = os.pathsep.join(env_paths) 29 30 # Add argument for finding the nanopb generator when using --nanopb_out= 31 # argument to protoc. 32 if os.path.isfile(os.path.join(mypath, "protoc-gen-nanopb.exe")): 33 protoc_gen_nanopb = os.path.join(mypath, "protoc-gen-nanopb.exe") 34 elif os.name == 'nt': 35 protoc_gen_nanopb = os.path.join(mypath, "protoc-gen-nanopb.bat") 36 else: 37 protoc_gen_nanopb = os.path.join(mypath, "protoc-gen-nanopb") 38 39 args = sys.argv[1:] 40 41 if os.path.isfile(protoc_gen_nanopb): 42 args = ['--plugin=protoc-gen-nanopb=%s' % protoc_gen_nanopb] + args 43 44 status = invoke_protoc(['protoc'] + args) 45 sys.exit(status) 46