Debian Codegraph @

ansible_collections/community/postgresql/plugins/module_utils/_version.py

raw
1 # Vendored copy of distutils/version.py from CPython 3.9.5
2 #
3 # Implements multiple version numbering conventions for the
4 # Python Module Distribution Utilities.
5 #
6 # PSF License (see PSF-license.txt or https://opensource.org/licenses/Python-2.0)
7 #
8
9 """Provides classes to represent module version numbers (one class for
10 each style of version numbering). There are currently two such classes
11 implemented: StrictVersion and LooseVersion.
12 Every version number class implements the following interface:
13 * the 'parse' method takes a string and parses it to some internal
14 representation; if the string is an invalid version number,
15 'parse' raises a ValueError exception
16 * the class constructor takes an optional string argument which,
17 if supplied, is passed to 'parse'
18 * __str__ reconstructs the string that was passed to 'parse' (or
19 an equivalent string -- ie. one that will generate an equivalent
20 version number instance)
21 * __repr__ generates Python code to recreate the version number instance
22 * _cmp compares the current instance with either another instance
23 of the same class or a string (which will be parsed to an instance
24 of the same class, thus must follow the same rules)
25 """
26
27 from __future__ import absolute_import, division, print_function
28
29 __metaclass__ = type
30
31 import re
32
33 try:
34 RE_FLAGS = re.VERBOSE | re.ASCII
35 except AttributeError:
36 RE_FLAGS = re.VERBOSE
37
38
39 class Version:
40 """Abstract base class for version numbering classes. Just provides
41 constructor (__init__) and reproducer (__repr__), because those
42 seem to be the same for all version numbering classes; and route
43 rich comparisons to _cmp.
44 """
45
46 def __init__(self, vstring=None):
47 if vstring:
48 self.parse(vstring)
49
50 def __repr__(self):
51 return "%s ('%s')" % (self.__class__.__name__, str(self))
52
53 def __eq__(self, other):
54 c = self._cmp(other)
55 if c is NotImplemented:
56 return c
57 return c == 0
58
59 def __lt__(self, other):
60 c = self._cmp(other)
61 if c is NotImplemented:
62 return c
63 return c < 0
64
65 def __le__(self, other):
66 c = self._cmp(other)
67 if c is NotImplemented:
68 return c
69 return c <= 0
70
71 def __gt__(self, other):
72 c = self._cmp(other)
73 if c is NotImplemented:
74 return c
75 return c > 0
76
77 def __ge__(self, other):
78 c = self._cmp(other)
79 if c is NotImplemented:
80 return c
81 return c >= 0
82
83
84 # Interface for version-number classes -- must be implemented
85 # by the following classes (the concrete ones -- Version should
86 # be treated as an abstract class).
87 # __init__ (string) - create and take same action as 'parse'
88 # (string parameter is optional)
89 # parse (string) - convert a string representation to whatever
90 # internal representation is appropriate for
91 # this style of version numbering
92 # __str__ (self) - convert back to a string; should be very similar
93 # (if not identical to) the string supplied to parse
94 # __repr__ (self) - generate Python code to recreate
95 # the instance
96 # _cmp (self, other) - compare two version numbers ('other' may
97 # be an unparsed version string, or another
98 # instance of your version class)
99
100
101 class StrictVersion(Version):
102 """Version numbering for anal retentives and software idealists.
103 Implements the standard interface for version number classes as
104 described above. A version number consists of two or three
105 dot-separated numeric components, with an optional "pre-release" tag
106 on the end. The pre-release tag consists of the letter 'a' or 'b'
107 followed by a number. If the numeric components of two version
108 numbers are equal, then one with a pre-release tag will always
109 be deemed earlier (lesser) than one without.
110 The following are valid version numbers (shown in the order that
111 would be obtained by sorting according to the supplied cmp function):
112 0.4 0.4.0 (these two are equivalent)
113 0.4.1
114 0.5a1
115 0.5b3
116 0.5
117 0.9.6
118 1.0
119 1.0.4a3
120 1.0.4b1
121 1.0.4
122 The following are examples of invalid version numbers:
123 1
124 2.7.2.2
125 1.3.a4
126 1.3pl1
127 1.3c4
128 The rationale for this version numbering system will be explained
129 in the distutils documentation.
130 """
131
132 version_re = re.compile(r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$',
133 RE_FLAGS)
134
135 def parse(self, vstring):
136 match = self.version_re.match(vstring)
137 if not match:
138 raise ValueError("invalid version number '%s'" % vstring)
139
140 (major, minor, patch, prerelease, prerelease_num) = \
141 match.group(1, 2, 4, 5, 6)
142
143 if patch:
144 self.version = tuple(map(int, [major, minor, patch]))
145 else:
146 self.version = tuple(map(int, [major, minor])) + (0,)
147
148 if prerelease:
149 self.prerelease = (prerelease[0], int(prerelease_num))
150 else:
151 self.prerelease = None
152
153 def __str__(self):
154 if self.version[2] == 0:
155 vstring = '.'.join(map(str, self.version[0:2]))
156 else:
157 vstring = '.'.join(map(str, self.version))
158
159 if self.prerelease:
160 vstring = vstring + self.prerelease[0] + str(self.prerelease[1])
161
162 return vstring
163
164 def _cmp(self, other):
165 if isinstance(other, str):
166 other = StrictVersion(other)
167 elif not isinstance(other, StrictVersion):
168 return NotImplemented
169
170 if self.version != other.version:
171 # numeric versions don't match
172 # prerelease stuff doesn't matter
173 if self.version < other.version:
174 return -1
175 else:
176 return 1
177
178 # have to compare prerelease
179 # case 1: neither has prerelease; they're equal
180 # case 2: self has prerelease, other doesn't; other is greater
181 # case 3: self doesn't have prerelease, other does: self is greater
182 # case 4: both have prerelease: must compare them!
183
184 if (not self.prerelease and not other.prerelease):
185 return 0
186 elif (self.prerelease and not other.prerelease):
187 return -1
188 elif (not self.prerelease and other.prerelease):
189 return 1
190 elif (self.prerelease and other.prerelease):
191 if self.prerelease == other.prerelease:
192 return 0
193 elif self.prerelease < other.prerelease:
194 return -1
195 else:
196 return 1
197 else:
198 raise AssertionError("never get here")
199
200 # end class StrictVersion
201
202 # The rules according to Greg Stein:
203 # 1) a version number has 1 or more numbers separated by a period or by
204 # sequences of letters. If only periods, then these are compared
205 # left-to-right to determine an ordering.
206 # 2) sequences of letters are part of the tuple for comparison and are
207 # compared lexicographically
208 # 3) recognize the numeric components may have leading zeroes
209 #
210 # The LooseVersion class below implements these rules: a version number
211 # string is split up into a tuple of integer and string components, and
212 # comparison is a simple tuple comparison. This means that version
213 # numbers behave in a predictable and obvious way, but a way that might
214 # not necessarily be how people *want* version numbers to behave. There
215 # wouldn't be a problem if people could stick to purely numeric version
216 # numbers: just split on period and compare the numbers as tuples.
217 # However, people insist on putting letters into their version numbers;
218 # the most common purpose seems to be:
219 # - indicating a "pre-release" version
220 # ('alpha', 'beta', 'a', 'b', 'pre', 'p')
221 # - indicating a post-release patch ('p', 'pl', 'patch')
222 # but of course this can't cover all version number schemes, and there's
223 # no way to know what a programmer means without asking him.
224 #
225 # The problem is what to do with letters (and other non-numeric
226 # characters) in a version number. The current implementation does the
227 # obvious and predictable thing: keep them as strings and compare
228 # lexically within a tuple comparison. This has the desired effect if
229 # an appended letter sequence implies something "post-release":
230 # eg. "0.99" < "0.99pl14" < "1.0", and "5.001" < "5.001m" < "5.002".
231 #
232 # However, if letters in a version number imply a pre-release version,
233 # the "obvious" thing isn't correct. Eg. you would expect that
234 # "1.5.1" < "1.5.2a2" < "1.5.2", but under the tuple/lexical comparison
235 # implemented here, this just isn't so.
236 #
237 # Two possible solutions come to mind. The first is to tie the
238 # comparison algorithm to a particular set of semantic rules, as has
239 # been done in the StrictVersion class above. This works great as long
240 # as everyone can go along with bondage and discipline. Hopefully a
241 # (large) subset of Python module programmers will agree that the
242 # particular flavour of bondage and discipline provided by StrictVersion
243 # provides enough benefit to be worth using, and will submit their
244 # version numbering scheme to its domination. The free-thinking
245 # anarchists in the lot will never give in, though, and something needs
246 # to be done to accommodate them.
247 #
248 # Perhaps a "moderately strict" version class could be implemented that
249 # lets almost anything slide (syntactically), and makes some heuristic
250 # assumptions about non-digits in version number strings. This could
251 # sink into special-case-hell, though; if I was as talented and
252 # idiosyncratic as Larry Wall, I'd go ahead and implement a class that
253 # somehow knows that "1.2.1" < "1.2.2a2" < "1.2.2" < "1.2.2pl3", and is
254 # just as happy dealing with things like "2g6" and "1.13++". I don't
255 # think I'm smart enough to do it right though.
256 #
257 # In any case, I've coded the test suite for this module (see
258 # ../test/test_version.py) specifically to fail on things like comparing
259 # "1.2a2" and "1.2". That's not because the *code* is doing anything
260 # wrong, it's because the simple, obvious design doesn't match my
261 # complicated, hairy expectations for real-world version numbers. It
262 # would be a snap to fix the test suite to say, "Yep, LooseVersion does
263 # the Right Thing" (ie. the code matches the conception). But I'd rather
264 # have a conception that matches common notions about version numbers.
265
266
267 class LooseVersion(Version):
268 """Version numbering for anarchists and software realists.
269 Implements the standard interface for version number classes as
270 described above. A version number consists of a series of numbers,
271 separated by either periods or strings of letters. When comparing
272 version numbers, the numeric components will be compared
273 numerically, and the alphabetic components lexically. The following
274 are all valid version numbers, in no particular order:
275 1.5.1
276 1.5.2b2
277 161
278 3.10a
279 8.02
280 3.4j
281 1996.07.12
282 3.2.pl0
283 3.1.1.6
284 2g6
285 11g
286 0.960923
287 2.2beta29
288 1.13++
289 5.5.kw
290 2.0b1pl0
291 In fact, there is no such thing as an invalid version number under
292 this scheme; the rules for comparison are simple and predictable,
293 but may not always give the results you want (for some definition
294 of "want").
295 """
296
297 component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE)
298
299 def __init__(self, vstring=None):
300 if vstring:
301 self.parse(vstring)
302
303 def parse(self, vstring):
304 # I've given up on thinking I can reconstruct the version string
305 # from the parsed tuple -- so I just store the string here for
306 # use by __str__
307 self.vstring = vstring
308 components = [x for x in self.component_re.split(vstring) if x and x != '.']
309 for i, obj in enumerate(components):
310 try:
311 components[i] = int(obj)
312 except ValueError:
313 pass
314
315 self.version = components
316
317 def __str__(self):
318 return self.vstring
319
320 def __repr__(self):
321 return "LooseVersion ('%s')" % str(self)
322
323 def _cmp(self, other):
324 if isinstance(other, str):
325 other = LooseVersion(other)
326 elif not isinstance(other, LooseVersion):
327 return NotImplemented
328
329 if self.version == other.version:
330 return 0
331 if self.version < other.version:
332 return -1
333 if self.version > other.version:
334 return 1
335
336 # end class LooseVersion