Remove option to specify path

pull/21458/head
Richard Belleville 5 years ago
parent c2c5057e9d
commit 9448150eec
  1. 18
      src/python/grpcio/grpc/_grpcio_metadata.py
  2. 46
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py
  3. 72
      tools/distrib/python/grpcio_tools/grpc_tools/test/protoc_test.py

@ -1,17 +1 @@
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# AUTO-GENERATED FROM `$REPO_ROOT/templates/src/python/grpcio/grpc/_grpcio_metadata.py.template`!!!
__version__ = """1.31.0.dev0"""
__version__ = """1.31.0.dev0"""

@ -52,36 +52,22 @@ if sys.version_info[0] > 2:
proto_base_name = os.path.splitext(components[-1])[0]
return ".".join(components[:-1] + [proto_base_name + suffix])
@contextlib.contextmanager
def _augmented_syspath(new_paths):
original_sys_path = sys.path
if new_paths is not None:
sys.path = sys.path + new_paths
try:
yield
finally:
sys.path = original_sys_path
# TODO: Investigate making this even more of a no-op in the case that we have
# truly already imported the module.
def _protos(protobuf_path, include_paths=None):
with _augmented_syspath(include_paths):
module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX,
protobuf_path)
module = importlib.import_module(module_name)
return module
def _services(protobuf_path, include_paths=None):
_protos(protobuf_path, include_paths)
with _augmented_syspath(include_paths):
module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX,
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),
_services(protobuf_path, include_paths=include_paths))
def _protos(protobuf_path):
module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX,
protobuf_path)
module = importlib.import_module(module_name)
return module
def _services(protobuf_path):
_protos(protobuf_path)
module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX,
protobuf_path)
module = importlib.import_module(module_name)
return module
def _protos_and_services(protobuf_path):
return (_protos(protobuf_path),
_services(protobuf_path))
_proto_code_cache = {}
_proto_code_cache_lock = threading.RLock()

@ -4,10 +4,11 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import unittest
import multiprocessing
import contextlib
import functools
import multiprocessing
import sys
import unittest
def _wrap_in_subprocess(error_queue, fn):
@ -35,27 +36,30 @@ def _run_in_subprocess(test_case):
proc.exitcode)
def _test_import_protos():
from grpc_tools import protoc
proto_path = "tools/distrib/python/grpcio_tools/"
protos = protoc._protos("grpc_tools/test/simple.proto", [proto_path])
assert protos.SimpleMessage is not None
@contextlib.contextmanager
def _augmented_syspath(new_paths):
original_sys_path = sys.path
if new_paths is not None:
sys.path = sys.path + list(new_paths)
try:
yield
finally:
sys.path = original_sys_path
def _test_import_services():
def _test_import_protos():
from grpc_tools import protoc
proto_path = "tools/distrib/python/grpcio_tools/"
protos = protoc._protos("grpc_tools/test/simple.proto", [proto_path])
services = protoc._services("grpc_tools/test/simple.proto", [proto_path])
assert services.SimpleMessageServiceStub is not None
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)):
protos = protoc._protos("grpc_tools/test/simple.proto")
assert protos.SimpleMessage is not None
# NOTE: In this case, we use sys.path to determine where to look for our protos.
def _test_import_implicit_include():
def _test_import_services():
from grpc_tools import protoc
protos = protoc._protos("grpc_tools/test/simple.proto")
services = protoc._services("grpc_tools/test/simple.proto")
assert services.SimpleMessageServiceStub is not None
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)):
protos = protoc._protos("grpc_tools/test/simple.proto")
services = protoc._services("grpc_tools/test/simple.proto")
assert services.SimpleMessageServiceStub is not None
def _test_import_services_without_protos():
@ -66,26 +70,25 @@ def _test_import_services_without_protos():
def _test_proto_module_imported_once():
from grpc_tools import protoc
proto_path = "tools/distrib/python/grpcio_tools/"
protos = protoc._protos("grpc_tools/test/simple.proto", [proto_path])
services = protoc._services("grpc_tools/test/simple.proto", [proto_path])
complicated_protos = protoc._protos("grpc_tools/test/complicated.proto",
[proto_path])
simple_message = protos.SimpleMessage()
complicated_message = complicated_protos.ComplicatedMessage()
assert (simple_message.simpler_message.simplest_message.__class__ is
complicated_message.simplest_message.__class__)
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)):
protos = protoc._protos("grpc_tools/test/simple.proto")
services = protoc._services("grpc_tools/test/simple.proto")
complicated_protos = protoc._protos("grpc_tools/test/complicated.proto")
simple_message = protos.SimpleMessage()
complicated_message = complicated_protos.ComplicatedMessage()
assert (simple_message.simpler_message.simplest_message.__class__ is
complicated_message.simplest_message.__class__)
def _test_static_dynamic_combo():
from grpc_tools.test import complicated_pb2
from grpc_tools import protoc
proto_path = "tools/distrib/python/grpcio_tools/"
protos = protoc._protos("grpc_tools/test/simple.proto", [proto_path])
static_message = complicated_pb2.ComplicatedMessage()
dynamic_message = protos.SimpleMessage()
assert (dynamic_message.simpler_message.simplest_message.__class__ is
static_message.simplest_message.__class__)
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)):
protos = protoc._protos("grpc_tools/test/simple.proto")
static_message = complicated_pb2.ComplicatedMessage()
dynamic_message = protos.SimpleMessage()
assert (dynamic_message.simpler_message.simplest_message.__class__ is
static_message.simplest_message.__class__)
def _test_combined_import():
@ -117,9 +120,6 @@ class ProtocTest(unittest.TestCase):
def test_import_services(self):
_run_in_subprocess(_test_import_services)
def test_import_implicit_include_path(self):
_run_in_subprocess(_test_import_implicit_include)
def test_import_services_without_protos(self):
_run_in_subprocess(_test_import_services_without_protos)

Loading…
Cancel
Save