summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/version.py
blob: 86803afcbcec511fc74498a76fbd38c4054af4ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
""" bcfg2 version declaration and handling """

import re

__version__ = "1.3.0rc1"


class Bcfg2VersionInfo(tuple):
    """ object to make granular version operations (particularly
    comparisons) easier """

    v_re = re.compile(r'(\d+)(\w+)(\d+)')

    def __new__(cls, vstr):
        (major, minor, rest) = vstr.split(".")
        match = cls.v_re.match(rest)
        if match:
            micro, releaselevel, serial = match.groups()
        else:
            micro = rest
            releaselevel = 'final'
            serial = 0
        return tuple.__new__(cls, [int(major), int(minor), int(micro),
                                   releaselevel, int(serial)])

    def __init__(self, vstr):  # pylint: disable=W0613
        tuple.__init__(self)
        self.major, self.minor, self.micro, self.releaselevel, self.serial = \
            tuple(self)

    def __repr__(self):
        return "%s(major=%s, minor=%s, micro=%s, releaselevel=%s, serial=%s)" \
            % ((self.__class__.__name__,) + tuple(self))

    def _release_cmp(self, rel1, rel2):  # pylint: disable=R0911
        """ compare two release numbers """
        if rel1 == rel2:
            return 0
        elif rel1 == "final":
            return -1
        elif rel2 == "final":
            return 1
        elif rel1 == "rc":
            return -1
        elif rel2 == "rc":
            return 1
            # should never get to anything past this point
        elif rel1 == "pre":
            return -1
        elif rel2 == "pre":
            return 1
        else:
            # wtf?
            return 0

    def __gt__(self, version):
        if version is None:
            # older bcfg2 clients didn't report their version, so we
            # handle this case specially and assume that any reported
            # version is newer than any indeterminate version
            return True
        try:
            for i in range(3):
                if self[i] != version[i]:
                    return self[i] > version[i]
            rel = self._release_cmp(self[3], version[3])
            if rel != 0:
                return rel < 0
            return self[4] > version[4]
        except TypeError:
            return self > Bcfg2VersionInfo(version)

    def __lt__(self, version):
        if version is None:
            # older bcfg2 clients didn't report their version, so we
            # handle this case specially and assume that any reported
            # version is newer than any indeterminate version
            return False
        try:
            for i in range(3):
                if self[i] != version[i]:
                    return self[i] < version[i]
            rel = self._release_cmp(self[3], version[3])
            if rel != 0:
                return rel > 0
            return self[4] < version[4]
        except TypeError:
            return self < Bcfg2VersionInfo(version)

    def __eq__(self, version):
        if version is None:
            # older bcfg2 clients didn't report their version, so we
            # handle this case specially and assume that any reported
            # version is newer than any indeterminate version
            return False
        try:
            rv = True
            for i in range(len(self)):
                rv &= self[i] == version[i]
            return rv
        except TypeError:
            return self == Bcfg2VersionInfo(version)

    def __ge__(self, version):
        return not self < version

    def __le__(self, version):
        return not self > version