]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/_distutils/version.py
4 # Implements multiple version numbering conventions for the
5 # Python Module Distribution Utilities.
10 """Provides classes to represent module version numbers (one class for
11 each style of version numbering). There are currently two such classes
12 implemented: StrictVersion and LooseVersion.
14 Every version number class implements the following interface:
15 * the 'parse' method takes a string and parses it to some internal
16 representation; if the string is an invalid version number,
17 'parse' raises a ValueError exception
18 * the class constructor takes an optional string argument which,
19 if supplied, is passed to 'parse'
20 * __str__ reconstructs the string that was passed to 'parse' (or
21 an equivalent string -- ie. one that will generate an equivalent
22 version number instance)
23 * __repr__ generates Python code to recreate the version number instance
24 * _cmp compares the current instance with either another instance
25 of the same class or a string (which will be parsed to an instance
26 of the same class, thus must follow the same rules)
34 @contextlib.contextmanager
35 def suppress_known_deprecation():
36 with warnings
.catch_warnings(record
=True) as ctx
:
37 warnings
.filterwarnings(
39 category
=DeprecationWarning,
40 message
="distutils Version classes are deprecated.",
46 """Abstract base class for version numbering classes. Just provides
47 constructor (__init__) and reproducer (__repr__), because those
48 seem to be the same for all version numbering classes; and route
49 rich comparisons to _cmp.
52 def __init__(self
, vstring
=None):
56 "distutils Version classes are deprecated. "
57 "Use packaging.version instead.",
63 return "{} ('{}')".format(self
.__class
__.__name
__, str(self
))
65 def __eq__(self
, other
):
67 if c
is NotImplemented:
71 def __lt__(self
, other
):
73 if c
is NotImplemented:
77 def __le__(self
, other
):
79 if c
is NotImplemented:
83 def __gt__(self
, other
):
85 if c
is NotImplemented:
89 def __ge__(self
, other
):
91 if c
is NotImplemented:
96 # Interface for version-number classes -- must be implemented
97 # by the following classes (the concrete ones -- Version should
98 # be treated as an abstract class).
99 # __init__ (string) - create and take same action as 'parse'
100 # (string parameter is optional)
101 # parse (string) - convert a string representation to whatever
102 # internal representation is appropriate for
103 # this style of version numbering
104 # __str__ (self) - convert back to a string; should be very similar
105 # (if not identical to) the string supplied to parse
106 # __repr__ (self) - generate Python code to recreate
108 # _cmp (self, other) - compare two version numbers ('other' may
109 # be an unparsed version string, or another
110 # instance of your version class)
113 class StrictVersion(Version
):
115 """Version numbering for anal retentives and software idealists.
116 Implements the standard interface for version number classes as
117 described above. A version number consists of two or three
118 dot-separated numeric components, with an optional "pre-release" tag
119 on the end. The pre-release tag consists of the letter 'a' or 'b'
120 followed by a number. If the numeric components of two version
121 numbers are equal, then one with a pre-release tag will always
122 be deemed earlier (lesser) than one without.
124 The following are valid version numbers (shown in the order that
125 would be obtained by sorting according to the supplied cmp function):
127 0.4 0.4.0 (these two are equivalent)
138 The following are examples of invalid version numbers:
146 The rationale for this version numbering system will be explained
147 in the distutils documentation.
150 version_re
= re
.compile(
151 r
'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$', re
.VERBOSE | re
.ASCII
154 def parse(self
, vstring
):
155 match
= self
.version_re
.match(vstring
)
157 raise ValueError("invalid version number '%s'" % vstring
)
159 (major
, minor
, patch
, prerelease
, prerelease_num
) = match
.group(1, 2, 4, 5, 6)
162 self
.version
= tuple(map(int, [major
, minor
, patch
]))
164 self
.version
= tuple(map(int, [major
, minor
])) + (0,)
167 self
.prerelease
= (prerelease
[0], int(prerelease_num
))
169 self
.prerelease
= None
173 if self
.version
[2] == 0:
174 vstring
= '.'.join(map(str, self
.version
[0:2]))
176 vstring
= '.'.join(map(str, self
.version
))
179 vstring
= vstring
+ self
.prerelease
[0] + str(self
.prerelease
[1])
183 def _cmp(self
, other
): # noqa: C901
184 if isinstance(other
, str):
185 with
suppress_known_deprecation():
186 other
= StrictVersion(other
)
187 elif not isinstance(other
, StrictVersion
):
188 return NotImplemented
190 if self
.version
!= other
.version
:
191 # numeric versions don't match
192 # prerelease stuff doesn't matter
193 if self
.version
< other
.version
:
198 # have to compare prerelease
199 # case 1: neither has prerelease; they're equal
200 # case 2: self has prerelease, other doesn't; other is greater
201 # case 3: self doesn't have prerelease, other does: self is greater
202 # case 4: both have prerelease: must compare them!
204 if not self
.prerelease
and not other
.prerelease
:
206 elif self
.prerelease
and not other
.prerelease
:
208 elif not self
.prerelease
and other
.prerelease
:
210 elif self
.prerelease
and other
.prerelease
:
211 if self
.prerelease
== other
.prerelease
:
213 elif self
.prerelease
< other
.prerelease
:
218 assert False, "never get here"
221 # end class StrictVersion
224 # The rules according to Greg Stein:
225 # 1) a version number has 1 or more numbers separated by a period or by
226 # sequences of letters. If only periods, then these are compared
227 # left-to-right to determine an ordering.
228 # 2) sequences of letters are part of the tuple for comparison and are
229 # compared lexicographically
230 # 3) recognize the numeric components may have leading zeroes
232 # The LooseVersion class below implements these rules: a version number
233 # string is split up into a tuple of integer and string components, and
234 # comparison is a simple tuple comparison. This means that version
235 # numbers behave in a predictable and obvious way, but a way that might
236 # not necessarily be how people *want* version numbers to behave. There
237 # wouldn't be a problem if people could stick to purely numeric version
238 # numbers: just split on period and compare the numbers as tuples.
239 # However, people insist on putting letters into their version numbers;
240 # the most common purpose seems to be:
241 # - indicating a "pre-release" version
242 # ('alpha', 'beta', 'a', 'b', 'pre', 'p')
243 # - indicating a post-release patch ('p', 'pl', 'patch')
244 # but of course this can't cover all version number schemes, and there's
245 # no way to know what a programmer means without asking him.
247 # The problem is what to do with letters (and other non-numeric
248 # characters) in a version number. The current implementation does the
249 # obvious and predictable thing: keep them as strings and compare
250 # lexically within a tuple comparison. This has the desired effect if
251 # an appended letter sequence implies something "post-release":
252 # eg. "0.99" < "0.99pl14" < "1.0", and "5.001" < "5.001m" < "5.002".
254 # However, if letters in a version number imply a pre-release version,
255 # the "obvious" thing isn't correct. Eg. you would expect that
256 # "1.5.1" < "1.5.2a2" < "1.5.2", but under the tuple/lexical comparison
257 # implemented here, this just isn't so.
259 # Two possible solutions come to mind. The first is to tie the
260 # comparison algorithm to a particular set of semantic rules, as has
261 # been done in the StrictVersion class above. This works great as long
262 # as everyone can go along with bondage and discipline. Hopefully a
263 # (large) subset of Python module programmers will agree that the
264 # particular flavour of bondage and discipline provided by StrictVersion
265 # provides enough benefit to be worth using, and will submit their
266 # version numbering scheme to its domination. The free-thinking
267 # anarchists in the lot will never give in, though, and something needs
268 # to be done to accommodate them.
270 # Perhaps a "moderately strict" version class could be implemented that
271 # lets almost anything slide (syntactically), and makes some heuristic
272 # assumptions about non-digits in version number strings. This could
273 # sink into special-case-hell, though; if I was as talented and
274 # idiosyncratic as Larry Wall, I'd go ahead and implement a class that
275 # somehow knows that "1.2.1" < "1.2.2a2" < "1.2.2" < "1.2.2pl3", and is
276 # just as happy dealing with things like "2g6" and "1.13++". I don't
277 # think I'm smart enough to do it right though.
279 # In any case, I've coded the test suite for this module (see
280 # ../test/test_version.py) specifically to fail on things like comparing
281 # "1.2a2" and "1.2". That's not because the *code* is doing anything
282 # wrong, it's because the simple, obvious design doesn't match my
283 # complicated, hairy expectations for real-world version numbers. It
284 # would be a snap to fix the test suite to say, "Yep, LooseVersion does
285 # the Right Thing" (ie. the code matches the conception). But I'd rather
286 # have a conception that matches common notions about version numbers.
289 class LooseVersion(Version
):
291 """Version numbering for anarchists and software realists.
292 Implements the standard interface for version number classes as
293 described above. A version number consists of a series of numbers,
294 separated by either periods or strings of letters. When comparing
295 version numbers, the numeric components will be compared
296 numerically, and the alphabetic components lexically. The following
297 are all valid version numbers, in no particular order:
316 In fact, there is no such thing as an invalid version number under
317 this scheme; the rules for comparison are simple and predictable,
318 but may not always give the results you want (for some definition
322 component_re
= re
.compile(r
'(\d+ | [a-z]+ | \.)', re
.VERBOSE
)
324 def parse(self
, vstring
):
325 # I've given up on thinking I can reconstruct the version string
326 # from the parsed tuple -- so I just store the string here for
328 self
.vstring
= vstring
329 components
= [x
for x
in self
.component_re
.split(vstring
) if x
and x
!= '.']
330 for i
, obj
in enumerate(components
):
332 components
[i
] = int(obj
)
336 self
.version
= components
342 return "LooseVersion ('%s')" % str(self
)
344 def _cmp(self
, other
):
345 if isinstance(other
, str):
346 other
= LooseVersion(other
)
347 elif not isinstance(other
, LooseVersion
):
348 return NotImplemented
350 if self
.version
== other
.version
:
352 if self
.version
< other
.version
:
354 if self
.version
> other
.version
:
358 # end class LooseVersion