mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Accept ignore_epoch argument for salt.utils.compare_versions()
This commit is contained in:
parent
91e095bb41
commit
07368fac40
1 changed files with 13 additions and 6 deletions
|
@ -2279,7 +2279,7 @@ def kwargs_warn_until(kwargs,
|
|||
)
|
||||
|
||||
|
||||
def version_cmp(pkg1, pkg2):
|
||||
def version_cmp(pkg1, pkg2, ignore_epoch=False):
|
||||
'''
|
||||
Compares two version strings using distutils.version.LooseVersion. This is
|
||||
a fallback for providers which don't have a version comparison utility
|
||||
|
@ -2287,6 +2287,10 @@ def version_cmp(pkg1, pkg2):
|
|||
version2, and 1 if version1 > version2. Return None if there was a problem
|
||||
making the comparison.
|
||||
'''
|
||||
normalize = lambda x: str(x).split(':', 1)[-1] if ignore_epoch else str(x)
|
||||
pkg1 = normalize(pkg1)
|
||||
pkg2 = normalize(pkg2)
|
||||
|
||||
try:
|
||||
# pylint: disable=no-member
|
||||
if distutils.version.LooseVersion(pkg1) < \
|
||||
|
@ -2303,22 +2307,25 @@ def version_cmp(pkg1, pkg2):
|
|||
return None
|
||||
|
||||
|
||||
def compare_versions(ver1='', oper='==', ver2='', cmp_func=None):
|
||||
def compare_versions(ver1='',
|
||||
oper='==',
|
||||
ver2='',
|
||||
cmp_func=None,
|
||||
ignore_epoch=False):
|
||||
'''
|
||||
Compares two version numbers. Accepts a custom function to perform the
|
||||
cmp-style version comparison, otherwise uses version_cmp().
|
||||
'''
|
||||
cmp_map = {'<': (-1,), '<=': (-1, 0), '==': (0,),
|
||||
'>=': (0, 1), '>': (1,)}
|
||||
if oper not in ['!='] and oper not in cmp_map:
|
||||
log.error('Invalid operator "{0}" for version '
|
||||
'comparison'.format(oper))
|
||||
if oper not in ('!=',) and oper not in cmp_map:
|
||||
log.error('Invalid operator \'%s\' for version comparison', oper)
|
||||
return False
|
||||
|
||||
if cmp_func is None:
|
||||
cmp_func = version_cmp
|
||||
|
||||
cmp_result = cmp_func(ver1, ver2)
|
||||
cmp_result = cmp_func(ver1, ver2, ignore_epoch=ignore_epoch)
|
||||
if cmp_result is None:
|
||||
return False
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue