20 from grpc_tools
import _protoc_compiler
23 _PROTO_MODULE_SUFFIX =
"_pb2"
24 _SERVICE_MODULE_SUFFIX =
"_pb2_grpc"
26 _DISABLE_DYNAMIC_STUBS =
"GRPC_PYTHON_DISABLE_DYNAMIC_STUBS"
29 def main(command_arguments):
30 """Run the protocol buffer compiler with the given command-line arguments.
33 command_arguments: a list of strings representing command line arguments to
36 command_arguments = [argument.encode()
for argument
in command_arguments]
37 return _protoc_compiler.run_main(command_arguments)
41 if sys.version_info >= (3, 5, 0):
45 import importlib.machinery
48 _FINDERS_INSTALLED =
False
49 _FINDERS_INSTALLED_LOCK = threading.Lock()
52 global _FINDERS_INSTALLED
53 with _FINDERS_INSTALLED_LOCK:
54 if not _FINDERS_INSTALLED:
55 sys.meta_path.extend([
57 _protoc_compiler.get_protos),
59 _protoc_compiler.get_services)
62 pkg_resources.resource_filename(
'grpc_tools',
'_proto'))
63 _FINDERS_INSTALLED =
True
66 components = module_name.split(
".")
67 proto_name = components[-1][:-1 *
len(suffix)]
70 return "/".join(components[:-1] + [proto_name +
".proto"])
73 components = proto_file.split(os.path.sep)
74 proto_base_name = os.path.splitext(components[-1])[0]
75 return ".".join(components[:-1] + [proto_base_name + suffix])
78 """Returns a gRPC module generated from the indicated proto file."""
82 module = importlib.import_module(module_name)
86 """Returns a module generated from the indicated proto file."""
91 module = importlib.import_module(module_name)
95 """Returns two modules, corresponding to _pb2.py and _pb2_grpc.py files."""
98 _proto_code_cache = {}
99 _proto_code_cache_lock = threading.RLock()
103 def __init__(self, suffix, codegen_fn, module_name, protobuf_path,
115 components = filepath.split(os.path.sep)
116 return ".".join(components[:-1] +
117 [os.path.splitext(components[-1])[0]])
122 with _proto_code_cache_lock:
125 exec(code, module.__dict__)
129 [path.encode(
'ascii')
for path
in sys.path])
138 if module_name
not in sys.modules:
139 if module_name
not in _proto_code_cache:
140 _proto_code_cache[module_name] = f[1]
141 importlib.import_module(module_name)
142 exec(files[-1][1], module.__dict__)
151 if not fullname.endswith(self.
_suffix):
154 for search_path
in sys.path:
156 prospective_path = os.path.join(search_path, filepath)
157 os.stat(prospective_path)
158 except (FileNotFoundError, NotADirectoryError, OSError):
161 return importlib.machinery.ModuleSpec(
164 filepath, search_path))
168 if not os.getenv(_DISABLE_DYNAMIC_STUBS):
171 if __name__ ==
'__main__':
172 proto_include = pkg_resources.resource_filename(
'grpc_tools',
'_proto')
173 sys.exit(
main(sys.argv + [
'-I{}'.
format(proto_include)]))