| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Provides classes to represent module version numbers (one class for |
| 18 |
each style of version numbering). There are currently two such classes |
| 19 |
implemented: StrictVersion and LooseVersion. |
| 20 |
Every version number class implements the following interface: |
| 21 |
* the 'parse' method takes a string and parses it to some internal |
| 22 |
representation; if the string is an invalid version number, |
| 23 |
'parse' raises a ValueError exception |
| 24 |
* the class constructor takes an optional string argument which, |
| 25 |
if supplied, is passed to 'parse' |
| 26 |
* __str__ reconstructs the string that was passed to 'parse' (or |
| 27 |
an equivalent string -- ie. one that will generate an equivalent |
| 28 |
version number instance) |
| 29 |
* __repr__ generates Python code to recreate the version number instance |
| 30 |
* _cmp compares the current instance with either another instance |
| 31 |
of the same class or a string (which will be parsed to an instance |
| 32 |
of the same class, thus must follow the same rules) |
| 33 |
""" |
| 34 |
|
| 35 |
import re |
| 36 |
|
| 37 |
class Version: |
| 38 |
"""Abstract base class for version numbering classes. Just provides |
| 39 |
constructor (__init__) and reproducer (__repr__), because those |
| 40 |
seem to be the same for all version numbering classes; and route |
| 41 |
rich comparisons to _cmp. |
| 42 |
""" |
| 43 |
|
| 44 |
def __init__ (self, vstring=None): |
| 45 |
if vstring: |
| 46 |
self.parse(vstring) |
| 47 |
|
| 48 |
def __repr__ (self): |
| 49 |
return "%s ('%s')" % (self.__class__.__name__, str(self)) |
| 50 |
|
| 51 |
def __eq__(self, other): |
| 52 |
c = self._cmp(other) |
| 53 |
if c is NotImplemented: |
| 54 |
return c |
| 55 |
return c == 0 |
| 56 |
|
| 57 |
def __lt__(self, other): |
| 58 |
c = self._cmp(other) |
| 59 |
if c is NotImplemented: |
| 60 |
return c |
| 61 |
return c < 0 |
| 62 |
|
| 63 |
def __le__(self, other): |
| 64 |
c = self._cmp(other) |
| 65 |
if c is NotImplemented: |
| 66 |
return c |
| 67 |
return c <= 0 |
| 68 |
|
| 69 |
def __gt__(self, other): |
| 70 |
c = self._cmp(other) |
| 71 |
if c is NotImplemented: |
| 72 |
return c |
| 73 |
return c > 0 |
| 74 |
|
| 75 |
def __ge__(self, other): |
| 76 |
c = self._cmp(other) |
| 77 |
if c is NotImplemented: |
| 78 |
return c |
| 79 |
return c >= 0 |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
class StrictVersion (Version): |
| 100 |
|
| 101 |
"""Version numbering for anal retentives and software idealists. |
| 102 |
Implements the standard interface for version number classes as |
| 103 |
described above. A version number consists of two or three |
| 104 |
dot-separated numeric components, with an optional "pre-release" tag |
| 105 |
on the end. The pre-release tag consists of the letter 'a' or 'b' |
| 106 |
followed by a number. If the numeric components of two version |
| 107 |
numbers are equal, then one with a pre-release tag will always |
| 108 |
be deemed earlier (lesser) than one without. |
| 109 |
The following are valid version numbers (shown in the order that |
| 110 |
would be obtained by sorting according to the supplied cmp function): |
| 111 |
0.4 0.4.0 (these two are equivalent) |
| 112 |
0.4.1 |
| 113 |
0.5a1 |
| 114 |
0.5b3 |
| 115 |
0.5 |
| 116 |
0.9.6 |
| 117 |
1.0 |
| 118 |
1.0.4a3 |
| 119 |
1.0.4b1 |
| 120 |
1.0.4 |
| 121 |
The following are examples of invalid version numbers: |
| 122 |
1 |
| 123 |
2.7.2.2 |
| 124 |
1.3.a4 |
| 125 |
1.3pl1 |
| 126 |
1.3c4 |
| 127 |
The rationale for this version numbering system will be explained |
| 128 |
in the distutils documentation. |
| 129 |
""" |
| 130 |
|
| 131 |
version_re = re.compile(r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$', |
| 132 |
re.VERBOSE | re.ASCII) |
| 133 |
|
| 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 |
|
| 154 |
def __str__ (self): |
| 155 |
|
| 156 |
if self.version[2] == 0: |
| 157 |
vstring = '.'.join(map(str, self.version[0:2])) |
| 158 |
else: |
| 159 |
vstring = '.'.join(map(str, self.version)) |
| 160 |
|
| 161 |
if self.prerelease: |
| 162 |
vstring = vstring + self.prerelease[0] + str(self.prerelease[1]) |
| 163 |
|
| 164 |
return vstring |
| 165 |
|
| 166 |
|
| 167 |
def _cmp (self, other): |
| 168 |
if isinstance(other, str): |
| 169 |
other = StrictVersion(other) |
| 170 |
elif not isinstance(other, StrictVersion): |
| 171 |
return NotImplemented |
| 172 |
|
| 173 |
if self.version != other.version: |
| 174 |
|
| 175 |
|
| 176 |
if self.version < other.version: |
| 177 |
return -1 |
| 178 |
else: |
| 179 |
return 1 |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (not self.prerelease and not other.prerelease): |
| 188 |
return 0 |
| 189 |
elif (self.prerelease and not other.prerelease): |
| 190 |
return -1 |
| 191 |
elif (not self.prerelease and other.prerelease): |
| 192 |
return 1 |
| 193 |
elif (self.prerelease and other.prerelease): |
| 194 |
if self.prerelease == other.prerelease: |
| 195 |
return 0 |
| 196 |
elif self.prerelease < other.prerelease: |
| 197 |
return -1 |
| 198 |
else: |
| 199 |
return 1 |
| 200 |
else: |
| 201 |
assert False, "never get here" |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
class LooseVersion (Version): |
| 271 |
|
| 272 |
"""Version numbering for anarchists and software realists. |
| 273 |
Implements the standard interface for version number classes as |
| 274 |
described above. A version number consists of a series of numbers, |
| 275 |
separated by either periods or strings of letters. When comparing |
| 276 |
version numbers, the numeric components will be compared |
| 277 |
numerically, and the alphabetic components lexically. The following |
| 278 |
are all valid version numbers, in no particular order: |
| 279 |
1.5.1 |
| 280 |
1.5.2b2 |
| 281 |
161 |
| 282 |
3.10a |
| 283 |
8.02 |
| 284 |
3.4j |
| 285 |
1996.07.12 |
| 286 |
3.2.pl0 |
| 287 |
3.1.1.6 |
| 288 |
2g6 |
| 289 |
11g |
| 290 |
0.960923 |
| 291 |
2.2beta29 |
| 292 |
1.13++ |
| 293 |
5.5.kw |
| 294 |
2.0b1pl0 |
| 295 |
In fact, there is no such thing as an invalid version number under |
| 296 |
this scheme; the rules for comparison are simple and predictable, |
| 297 |
but may not always give the results you want (for some definition |
| 298 |
of "want"). |
| 299 |
""" |
| 300 |
|
| 301 |
component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE) |
| 302 |
|
| 303 |
def __init__ (self, vstring=None): |
| 304 |
if vstring: |
| 305 |
self.parse(vstring) |
| 306 |
|
| 307 |
|
| 308 |
def parse (self, vstring): |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
self.vstring = vstring |
| 313 |
components = [x for x in self.component_re.split(vstring) |
| 314 |
if x and x != '.'] |
| 315 |
for i, obj in enumerate(components): |
| 316 |
try: |
| 317 |
components[i] = int(obj) |
| 318 |
except ValueError: |
| 319 |
pass |
| 320 |
|
| 321 |
self.version = components |
| 322 |
|
| 323 |
|
| 324 |
def __str__ (self): |
| 325 |
return self.vstring |
| 326 |
|
| 327 |
|
| 328 |
def __repr__ (self): |
| 329 |
return "LooseVersion ('%s')" % str(self) |
| 330 |
|
| 331 |
|
| 332 |
def _cmp (self, other): |
| 333 |
if isinstance(other, str): |
| 334 |
other = LooseVersion(other) |
| 335 |
elif not isinstance(other, LooseVersion): |
| 336 |
return NotImplemented |
| 337 |
|
| 338 |
if self.version == other.version: |
| 339 |
return 0 |
| 340 |
if self.version < other.version: |
| 341 |
return -1 |
| 342 |
if self.version > other.version: |
| 343 |
return 1 |
| 344 |
|
| 345 |
|
| 346 |
|