Use apt_pkg.version_compare if available

This fixes #24397

Signed-off-by: Mathieu Le Marec - Pasquet <kiorky@cryptelium.net>
This commit is contained in:
Mathieu Le Marec - Pasquet 2015-06-04 13:45:13 +02:00
parent 953725a563
commit e15cb936b5

View file

@ -49,6 +49,12 @@ try:
except ImportError:
HAS_APT = False
try:
import apt_pkg
HAS_APTPKG = True
except ImportError:
HAS_APTPKG = False
try:
import softwareproperties.ppa
HAS_SOFTWAREPROPERTIES = True
@ -1117,6 +1123,15 @@ def version_cmp(pkg1, pkg2):
salt '*' pkg.version_cmp '0.2.4-0ubuntu1' '0.2.4.1-0ubuntu1'
'''
# if we have apt_pkg, this will be quickier this way
# and also do not rely on shell.
if HAS_APTPKG:
try:
return apt_pkg.version_compare(pkg1, pkg2)
except (TypeError, ValueError):
# try to use shell version in case of errors via
# the python binding
pass
try:
for oper, ret in (('lt', -1), ('eq', 0), ('gt', 1)):
cmd = 'dpkg --compare-versions {0} {1} ' \