1import subprocess 2import os.path 3 4def has_grpcio_protoc(): 5 # type: () -> bool 6 """ checks if grpcio-tools protoc is installed""" 7 8 try: 9 import grpc_tools.protoc 10 except ImportError: 11 return False 12 return True 13 14 15def invoke_protoc(argv): 16 # type: (list) -> typing.Any 17 """ 18 Invoke protoc. 19 20 This routine will use grpcio-provided protoc if it exists, 21 using system-installed protoc as a fallback. 22 23 Args: 24 argv: protoc CLI invocation, first item must be 'protoc' 25 """ 26 27 # Add current directory to include path if nothing else is specified 28 if not [x for x in argv if x.startswith('-I')]: 29 argv.append("-I.") 30 31 # Add default protoc include paths 32 nanopb_include = os.path.dirname(os.path.abspath(__file__)) 33 argv.append('-I' + nanopb_include) 34 35 if has_grpcio_protoc(): 36 import grpc_tools.protoc as protoc 37 import pkg_resources 38 proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') 39 argv.append('-I' + proto_include) 40 41 return protoc.main(argv) 42 else: 43 return subprocess.call(argv) 44