e0ab38
From 30902db32fcc04dccbaf40839f44cdf4f505f588 Mon Sep 17 00:00:00 2001
e0ab38
From: Martin Basti <mbasti@redhat.com>
e0ab38
Date: Wed, 9 Dec 2015 18:53:35 +0100
e0ab38
Subject: [PATCH] Fix version comparison
e0ab38
e0ab38
Use RPM library to compare vendor versions of IPA for redhat platform
e0ab38
e0ab38
https://fedorahosted.org/freeipa/ticket/5535
e0ab38
e0ab38
Reviewed-By: Tomas Babej <tbabej@redhat.com>
e0ab38
---
e0ab38
 freeipa.spec.in             |  1 +
e0ab38
 ipaplatform/redhat/tasks.py | 53 +++++++++++++++++++++++++++++++++++++++++++++
e0ab38
 2 files changed, 54 insertions(+)
e0ab38
e0ab38
diff --git a/freeipa.spec.in b/freeipa.spec.in
e0ab38
index 6527109b422a1e3065d5a540c3e2a3af670f2ebf..01d42bc621c83541af7517d6d91eb37fd5b5c5cc 100644
e0ab38
--- a/freeipa.spec.in
e0ab38
+++ b/freeipa.spec.in
e0ab38
@@ -159,6 +159,7 @@ Requires: p11-kit
e0ab38
 Requires: systemd-python
e0ab38
 Requires: %{etc_systemd_dir}
e0ab38
 Requires: gzip
e0ab38
+Requires: rpm-python
e0ab38
 
e0ab38
 Conflicts: %{alt_name}-server
e0ab38
 Obsoletes: %{alt_name}-server < %{version}
e0ab38
diff --git a/ipaplatform/redhat/tasks.py b/ipaplatform/redhat/tasks.py
e0ab38
index db31cd04cb234fac8fee97f3579ba2ca919f3262..2e894d776dcd5542e6c11cc0210add8ad9d90298 100644
e0ab38
--- a/ipaplatform/redhat/tasks.py
e0ab38
+++ b/ipaplatform/redhat/tasks.py
e0ab38
@@ -30,6 +30,8 @@ import socket
e0ab38
 import sys
e0ab38
 import urllib
e0ab38
 import base64
e0ab38
+import rpm
e0ab38
+from functools import total_ordering
e0ab38
 
e0ab38
 from subprocess import CalledProcessError
e0ab38
 from nss.error import NSPRError
e0ab38
@@ -46,6 +48,35 @@ from ipaplatform.redhat.authconfig import RedHatAuthConfig
e0ab38
 from ipaplatform.base.tasks import BaseTaskNamespace
e0ab38
 
e0ab38
 
e0ab38
+# copied from rpmUtils/miscutils.py
e0ab38
+def stringToVersion(verstring):
e0ab38
+    if verstring in [None, '']:
e0ab38
+        return (None, None, None)
e0ab38
+    i = verstring.find(':')
e0ab38
+    if i != -1:
e0ab38
+        try:
e0ab38
+            epoch = str(long(verstring[:i]))
e0ab38
+        except ValueError:
e0ab38
+            # look, garbage in the epoch field, how fun, kill it
e0ab38
+            epoch = '0' # this is our fallback, deal
e0ab38
+    else:
e0ab38
+        epoch = '0'
e0ab38
+    j = verstring.find('-')
e0ab38
+    if j != -1:
e0ab38
+        if verstring[i + 1:j] == '':
e0ab38
+            version = None
e0ab38
+        else:
e0ab38
+            version = verstring[i + 1:j]
e0ab38
+        release = verstring[j + 1:]
e0ab38
+    else:
e0ab38
+        if verstring[i + 1:] == '':
e0ab38
+            version = None
e0ab38
+        else:
e0ab38
+            version = verstring[i + 1:]
e0ab38
+        release = None
e0ab38
+    return (epoch, version, release)
e0ab38
+
e0ab38
+
e0ab38
 log = log_mgr.get_logger(__name__)
e0ab38
 
e0ab38
 
e0ab38
@@ -65,6 +96,21 @@ def selinux_enabled():
e0ab38
         return False
e0ab38
 
e0ab38
 
e0ab38
+@total_ordering
e0ab38
+class IPAVersion(object):
e0ab38
+
e0ab38
+    def __init__(self, version):
e0ab38
+        self.version_tuple = stringToVersion(version)
e0ab38
+
e0ab38
+    def __eq__(self, other):
e0ab38
+        assert isinstance(other, IPAVersion)
e0ab38
+        return rpm.labelCompare(self.version_tuple, other.version_tuple) == 0
e0ab38
+
e0ab38
+    def __lt__(self, other):
e0ab38
+        assert isinstance(other, IPAVersion)
e0ab38
+        return rpm.labelCompare(self.version_tuple, other.version_tuple) == -1
e0ab38
+
e0ab38
+
e0ab38
 class RedHatTaskNamespace(BaseTaskNamespace):
e0ab38
 
e0ab38
     def restore_context(self, filepath, restorecon=paths.SBIN_RESTORECON):
e0ab38
@@ -422,5 +468,12 @@ class RedHatTaskNamespace(BaseTaskNamespace):
e0ab38
         super(RedHatTaskNamespace, self).create_system_user(name, group,
e0ab38
             homedir, shell, uid, gid, comment, create_homedir)
e0ab38
 
e0ab38
+    def parse_ipa_version(self, version):
e0ab38
+        """
e0ab38
+        :param version: textual version
e0ab38
+        :return: object implementing proper __cmp__ method for version compare
e0ab38
+        """
e0ab38
+        return IPAVersion(version)
e0ab38
+
e0ab38
 
e0ab38
 tasks = RedHatTaskNamespace()
e0ab38
-- 
e0ab38
2.4.3
e0ab38