mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-16 05:59:47 -07:00
Fix pip idempotence in check mode
PIP package names must be case insensitive, and must consider hyphens and underscores to be equivalent (https://www.python.org/dev/peps/pep-0426/#name), because of this the module didn't work correctly in check mode. For example if the passed package name had a different case or an underscore instead of a hyphen (or the other way around) compared to the installed package, check mode reported as changed, even though packages were installed. Now the module ignores case and hyphens/underscores in package names, so check mode works correctly.
This commit is contained in:
parent
96c2375692
commit
b89b688d52
3 changed files with 33 additions and 2 deletions
|
@ -350,10 +350,11 @@ def _is_present(module, req, installed_pkgs, pkg_command):
|
|||
for pkg in installed_pkgs:
|
||||
if '==' in pkg:
|
||||
pkg_name, pkg_version = pkg.split('==')
|
||||
pkg_name = Package.canonicalize_name(pkg_name)
|
||||
else:
|
||||
continue
|
||||
|
||||
if pkg_name.lower() == req.package_name and req.is_satisfied_by(pkg_version):
|
||||
if pkg_name == req.package_name and req.is_satisfied_by(pkg_version):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
@ -499,6 +500,8 @@ class Package:
|
|||
test whether a package is already satisfied.
|
||||
"""
|
||||
|
||||
_CANONICALIZE_RE = re.compile(r'[-_.]+')
|
||||
|
||||
def __init__(self, name_string, version_string=None):
|
||||
self._plain_package = False
|
||||
self.package_name = name_string
|
||||
|
@ -515,7 +518,7 @@ class Package:
|
|||
self.package_name = "setuptools"
|
||||
self._requirement.project_name = "setuptools"
|
||||
else:
|
||||
self.package_name = self._requirement.project_name
|
||||
self.package_name = Package.canonicalize_name(self._requirement.project_name)
|
||||
self._plain_package = True
|
||||
except ValueError as e:
|
||||
pass
|
||||
|
@ -539,6 +542,11 @@ class Package:
|
|||
for op, ver in self._requirement.specs
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def canonicalize_name(name):
|
||||
# This is taken from PEP 503.
|
||||
return Package._CANONICALIZE_RE.sub("-", name).lower()
|
||||
|
||||
def __str__(self):
|
||||
if self._plain_package:
|
||||
return to_native(self._requirement)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue