1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-03-22 21:29:19 +00:00

[PR #11497/d9e48f04 backport][stable-10] [stable-11] python_requirements_info: use importlib.metadata when available (#11495) (#11498)

[stable-11] python_requirements_info: use importlib.metadata when available (#11495) (#11497)

python_requirements_info: use importlib.metadata when available (#11495)

Use importlib.metadata when available.

(cherry picked from commit 88adca3fb4)
(cherry picked from commit d9e48f0488)

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot] 2026-02-11 07:27:45 +01:00 committed by GitHub
parent bba060ef71
commit 4b4479844b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 13 deletions

View file

@ -123,15 +123,25 @@ import re
import sys
import operator
HAS_DISTUTILS = False
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
HAS_IMPORTLIB_METADATA = False
try:
import pkg_resources
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
HAS_DISTUTILS = True
import importlib.metadata
HAS_IMPORTLIB_METADATA = True
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
HAS_DISTUTILS = False
try:
import pkg_resources
HAS_DISTUTILS = True
except ImportError:
pass
operations = {
'<=': operator.le,
@ -157,9 +167,9 @@ def main():
),
supports_check_mode=True,
)
if not HAS_DISTUTILS:
if not HAS_DISTUTILS and not HAS_IMPORTLIB_METADATA:
module.fail_json(
msg='Could not import "distutils" and "pkg_resources" libraries to introspect python environment.',
msg='Could not import "pkg_resources" or "importlib.metadata" libraries to introspect Python environment.',
python=sys.executable,
python_version=sys.version,
python_version_info=python_version_info,
@ -180,12 +190,20 @@ def main():
pkg, op, version = match.groups()
if op is not None and op not in operations:
module.fail_json(msg="Failed to parse version requirement '{0}'. Operator must be one of >, <, <=, >=, or ==".format(dep))
try:
existing = pkg_resources.get_distribution(pkg).version
except pkg_resources.DistributionNotFound:
# not there
results['not_found'].append(pkg)
continue
if HAS_DISTUTILS:
try:
existing = pkg_resources.get_distribution(pkg).version
except pkg_resources.DistributionNotFound:
# not there
results['not_found'].append(pkg)
continue
else:
try:
existing = importlib.metadata.version(pkg)
except importlib.metadata.PackageNotFoundError:
# not there
results['not_found'].append(pkg)
continue
if op is None and version is None:
results['valid'][pkg] = {
'installed': existing,