Skip to content

Commit

Permalink
Remove pkg_resources use (#1224) (#1230)
Browse files Browse the repository at this point in the history
* Remove pkg_resources use
* pkg_resources is deprecated in favor of importlib.metadata
* expand < 3.10 importlib-metadata polyfill version constraints

Fixes: #1223

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>
(cherry picked from commit 3d6f66b)

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
nitzmahone and Akasurde authored Apr 8, 2023
1 parent ec560a7 commit 0393c39
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions ansible_runner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pkg_resources

from .utils.importlib_compat import importlib_metadata
from .interface import run, run_async, \
run_command, run_command_async, \
get_plugin_docs, get_plugin_docs_async, get_plugin_list, \
Expand All @@ -13,5 +12,5 @@
plugins = {
entry_point.name: entry_point.load()
for entry_point
in pkg_resources.iter_entry_points('ansible_runner.plugins')
in importlib_metadata.entry_points(group='ansible_runner.plugins')
}
4 changes: 2 additions & 2 deletions ansible_runner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
#
import ast
import pkg_resources
import threading
import traceback
import argparse
Expand All @@ -42,10 +41,11 @@
from ansible_runner import cleanup
from ansible_runner.utils import dump_artifact, Bunch, register_for_cleanup
from ansible_runner.utils.capacity import get_cpu_count, get_mem_in_bytes, ensure_uuid
from ansible_runner.utils.importlib_compat import importlib_metadata
from ansible_runner.runner import Runner
from ansible_runner.exceptions import AnsibleRunnerException

VERSION = pkg_resources.require("ansible_runner")[0].version
VERSION = importlib_metadata.version("ansible_runner")

DEFAULT_ROLES_PATH = os.getenv('ANSIBLE_ROLES_PATH', None)
DEFAULT_RUNNER_BINARY = os.getenv('RUNNER_BINARY', None)
Expand Down
6 changes: 6 additions & 0 deletions ansible_runner/utils/importlib_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

if sys.version_info < (3, 10):
import importlib_metadata # noqa: F401
else:
import importlib.metadata as importlib_metadata # noqa: F401
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ packaging
python-daemon
pyyaml
six
importlib-metadata >= 4.6, < 6.3; python_version < '3.10' # enable `groups` arg to entry_points missing in 3.9 stdlib importlib.metadata
19 changes: 5 additions & 14 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from pathlib import Path
from packaging.version import Version
import subprocess

from ansible_runner import defaults
from ansible_runner.utils.importlib_compat import importlib_metadata

import pkg_resources
import pytest


Expand Down Expand Up @@ -35,9 +34,9 @@ def is_pre_ansible211():
"""

try:
if pkg_resources.get_distribution('ansible-core').version:
if importlib_metadata.version("ansible-core"):
return False
except pkg_resources.DistributionNotFound:
except importlib_metadata.PackageNotFoundError:
# Must be ansible-base or ansible
return True

Expand All @@ -51,18 +50,10 @@ def skipif_pre_ansible211(is_pre_ansible211):
@pytest.fixture(scope="session")
def is_pre_ansible212():
try:
base_version = (
subprocess.run(
"python -c 'import ansible; print(ansible.__version__)'",
capture_output=True,
shell=True,
)
.stdout.strip()
.decode()
)
base_version = importlib_metadata.version("ansible")
if Version(base_version) < Version("2.12"):
return True
except pkg_resources.DistributionNotFound:
except importlib_metadata.PackageNotFoundError:
pass


Expand Down

0 comments on commit 0393c39

Please sign in to comment.