[Python Deps] Deprecate pkg_resources in grpc_tools (#35382)

…/protoc.py

pkg_resources deprecated in grpc_tools/protoc.py

.venv/lib/python3.11/site-packages/grpc_tools/protoc.py:21: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources

Fix for [https://github.com/grpc/grpc/issues/33570](https://github.com/grpc/grpc/issues/33570)

Closes #35382

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35382 from fahadahammed:fix-33570 27429f7a8c
PiperOrigin-RevId: 603189291
pull/35774/head
Fahad Ahammed 10 months ago committed by Copybara-Service
parent 03375e064f
commit eae9c558cf
  1. 2
      src/python/grpcio_tests/tests/unit/_dynamic_stubs_test.py
  2. 29
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py

@ -29,7 +29,7 @@ _DATA_DIR = os.path.join("tests", "unit", "data")
@contextlib.contextmanager
def _grpc_tools_unimportable():
original_sys_path = sys.path
sys.path = [path for path in sys.path if "grpcio_tools" not in path]
sys.path = [path for path in sys.path if "grpcio_tools" not in str(path)]
try:
import grpc_tools
except ImportError:

@ -18,7 +18,11 @@ import os
import sys
from grpc_tools import _protoc_compiler
import pkg_resources
if sys.version_info >= (3, 9, 0):
from importlib import resources
else:
import pkg_resources
_PROTO_MODULE_SUFFIX = "_pb2"
_SERVICE_MODULE_SUFFIX = "_pb2_grpc"
@ -37,6 +41,20 @@ def main(command_arguments):
return _protoc_compiler.run_main(command_arguments)
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
)
# NOTE(rbellevi): importlib.abc is not supported on 3.4.
if sys.version_info >= (3, 5, 0):
import contextlib
@ -63,9 +81,10 @@ if sys.version_info >= (3, 5, 0):
),
]
)
sys.path.append(
pkg_resources.resource_filename("grpc_tools", "_proto")
)
proto_include = _get_resource_file_name("grpc_tools", "_proto")
sys.path.append(proto_include)
_FINDERS_INSTALLED = True
def _module_name_to_proto_file(suffix, module_name):
@ -185,5 +204,5 @@ if sys.version_info >= (3, 5, 0):
_maybe_install_proto_finders()
if __name__ == "__main__":
proto_include = pkg_resources.resource_filename("grpc_tools", "_proto")
proto_include = _get_resource_file_name("grpc_tools", "_proto")
sys.exit(main(sys.argv + ["-I{}".format(proto_include)]))

Loading…
Cancel
Save