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