_utils.py 1.1 KB

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