Fix version comparison

Use RPM library to compare vendor versions of IPA for redhat platform

https://fedorahosted.org/freeipa/ticket/5535

Reviewed-By: Tomas Babej <tbabej@redhat.com>
This commit is contained in:
Martin Basti 2015-12-09 18:53:35 +01:00
parent e622da3e1a
commit 91913c5ba7
2 changed files with 54 additions and 0 deletions

View File

@ -214,6 +214,7 @@ Requires: python-pyasn1
Requires: dbus-python Requires: dbus-python
Requires: python-dns >= 1.11.1 Requires: python-dns >= 1.11.1
Requires: python-kdcproxy >= 0.3 Requires: python-kdcproxy >= 0.3
Requires: rpm-python
%description -n python2-ipaserver %description -n python2-ipaserver
IPA is an integrated solution to provide centrally managed Identity (users, IPA is an integrated solution to provide centrally managed Identity (users,

View File

@ -30,11 +30,13 @@ import stat
import socket import socket
import sys import sys
import base64 import base64
from functools import total_ordering
from subprocess import CalledProcessError from subprocess import CalledProcessError
from nss.error import NSPRError from nss.error import NSPRError
from pyasn1.error import PyAsn1Error from pyasn1.error import PyAsn1Error
from six.moves import urllib from six.moves import urllib
import rpm
from ipapython.ipa_log_manager import root_logger, log_mgr from ipapython.ipa_log_manager import root_logger, log_mgr
from ipapython import ipautil from ipapython import ipautil
@ -47,6 +49,35 @@ from ipaplatform.redhat.authconfig import RedHatAuthConfig
from ipaplatform.base.tasks import BaseTaskNamespace from ipaplatform.base.tasks import BaseTaskNamespace
# copied from rpmUtils/miscutils.py
def stringToVersion(verstring):
if verstring in [None, '']:
return (None, None, None)
i = verstring.find(':')
if i != -1:
try:
epoch = str(long(verstring[:i]))
except ValueError:
# look, garbage in the epoch field, how fun, kill it
epoch = '0' # this is our fallback, deal
else:
epoch = '0'
j = verstring.find('-')
if j != -1:
if verstring[i + 1:j] == '':
version = None
else:
version = verstring[i + 1:j]
release = verstring[j + 1:]
else:
if verstring[i + 1:] == '':
version = None
else:
version = verstring[i + 1:]
release = None
return (epoch, version, release)
log = log_mgr.get_logger(__name__) log = log_mgr.get_logger(__name__)
@ -66,6 +97,21 @@ def selinux_enabled():
return False return False
@total_ordering
class IPAVersion(object):
def __init__(self, version):
self.version_tuple = stringToVersion(version)
def __eq__(self, other):
assert isinstance(other, IPAVersion)
return rpm.labelCompare(self.version_tuple, other.version_tuple) == 0
def __lt__(self, other):
assert isinstance(other, IPAVersion)
return rpm.labelCompare(self.version_tuple, other.version_tuple) == -1
class RedHatTaskNamespace(BaseTaskNamespace): class RedHatTaskNamespace(BaseTaskNamespace):
def restore_context(self, filepath, restorecon=paths.SBIN_RESTORECON): def restore_context(self, filepath, restorecon=paths.SBIN_RESTORECON):
@ -426,5 +472,12 @@ class RedHatTaskNamespace(BaseTaskNamespace):
super(RedHatTaskNamespace, self).create_system_user(name, group, super(RedHatTaskNamespace, self).create_system_user(name, group,
homedir, shell, uid, gid, comment, create_homedir) homedir, shell, uid, gid, comment, create_homedir)
def parse_ipa_version(self, version):
"""
:param version: textual version
:return: object implementing proper __cmp__ method for version compare
"""
return IPAVersion(version)
tasks = RedHatTaskNamespace() tasks = RedHatTaskNamespace()