| 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 |
from __future__ import annotations |
| 20 |
|
| 21 |
import re |
| 22 |
import sys |
| 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.Pattern[str] = re.compile(r"(\d+ | [a-z]+ | \.)", re.VERBOSE) |
| 123 |
vstring: str |
| 124 |
version: list[int | str] |
| 125 |
|
| 126 |
def __init__(self, vstring: str | None = None): |
| 127 |
if vstring: |
| 128 |
self.parse(vstring) |
| 129 |
|
| 130 |
def __eq__(self, other: object) -> bool: |
| 131 |
c = self._cmp(other) |
| 132 |
if c is NotImplemented: |
| 133 |
return NotImplemented |
| 134 |
return c == 0 |
| 135 |
|
| 136 |
def __lt__(self, other: object) -> bool: |
| 137 |
c = self._cmp(other) |
| 138 |
if c is NotImplemented: |
| 139 |
return NotImplemented |
| 140 |
return c < 0 |
| 141 |
|
| 142 |
def __le__(self, other: object) -> bool: |
| 143 |
c = self._cmp(other) |
| 144 |
if c is NotImplemented: |
| 145 |
return NotImplemented |
| 146 |
return c <= 0 |
| 147 |
|
| 148 |
def __gt__(self, other: object) -> bool: |
| 149 |
c = self._cmp(other) |
| 150 |
if c is NotImplemented: |
| 151 |
return NotImplemented |
| 152 |
return c > 0 |
| 153 |
|
| 154 |
def __ge__(self, other: object) -> bool: |
| 155 |
c = self._cmp(other) |
| 156 |
if c is NotImplemented: |
| 157 |
return NotImplemented |
| 158 |
return c >= 0 |
| 159 |
|
| 160 |
def parse(self, vstring: str) -> None: |
| 161 |
# I've given up on thinking I can reconstruct the version string |
| 162 |
# from the parsed tuple -- so I just store the string here for |
| 163 |
# use by __str__ |
| 164 |
self.vstring = vstring |
| 165 |
components: list[str | int] = [ |
| 166 |
x for x in self.component_re.split(vstring) if x and x != "." |
| 167 |
] |
| 168 |
for i, obj in enumerate(components): |
| 169 |
try: |
| 170 |
components[i] = int(obj) |
| 171 |
except ValueError: |
| 172 |
pass |
| 173 |
|
| 174 |
self.version = components |
| 175 |
|
| 176 |
def __str__(self) -> str: |
| 177 |
return self.vstring |
| 178 |
|
| 179 |
def __repr__(self) -> str: |
| 180 |
return "LooseVersion ('%s')" % str(self) |
| 181 |
|
| 182 |
def _cmp(self, other: object) -> int: |
| 183 |
other = self._coerce(other) |
| 184 |
if other is NotImplemented: |
| 185 |
return NotImplemented |
| 186 |
|
| 187 |
if self.version == other.version: |
| 188 |
return 0 |
| 189 |
if self.version < other.version: |
| 190 |
return -1 |
| 191 |
if self.version > other.version: |
| 192 |
return 1 |
| 193 |
return NotImplemented |
| 194 |
|
| 195 |
@staticmethod |
| 196 |
def _coerce(other: object) -> LooseVersion: |
| 197 |
if isinstance(other, LooseVersion): |
| 198 |
return other |
| 199 |
elif isinstance(other, str): |
| 200 |
return LooseVersion(other) |
| 201 |
elif "distutils" in sys.modules: |
| 202 |
# Using this check to avoid importing distutils and suppressing the warning |
| 203 |
try: |
| 204 |
from distutils.version import LooseVersion as deprecated |
| 205 |
except ImportError: |
| 206 |
return NotImplemented |
| 207 |
if isinstance(other, deprecated): |
| 208 |
return LooseVersion(str(other)) |
| 209 |
return NotImplemented |