Skip to content

Commit

Permalink
Remove pkg_resources
Browse files Browse the repository at this point in the history
Replaces all uses of pkg_resources with importlib, because the former
has been deprecated.

fixes pulp#865
  • Loading branch information
mdellweg committed Jan 16, 2024
1 parent ab5f324 commit f2b3946
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .ci/scripts/collect_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import toml
from git import GitCommandError, Repo
from pkg_resources import parse_version
from packaging.version import parse as parse_version

# Read Towncrier settings
tc_settings = toml.load("pyproject.toml")["tool"]["towncrier"]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/collect_changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
git config user.email pulp-infra@redhat.com
- name: Collect changes
run: |
pip install GitPython toml
pip install GitPython packaging toml
python3 .ci/scripts/collect_changes.py
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
Expand Down
1 change: 1 addition & 0 deletions CHANGES/865.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove dependency on `pkg_resources` that failed some installations but is deprecated anyway.
18 changes: 9 additions & 9 deletions pulp-glue/pulp_glue/common/i18n.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import gettext
import sys
from functools import lru_cache

import pkg_resources
if sys.version_info >= (3, 9):
from importlib.resources import files
else:
from importlib_resources import files


# Need to call lru_cache() before using it as a decorator for python 3.7 compatibility
@lru_cache(maxsize=None)
def get_translation(name: str) -> gettext.NullTranslations:
"""
Return a translations object for a certain import path.
Expand All @@ -23,11 +29,5 @@ def get_translation(name: str) -> gettext.NullTranslations:
_ = translation.gettext
```
"""
localedir = pkg_resources.resource_filename(name, "locale")
return _get_translation_for_domain("messages", localedir)


# Need to call lru_cache() before using it as a decorator for python 3.7 compatibility
@lru_cache(maxsize=None)
def _get_translation_for_domain(domain: str, localedir: str) -> gettext.NullTranslations:
return gettext.translation(domain, localedir=localedir, fallback=True)
localedir = files(name) / "locale"
return gettext.translation("messages", localedir=str(localedir), fallback=True)
21 changes: 11 additions & 10 deletions pulp-glue/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ authors = [
{name = "Pulp Team", email = "pulp-list@redhat.com"},
]
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Topic :: System :: Software Distribution",
"Typing :: Typed",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Topic :: System :: Software Distribution",
"Typing :: Typed",
]
dependencies = [
"packaging>=20.0,<24",
"requests>=2.24.0,<2.32",
"packaging>=20.0,<24",
"requests>=2.24.0,<2.32",
"importlib_resources>=5.7,<6.2;python_version<'3.9'",
]

[project.urls]
Expand Down
9 changes: 7 additions & 2 deletions pulp_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import sys
from types import ModuleType
from typing import Any, Dict, Optional

import click
import pkg_resources

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
else:
from importlib_metadata import entry_points

__version__ = "0.23.0.dev"
_main: Optional[click.Group] = None
Expand All @@ -16,7 +21,7 @@ def load_plugins() -> click.Group:
# https://packaging.python.org/guides/creating-and-discovering-plugins/#using-package-metadata
discovered_plugins: Dict[str, ModuleType] = {
entry_point.name: entry_point.load()
for entry_point in pkg_resources.iter_entry_points("pulp_cli.plugins")
for entry_point in entry_points(group="pulp_cli.plugins")
}
_main = discovered_plugins["common"].main
assert isinstance(_main, click.Group)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
"PyYAML>=5.3,<6.1",
"schema>=0.7.5,<0.8",
"toml>=0.10.2,<0.11",
"importlib_metadata>=4.8,<7.1;python_version<'3.10'",
]

[project.optional-dependencies]
Expand Down

0 comments on commit f2b3946

Please sign in to comment.