Debian Codegraph @

third_party/python/looseversion/looseversion.py

raw
1 """Provides classes to represent module version numbers (one class for
2 each style of version numbering). There are currently two such classes
3 implemented: StrictVersion and LooseVersion.
4
5 Every version number class implements the following interface:
6 * the 'parse' method takes a string and parses it to some internal
7 representation; if the string is an invalid version number,
8 'parse' raises a ValueError exception
9 * the class constructor takes an optional string argument which,
10 if supplied, is passed to 'parse'
11 * __str__ reconstructs the string that was passed to 'parse' (or
12 an equivalent string -- ie. one that will generate an equivalent
13 version number instance)
14 * __repr__ generates Python code to recreate the version number instance
15 * _cmp compares the current instance with either another instance
16 of the same class or a string (which will be parsed to an instance
17 of the same class, thus must follow the same rules)
18 """
19
20 import sys
21 import re
22
23
24 # The rules according to Greg Stein:
25 # 1) a version number has 1 or more numbers separated by a period or by
26 # sequences of letters. If only periods, then these are compared
27 # left-to-right to determine an ordering.
28 # 2) sequences of letters are part of the tuple for comparison and are
29 # compared lexicographically
30 # 3) recognize the numeric components may have leading zeroes
31 #
32 # The LooseVersion class below implements these rules: a version number
33 # string is split up into a tuple of integer and string components, and
34 # comparison is a simple tuple comparison. This means that version
35 # numbers behave in a predictable and obvious way, but a way that might
36 # not necessarily be how people *want* version numbers to behave. There
37 # wouldn't be a problem if people could stick to purely numeric version
38 # numbers: just split on period and compare the numbers as tuples.
39 # However, people insist on putting letters into their version numbers;
40 # the most common purpose seems to be:
41 # - indicating a "pre-release" version
42 # ('alpha', 'beta', 'a', 'b', 'pre', 'p')
43 # - indicating a post-release patch ('p', 'pl', 'patch')
44 # but of course this can't cover all version number schemes, and there's
45 # no way to know what a programmer means without asking him.
46 #
47 # The problem is what to do with letters (and other non-numeric
48 # characters) in a version number. The current implementation does the
49 # obvious and predictable thing: keep them as strings and compare
50 # lexically within a tuple comparison. This has the desired effect if
51 # an appended letter sequence implies something "post-release":
52 # eg. "0.99" < "0.99pl14" < "1.0", and "5.001" < "5.001m" < "5.002".
53 #
54 # However, if letters in a version number imply a pre-release version,
55 # the "obvious" thing isn't correct. Eg. you would expect that
56 # "1.5.1" < "1.5.2a2" < "1.5.2", but under the tuple/lexical comparison
57 # implemented here, this just isn't so.
58 #
59 # Two possible solutions come to mind. The first is to tie the
60 # comparison algorithm to a particular set of semantic rules, as has
61 # been done in the StrictVersion class above. This works great as long
62 # as everyone can go along with bondage and discipline. Hopefully a
63 # (large) subset of Python module programmers will agree that the
64 # particular flavour of bondage and discipline provided by StrictVersion
65 # provides enough benefit to be worth using, and will submit their
66 # version numbering scheme to its domination. The free-thinking
67 # anarchists in the lot will never give in, though, and something needs
68 # to be done to accommodate them.
69 #
70 # Perhaps a "moderately strict" version class could be implemented that
71 # lets almost anything slide (syntactically), and makes some heuristic
72 # assumptions about non-digits in version number strings. This could
73 # sink into special-case-hell, though; if I was as talented and
74 # idiosyncratic as Larry Wall, I'd go ahead and implement a class that
75 # somehow knows that "1.2.1" < "1.2.2a2" < "1.2.2" < "1.2.2pl3", and is
76 # just as happy dealing with things like "2g6" and "1.13++". I don't
77 # think I'm smart enough to do it right though.
78 #
79 # In any case, I've coded the test suite for this module (see
80 # ../test/test_version.py) specifically to fail on things like comparing
81 # "1.2a2" and "1.2". That's not because the *code* is doing anything
82 # wrong, it's because the simple, obvious design doesn't match my
83 # complicated, hairy expectations for real-world version numbers. It
84 # would be a snap to fix the test suite to say, "Yep, LooseVersion does
85 # the Right Thing" (ie. the code matches the conception). But I'd rather
86 # have a conception that matches common notions about version numbers.
87
88
89 class LooseVersion:
90
91 """Version numbering for anarchists and software realists.
92 Implements the standard interface for version number classes as
93 described above. A version number consists of a series of numbers,
94 separated by either periods or strings of letters. When comparing
95 version numbers, the numeric components will be compared
96 numerically, and the alphabetic components lexically. The following
97 are all valid version numbers, in no particular order:
98
99 1.5.1
100 1.5.2b2
101 161
102 3.10a
103 8.02
104 3.4j
105 1996.07.12
106 3.2.pl0
107 3.1.1.6
108 2g6
109 11g
110 0.960923
111 2.2beta29
112 1.13++
113 5.5.kw
114 2.0b1pl0
115
116 In fact, there is no such thing as an invalid version number under
117 this scheme; the rules for comparison are simple and predictable,
118 but may not always give the results you want (for some definition
119 of "want").
120 """
121
122 component_re = re.compile(r"(\d+ | [a-z]+ | \.)", re.VERBOSE)
123
124 def __init__(self, vstring=None):
125 if vstring:
126 self.parse(vstring)
127
128 def __eq__(self, other):
129 c = self._cmp(other)
130 if c is NotImplemented:
131 return c
132 return c == 0
133
134 def __lt__(self, other):
135 c = self._cmp(other)
136 if c is NotImplemented:
137 return c
138 return c < 0
139
140 def __le__(self, other):
141 c = self._cmp(other)
142 if c is NotImplemented:
143 return c
144 return c <= 0
145
146 def __gt__(self, other):
147 c = self._cmp(other)
148 if c is NotImplemented:
149 return c
150 return c > 0
151
152 def __ge__(self, other):
153 c = self._cmp(other)
154 if c is NotImplemented:
155 return c
156 return c >= 0
157
158 def parse(self, vstring):
159 # I've given up on thinking I can reconstruct the version string
160 # from the parsed tuple -- so I just store the string here for
161 # use by __str__
162 self.vstring = vstring
163 components = [x for x in self.component_re.split(vstring) if x and x != "."]
164 for i, obj in enumerate(components):
165 try:
166 components[i] = int(obj)
167 except ValueError:
168 pass
169
170 self.version = components
171
172 def __str__(self):
173 return self.vstring
174
175 def __repr__(self):
176 return "LooseVersion ('%s')" % str(self)
177
178 def _cmp(self, other):
179 other = self._coerce(other)
180 if other is NotImplemented:
181 return NotImplemented
182
183 if self.version == other.version:
184 return 0
185 if self.version < other.version:
186 return -1
187 if self.version > other.version:
188 return 1
189
190 @staticmethod
191 def _coerce(other):
192 if isinstance(other, LooseVersion):
193 return other
194 elif isinstance(other, str):
195 return LooseVersion(other)
196 elif "distutils" in sys.modules:
197 # Using this check to avoid importing distutils and suppressing the warning
198 try:
199 from distutils.version import LooseVersion as deprecated
200 except ImportError:
201 return NotImplemented
202 if isinstance(other, deprecated):
203 return LooseVersion(str(other))
204 return NotImplemented