annotate Lib/distutils/version.py @ 80661:94de77bd0b4b 2.7

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