Skip to content

Commit

Permalink
[Python Misc] Deprecate pkg_resources.resource_filename
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanWang-Amos committed Feb 5, 2024
1 parent 20e5e0c commit 6c45828
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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 = [
Expand Down
24 changes: 20 additions & 4 deletions tools/distrib/python/grpcio_tools/grpc_tools/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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 = [
Expand Down
23 changes: 21 additions & 2 deletions tools/distrib/python/xds_protos/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 6c45828

Please sign in to comment.