Be completely safe for Python 2

pull/21504/head
Richard Belleville 5 years ago
parent a0485f78aa
commit 928c6f23ea
  1. 1
      tools/distrib/python/grpcio_tools/grpc_tools/BUILD
  2. 243
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py
  3. 1
      tools/distrib/python/grpcio_tools/setup.py

@ -33,7 +33,6 @@ py_library(
srcs = ["__init__.py", "protoc.py"], srcs = ["__init__.py", "protoc.py"],
deps = [ deps = [
":cyprotoc", ":cyprotoc",
"@six_archive//:six",
"@com_google_protobuf//:protobuf_python", "@com_google_protobuf//:protobuf_python",
"//src/python/grpcio/grpc:grpcio", "//src/python/grpcio/grpc:grpcio",
], ],

@ -17,15 +17,8 @@
import pkg_resources import pkg_resources
import sys import sys
import six
import os import os
import contextlib
import importlib
import importlib.machinery
import sys
import threading
from grpc_tools import _protoc_compiler from grpc_tools import _protoc_compiler
_PROTO_MODULE_SUFFIX = "_pb2" _PROTO_MODULE_SUFFIX = "_pb2"
@ -43,125 +36,123 @@ def main(command_arguments):
return _protoc_compiler.run_main(command_arguments) return _protoc_compiler.run_main(command_arguments)
def _module_name_to_proto_file(suffix, module_name): if sys.version_info[0] > 2:
components = module_name.split(".") import contextlib
proto_name = components[-1][:-1 * len(suffix)] import importlib
return os.path.sep.join(components[:-1] + [proto_name + ".proto"]) import importlib.machinery
import threading
def _proto_file_to_module_name(suffix, proto_file): def _module_name_to_proto_file(suffix, module_name):
components = proto_file.split(os.path.sep) components = module_name.split(".")
proto_base_name = os.path.splitext(components[-1])[0] proto_name = components[-1][:-1 * len(suffix)]
return ".".join(components[:-1] + [proto_base_name + suffix]) return os.path.sep.join(components[:-1] + [proto_name + ".proto"])
def _proto_file_to_module_name(suffix, proto_file):
@contextlib.contextmanager components = proto_file.split(os.path.sep)
def _augmented_syspath(new_paths): proto_base_name = os.path.splitext(components[-1])[0]
original_sys_path = sys.path return ".".join(components[:-1] + [proto_base_name + suffix])
if new_paths is not None:
sys.path = sys.path + new_paths @contextlib.contextmanager
try: def _augmented_syspath(new_paths):
yield original_sys_path = sys.path
finally: if new_paths is not None:
sys.path = original_sys_path sys.path = sys.path + new_paths
try:
yield
# TODO: Investigate making this even more of a no-op in the case that we have finally:
# truly already imported the module. sys.path = original_sys_path
def _protos(protobuf_path, include_paths=None):
with _augmented_syspath(include_paths): # TODO: Investigate making this even more of a no-op in the case that we have
module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX, # truly already imported the module.
protobuf_path) def _protos(protobuf_path, include_paths=None):
module = importlib.import_module(module_name) with _augmented_syspath(include_paths):
return module module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX,
protobuf_path)
module = importlib.import_module(module_name)
def _services(protobuf_path, include_paths=None): return module
_protos(protobuf_path, include_paths)
with _augmented_syspath(include_paths): def _services(protobuf_path, include_paths=None):
module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX, _protos(protobuf_path, include_paths)
protobuf_path) with _augmented_syspath(include_paths):
module = importlib.import_module(module_name) module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX,
return module protobuf_path)
module = importlib.import_module(module_name)
return module
def _protos_and_services(protobuf_path, include_paths=None):
return (_protos(protobuf_path, include_paths=include_paths), def _protos_and_services(protobuf_path, include_paths=None):
_services(protobuf_path, include_paths=include_paths)) return (_protos(protobuf_path, include_paths=include_paths),
_services(protobuf_path, include_paths=include_paths))
_proto_code_cache = {} _proto_code_cache = {}
_proto_code_cache_lock = threading.RLock() _proto_code_cache_lock = threading.RLock()
class ProtoLoader(importlib.abc.Loader):
class ProtoLoader(importlib.abc.Loader):
def __init__(self, suffix, codegen_fn, module_name, protobuf_path,
def __init__(self, suffix, codegen_fn, module_name, protobuf_path, proto_root):
proto_root): self._suffix = suffix
self._suffix = suffix self._codegen_fn = codegen_fn
self._codegen_fn = codegen_fn self._module_name = module_name
self._module_name = module_name self._protobuf_path = protobuf_path
self._protobuf_path = protobuf_path self._proto_root = proto_root
self._proto_root = proto_root
def create_module(self, spec):
def create_module(self, spec): return None
return None
def _generated_file_to_module_name(self, filepath):
def _generated_file_to_module_name(self, filepath): components = filepath.split(os.path.sep)
components = filepath.split(os.path.sep) return ".".join(
return ".".join(components[:-1] + [os.path.splitext(components[-1])[0]]) components[:-1] + [os.path.splitext(components[-1])[0]])
def exec_module(self, module): def exec_module(self, module):
assert module.__name__ == self._module_name assert module.__name__ == self._module_name
code = None code = None
with _proto_code_cache_lock: with _proto_code_cache_lock:
if self._module_name in _proto_code_cache: if self._module_name in _proto_code_cache:
code = _proto_code_cache[self._module_name] code = _proto_code_cache[self._module_name]
six.exec_(code, module.__dict__) exec(code, module.__dict__)
else: else:
files = self._codegen_fn( files = self._codegen_fn(
self._protobuf_path.encode('ascii'), self._protobuf_path.encode('ascii'),
[path.encode('ascii') for path in sys.path]) [path.encode('ascii') for path in sys.path])
# NOTE: The files are returned in topological order of dependencies. Each # NOTE: The files are returned in topological order of dependencies. Each
# entry is guaranteed to depend only on the modules preceding it in the # entry is guaranteed to depend only on the modules preceding it in the
# list and the last entry is guaranteed to be our requested module. We # list and the last entry is guaranteed to be our requested module. We
# cache the code from the first invocation at module-scope so that we # cache the code from the first invocation at module-scope so that we
# don't have to regenerate code that has already been generated by protoc. # don't have to regenerate code that has already been generated by protoc.
for f in files[:-1]: for f in files[:-1]:
module_name = self._generated_file_to_module_name( module_name = self._generated_file_to_module_name(
f[0].decode('ascii')) f[0].decode('ascii'))
if module_name not in sys.modules: if module_name not in sys.modules:
if module_name not in _proto_code_cache: if module_name not in _proto_code_cache:
_proto_code_cache[module_name] = f[1] _proto_code_cache[module_name] = f[1]
importlib.import_module(module_name) importlib.import_module(module_name)
six.exec_(files[-1][1], module.__dict__) exec(files[-1][1], module.__dict__)
class ProtoFinder(importlib.abc.MetaPathFinder):
class ProtoFinder(importlib.abc.MetaPathFinder):
def __init__(self, suffix, codegen_fn):
def __init__(self, suffix, codegen_fn): self._suffix = suffix
self._suffix = suffix self._codegen_fn = codegen_fn
self._codegen_fn = codegen_fn
def find_spec(self, fullname, path, target=None):
def find_spec(self, fullname, path, target=None): filepath = _module_name_to_proto_file(self._suffix, fullname)
filepath = _module_name_to_proto_file(self._suffix, fullname) for search_path in sys.path:
for search_path in sys.path: try:
try: prospective_path = os.path.join(search_path, filepath)
prospective_path = os.path.join(search_path, filepath) os.stat(prospective_path)
os.stat(prospective_path) except (FileNotFoundError, NotADirectoryError):
except (FileNotFoundError, NotADirectoryError): continue
continue else:
else: return importlib.machinery.ModuleSpec(
return importlib.machinery.ModuleSpec( fullname,
fullname, ProtoLoader(self._suffix, self._codegen_fn, fullname,
ProtoLoader(self._suffix, self._codegen_fn, fullname, filepath, search_path))
filepath, search_path))
sys.meta_path.extend([
ProtoFinder(_PROTO_MODULE_SUFFIX, _protoc_compiler.get_protos),
sys.meta_path.extend([ ProtoFinder(_SERVICE_MODULE_SUFFIX, _protoc_compiler.get_services)
ProtoFinder(_PROTO_MODULE_SUFFIX, _protoc_compiler.get_protos), ])
ProtoFinder(_SERVICE_MODULE_SUFFIX, _protoc_compiler.get_services)
])
if __name__ == '__main__': if __name__ == '__main__':
proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') proto_include = pkg_resources.resource_filename('grpc_tools', '_proto')

@ -225,7 +225,6 @@ setuptools.setup(
packages=setuptools.find_packages('.'), packages=setuptools.find_packages('.'),
install_requires=[ install_requires=[
'protobuf>=3.5.0.post1', 'protobuf>=3.5.0.post1',
'six>=1.0.0',
'grpcio>={version}'.format(version=grpc_version.VERSION), 'grpcio>={version}'.format(version=grpc_version.VERSION),
], ],
package_data=package_data(), package_data=package_data(),

Loading…
Cancel
Save