freeipa

FreeIPA is an integrated Identity and Authentication solution for Linux/UNIX networked environments.  |  http://www.freeipa.org/

Commit dccdade Fix version comparison

2 files Authored and Committed by mbasti 2 years ago
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>

    
1 @@ -159,6 +159,7 @@
2   Requires: systemd-python
3   Requires: %{etc_systemd_dir}
4   Requires: gzip
5 + Requires: rpm-python
6   
7   Conflicts: %{alt_name}-server
8   Obsoletes: %{alt_name}-server < %{version}
 1 @@ -30,6 +30,8 @@
 2   import sys
 3   import urllib
 4   import base64
 5 + import rpm
 6 + from functools import total_ordering
 7   
 8   from subprocess import CalledProcessError
 9   from nss.error import NSPRError
10 @@ -46,6 +48,35 @@
11   from ipaplatform.base.tasks import BaseTaskNamespace
12   
13   
14 + # copied from rpmUtils/miscutils.py
15 + def stringToVersion(verstring):
16 +     if verstring in [None, '']:
17 +         return (None, None, None)
18 +     i = verstring.find(':')
19 +     if i != -1:
20 +         try:
21 +             epoch = str(long(verstring[:i]))
22 +         except ValueError:
23 +             # look, garbage in the epoch field, how fun, kill it
24 +             epoch = '0' # this is our fallback, deal
25 +     else:
26 +         epoch = '0'
27 +     j = verstring.find('-')
28 +     if j != -1:
29 +         if verstring[i + 1:j] == '':
30 +             version = None
31 +         else:
32 +             version = verstring[i + 1:j]
33 +         release = verstring[j + 1:]
34 +     else:
35 +         if verstring[i + 1:] == '':
36 +             version = None
37 +         else:
38 +             version = verstring[i + 1:]
39 +         release = None
40 +     return (epoch, version, release)
41 + 
42 + 
43   log = log_mgr.get_logger(__name__)
44   
45   
46 @@ -65,6 +96,21 @@
47           return False
48   
49   
50 + @total_ordering
51 + class IPAVersion(object):
52 + 
53 +     def __init__(self, version):
54 +         self.version_tuple = stringToVersion(version)
55 + 
56 +     def __eq__(self, other):
57 +         assert isinstance(other, IPAVersion)
58 +         return rpm.labelCompare(self.version_tuple, other.version_tuple) == 0
59 + 
60 +     def __lt__(self, other):
61 +         assert isinstance(other, IPAVersion)
62 +         return rpm.labelCompare(self.version_tuple, other.version_tuple) == -1
63 + 
64 + 
65   class RedHatTaskNamespace(BaseTaskNamespace):
66   
67       def restore_context(self, filepath, restorecon=paths.SBIN_RESTORECON):
68 @@ -422,5 +468,12 @@
69           super(RedHatTaskNamespace, self).create_system_user(name, group,
70               homedir, shell, uid, gid, comment, create_homedir)
71   
72 +     def parse_ipa_version(self, version):
73 +         """
74 +         :param version: textual version
75 +         :return: object implementing proper __cmp__ method for version compare
76 +         """
77 +         return IPAVersion(version)
78 + 
79   
80   tasks = RedHatTaskNamespace()