Skip to content

Commit

Permalink
[Python Modernization] Deprecate pkg_resources (grpc#35849)
Browse files Browse the repository at this point in the history
Discuss thread about this change: [link](https://mail.google.com/mail/u/0/#sent/QgrcJHsBpWNGRlrMktwbppGGfFTVCFLcQgL?compose=new)
<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes grpc#35849

PiperOrigin-RevId: 607144827
  • Loading branch information
XuanWang-Amos authored and copybara-github committed Feb 15, 2024
1 parent 2ca5686 commit bb3edd2
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 111 deletions.
17 changes: 0 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import sysconfig

import _metadata
import pkg_resources
from setuptools import Extension
from setuptools.command import egg_info

Expand Down Expand Up @@ -454,22 +453,6 @@ def _quote_build_define(argument):
DEFINE_MACROS += (("PyMODINIT_FUNC", pymodinit),)
DEFINE_MACROS += (("GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK", 1),)

# By default, Python3 distutils enforces compatibility of
# c plugins (.so files) with the OSX version Python was built with.
# We need OSX 10.10, the oldest which supports C++ thread_local.
# Python 3.9: Mac OS Big Sur sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') returns int (11)
if "darwin" in sys.platform:
mac_target = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET")
if mac_target:
mac_target = pkg_resources.parse_version(str(mac_target))
if mac_target < pkg_resources.parse_version("10.10.0"):
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.10"
os.environ["_PYTHON_HOST_PLATFORM"] = re.sub(
r"macosx-[0-9]+\.[0-9]+-(.+)",
r"macosx-10.10-\1",
sysconfig.get_platform(),
)


def cython_extensions_and_necessity():
cython_module_files = [
Expand Down
26 changes: 0 additions & 26 deletions src/python/grpcio/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,6 @@ Troubleshooting

Help, I ...

* **... see a** :code:`pkg_resources.VersionConflict` **when I try to install
grpc**

This is likely because :code:`pip` doesn't own the offending dependency,
which in turn is likely because your operating system's package manager owns
it. You'll need to force the installation of the dependency:

:code:`pip install --ignore-installed $OFFENDING_DEPENDENCY`

For example, if you get an error like the following:

::

Traceback (most recent call last):
File "<string>", line 17, in <module>
...
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 509, in find
raise VersionConflict(dist, req)
pkg_resources.VersionConflict: (six 1.8.0 (/usr/lib/python2.7/dist-packages), Requirement.parse('six>=1.10'))

You can fix it by doing:

::

sudo pip install --ignore-installed six

* **... see the following error on some platforms**

::
Expand Down
17 changes: 0 additions & 17 deletions src/python/grpcio_observability/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import sys
import sysconfig

import pkg_resources
import setuptools
from setuptools import Extension
from setuptools.command import build_ext
Expand Down Expand Up @@ -206,22 +205,6 @@ def get_ext_filename(self, ext_name):
pymodinit = 'extern "C" __attribute__((visibility ("default"))) PyObject*'
DEFINE_MACROS += (("PyMODINIT_FUNC", pymodinit),)

# By default, Python3 distutils enforces compatibility of
# c plugins (.so files) with the OSX version Python was built with.
# We need OSX 10.10, the oldest which supports C++ thread_local.
if "darwin" in sys.platform:
mac_target = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET")
if mac_target and (
pkg_resources.parse_version(mac_target)
< pkg_resources.parse_version("10.10.0")
):
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.10"
os.environ["_PYTHON_HOST_PLATFORM"] = re.sub(
r"macosx-[0-9]+\.[0-9]+-(.+)",
r"macosx-10.10-\1",
sysconfig.get_platform(),
)


def extension_modules():
if BUILD_WITH_CYTHON:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@

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 All @@ -48,6 +53,22 @@ def _system_path(path_insertion):
sys.path = old_system_path


def _get_resource_file_name(
package_or_requirement: str, resource_name: str
) -> str:
"""Obtain the filename for a resource on the file system."""
file_name = None
if sys.version_info >= (3, 9, 0):
file_name = (
resources.files(package_or_requirement) / resource_name
).resolve()
else:
file_name = pkg_resources.resource_filename(
package_or_requirement, resource_name
)
return str(file_name)


# NOTE(nathaniel): https://twitter.com/exoplaneteer/status/677259364256747520
# Life lesson "just always default to idempotence" reinforced.
def _create_directory_tree(root, path_components_sequence):
Expand Down Expand Up @@ -367,7 +388,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
26 changes: 0 additions & 26 deletions tools/distrib/python/grpcio_tools/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,6 @@ Troubleshooting

Help, I ...

* **... see a** :code:`pkg_resources.VersionConflict` **when I try to install
grpc**

This is likely because :code:`pip` doesn't own the offending dependency,
which in turn is likely because your operating system's package manager owns
it. You'll need to force the installation of the dependency:

:code:`pip install --ignore-installed $OFFENDING_DEPENDENCY`

For example, if you get an error like the following:

::

Traceback (most recent call last):
File "<string>", line 17, in <module>
...
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 509, in find
raise VersionConflict(dist, req)
pkg_resources.VersionConflict: (six 1.8.0 (/usr/lib/python2.7/dist-packages), Requirement.parse('six>=1.10'))

You can fix it by doing:

::

sudo pip install --ignore-installed six

* **... see compiler errors on some platforms when either installing from source or from the source distribution**

If you see
Expand Down
26 changes: 22 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,29 @@
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."""
file_name = None
if sys.version_info >= (3, 9, 0):
file_name = (
resources.files(package_or_requirement) / resource_name
).resolve()
else:
file_name = pkg_resources.resource_filename(
package_or_requirement, resource_name
)
return str(file_name)


def build_package_protos(package_root, strict_mode=False):
proto_files = []
Expand All @@ -30,9 +50,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
17 changes: 0 additions & 17 deletions tools/distrib/python/grpcio_tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import sys
import sysconfig

import pkg_resources
import setuptools
from setuptools import Extension
from setuptools.command import build_ext
Expand Down Expand Up @@ -214,22 +213,6 @@ def get_ext_filename(self, ext_name):
elif "linux" in sys.platform or "darwin" in sys.platform:
DEFINE_MACROS += (("HAVE_PTHREAD", 1),)

# By default, Python3 setuptools(distutils) enforces compatibility of
# c plugins (.so files) with the OSX version Python was built with.
# We need OSX 10.10, the oldest which supports C++ thread_local.
if "darwin" in sys.platform:
mac_target = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET")
if mac_target and (
pkg_resources.parse_version(mac_target)
< pkg_resources.parse_version("10.10.0")
):
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.10"
os.environ["_PYTHON_HOST_PLATFORM"] = re.sub(
r"macosx-[0-9]+\.[0-9]+-(.+)",
r"macosx-10.10-\1",
sysconfig.get_platform(),
)


def package_data():
tools_path = GRPC_PYTHON_TOOLS_PACKAGE.replace(".", os.path.sep)
Expand Down
25 changes: 23 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,36 @@
"""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."""
file_name = None
if sys.version_info >= (3, 9, 0):
file_name = (
resources.files(package_or_requirement) / resource_name
).resolve()
else:
file_name = pkg_resources.resource_filename(
package_or_requirement, resource_name
)
return str(file_name)


# We might not want to compile all the protos
EXCLUDE_PROTO_PACKAGES_LIST = tuple(
localize_path(p)
Expand All @@ -46,7 +67,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 bb3edd2

Please sign in to comment.