Blob Blame History Raw
From 30902db32fcc04dccbaf40839f44cdf4f505f588 Mon Sep 17 00:00:00 2001
From: Martin Basti <mbasti@redhat.com>
Date: Wed, 9 Dec 2015 18:53:35 +0100
Subject: [PATCH] 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>
---
 freeipa.spec.in             |  1 +
 ipaplatform/redhat/tasks.py | 53 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/freeipa.spec.in b/freeipa.spec.in
index 6527109b422a1e3065d5a540c3e2a3af670f2ebf..01d42bc621c83541af7517d6d91eb37fd5b5c5cc 100644
--- a/freeipa.spec.in
+++ b/freeipa.spec.in
@@ -159,6 +159,7 @@ Requires: p11-kit
 Requires: systemd-python
 Requires: %{etc_systemd_dir}
 Requires: gzip
+Requires: rpm-python
 
 Conflicts: %{alt_name}-server
 Obsoletes: %{alt_name}-server < %{version}
diff --git a/ipaplatform/redhat/tasks.py b/ipaplatform/redhat/tasks.py
index db31cd04cb234fac8fee97f3579ba2ca919f3262..2e894d776dcd5542e6c11cc0210add8ad9d90298 100644
--- a/ipaplatform/redhat/tasks.py
+++ b/ipaplatform/redhat/tasks.py
@@ -30,6 +30,8 @@ import socket
 import sys
 import urllib
 import base64
+import rpm
+from functools import total_ordering
 
 from subprocess import CalledProcessError
 from nss.error import NSPRError
@@ -46,6 +48,35 @@ from ipaplatform.redhat.authconfig import RedHatAuthConfig
 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__)
 
 
@@ -65,6 +96,21 @@ def selinux_enabled():
         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):
 
     def restore_context(self, filepath, restorecon=paths.SBIN_RESTORECON):
@@ -422,5 +468,12 @@ class RedHatTaskNamespace(BaseTaskNamespace):
         super(RedHatTaskNamespace, self).create_system_user(name, group,
             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()
-- 
2.4.3