mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-22 05:09:12 +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 commit88adca3fb4) (cherry picked from commitd9e48f0488) Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
bba060ef71
commit
4b4479844b
2 changed files with 35 additions and 13 deletions
4
changelogs/fragments/11492-python_requires_info.yml
Normal file
4
changelogs/fragments/11492-python_requires_info.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
bugfixes:
|
||||
- "python_requirements_info - use ``importlib.metadata`` if ``pkg_resources`` from ``setuptools`` cannot be imported.
|
||||
That module has been removed from setuptools 82.0.0
|
||||
(https://github.com/ansible-collections/community.general/issues/11491, https://github.com/ansible-collections/community.general/pull/11492)."
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue