diff --git a/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py b/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py index 9b8c3acf94870..7730d5e93ab57 100644 --- a/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py +++ b/src/python/grpcio_tests/tests/protoc_plugin/_split_definitions_test.py @@ -26,7 +26,11 @@ import grpc from grpc_tools import protoc -import pkg_resources + +if sys.version_info >= (3, 9, 0): + from importlib import resources +else: + import pkg_resources from tests.unit import test_common @@ -62,6 +66,20 @@ def _create_directory_tree(root, path_components_sequence): thus_far = path.join(thus_far, path_component) +def _get_resource_file_name( + package_or_requirement: str, resource_name: str +) -> str: + """Obtain the filename for a resource on the file system.""" + if sys.version_info >= (3, 9, 0): + return ( + resources.files(package_or_requirement) / resource_name + ).resolve() + else: + return pkg_resources.resource_filename( + package_or_requirement, resource_name + ) + + def _massage_proto_content( proto_content, test_name_bytes, messages_proto_relative_file_name_bytes ): @@ -367,7 +385,7 @@ class WellKnownTypesTest(unittest.TestCase): def testWellKnownTypes(self): os.chdir(_TEST_DIR) out_dir = tempfile.mkdtemp(suffix="wkt_test", dir=".") - well_known_protos_include = pkg_resources.resource_filename( + well_known_protos_include = _get_resource_file_name( "grpc_tools", "_proto" ) args = [ diff --git a/tools/distrib/python/grpcio_tools/grpc_tools/command.py b/tools/distrib/python/grpcio_tools/grpc_tools/command.py index 5b0b7eb6a79fa..3086f193a6d06 100644 --- a/tools/distrib/python/grpcio_tools/grpc_tools/command.py +++ b/tools/distrib/python/grpcio_tools/grpc_tools/command.py @@ -16,9 +16,27 @@ import sys from grpc_tools import protoc -import pkg_resources import setuptools +if sys.version_info >= (3, 9, 0): + from importlib import resources +else: + import pkg_resources + + +def _get_resource_file_name( + package_or_requirement: str, resource_name: str +) -> str: + """Obtain the filename for a resource on the file system.""" + if sys.version_info >= (3, 9, 0): + return ( + resources.files(package_or_requirement) / resource_name + ).resolve() + else: + return pkg_resources.resource_filename( + package_or_requirement, resource_name + ) + def build_package_protos(package_root, strict_mode=False): proto_files = [] @@ -30,9 +48,7 @@ def build_package_protos(package_root, strict_mode=False): os.path.abspath(os.path.join(root, filename)) ) - well_known_protos_include = pkg_resources.resource_filename( - "grpc_tools", "_proto" - ) + well_known_protos_include = _get_resource_file_name("grpc_tools", "_proto") for proto_file in proto_files: command = [ diff --git a/tools/distrib/python/xds_protos/build.py b/tools/distrib/python/xds_protos/build.py index cb5cc9bb1b738..7ae5b7af2d276 100644 --- a/tools/distrib/python/xds_protos/build.py +++ b/tools/distrib/python/xds_protos/build.py @@ -15,15 +15,34 @@ """Builds the content of xds-protos package""" import os +import sys from grpc_tools import protoc -import pkg_resources + +if sys.version_info >= (3, 9, 0): + from importlib import resources +else: + import pkg_resources def localize_path(p): return os.path.join(*p.split("/")) +def _get_resource_file_name( + package_or_requirement: str, resource_name: str +) -> str: + """Obtain the filename for a resource on the file system.""" + if sys.version_info >= (3, 9, 0): + return ( + resources.files(package_or_requirement) / resource_name + ).resolve() + else: + return pkg_resources.resource_filename( + package_or_requirement, resource_name + ) + + # We might not want to compile all the protos EXCLUDE_PROTO_PACKAGES_LIST = tuple( localize_path(p) @@ -46,7 +65,7 @@ def localize_path(p): GRPC_ROOT, "third_party", "opencensus-proto", "src" ) OPENTELEMETRY_PROTO_ROOT = os.path.join(GRPC_ROOT, "third_party", "opentelemetry") -WELL_KNOWN_PROTOS_INCLUDE = pkg_resources.resource_filename("grpc_tools", "_proto") +WELL_KNOWN_PROTOS_INCLUDE = _get_resource_file_name("grpc_tools", "_proto") OUTPUT_PATH = WORK_DIR