annotate Lib/distutils/version.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 7a7f557b4d54
children f465d6692bde
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 # written by Greg Ward, 1998/12/17
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 # $Id$
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
10 #
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
11
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
12 """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
13 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
14 implemented: StrictVersion and LooseVersion.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
15
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
16 Every version number class implements the following interface:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
17 * 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
18 representation; if the string is an invalid version number,
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
19 'parse' raises a ValueError exception
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
20 * the class constructor takes an optional string argument which,
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
21 if supplied, is passed to 'parse'
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
22 * __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
23 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
24 version number instance)
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
25 * __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
26 * __cmp__ compares the current instance with either another instance
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
27 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
28 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
29 """
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
30
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
31 import string, re
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
32 from types import StringType
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
33
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
34 class Version:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
35 """Abstract base class for version numbering classes. Just provides
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
36 constructor (__init__) and reproducer (__repr__), because those
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
37 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
38 """
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
39
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
40 def __init__ (self, vstring=None):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
41 if vstring:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
42 self.parse(vstring)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
43
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
44 def __repr__ (self):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
45 return "%s ('%s')" % (self.__class__.__name__, str(self))
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
46
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
47
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
48 # Interface for version-number classes -- must be implemented
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
49 # by the following classes (the concrete ones -- Version should
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
50 # be treated as an abstract class).
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
51 # __init__ (string) - create and take same action as 'parse'
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
52 # (string parameter is optional)
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
53 # parse (string) - convert a string representation to whatever
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
54 # internal representation is appropriate for
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
55 # this style of version numbering
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
56 # __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
57 # (if not identical to) the string supplied to parse
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
58 # __repr__ (self) - generate Python code to recreate
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
59 # the instance
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
60 # __cmp__ (self, other) - compare two version numbers ('other' may
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
61 # be an unparsed version string, or another
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
62 # instance of your version class)
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
63
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 class StrictVersion (Version):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
66
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
67 """Version numbering for anal retentives and software idealists.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
68 Implements the standard interface for version number classes as
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
69 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
70 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
71 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
72 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
73 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
74 be deemed earlier (lesser) than one without.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
75
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
76 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
77 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
78
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
79 0.4 0.4.0 (these two are equivalent)
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
80 0.4.1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
81 0.5a1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
82 0.5b3
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
83 0.5
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
84 0.9.6
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
85 1.0
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
86 1.0.4a3
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
87 1.0.4b1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
88 1.0.4
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 The following are examples of invalid version numbers:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
91
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
92 1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
93 2.7.2.2
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
94 1.3.a4
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
95 1.3pl1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
96 1.3c4
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
97
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
98 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
99 in the distutils documentation.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
100 """
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
101
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
102 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
103 re.VERBOSE)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
104
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
105
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
106 def parse (self, vstring):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
107 match = self.version_re.match(vstring)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
108 if not match:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
109 raise ValueError, "invalid version number '%s'" % vstring
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
110
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
111 (major, minor, patch, prerelease, prerelease_num) = \
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
112 match.group(1, 2, 4, 5, 6)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
113
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
114 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
115 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
116 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
117 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
118
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
119 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
120 self.prerelease = (prerelease[0], string.atoi(prerelease_num))
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
121 else:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
122 self.prerelease = None
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
123
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
124
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
125 def __str__ (self):
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
126
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
127 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
128 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
129 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
130 vstring = string.join(map(str, self.version), '.')
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
131
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
132 if self.prerelease:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
133 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
134
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
135 return vstring
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
136
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
137
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
138 def __cmp__ (self, other):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
139 if isinstance(other, StringType):
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
140 other = StrictVersion(other)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
141
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
142 compare = cmp(self.version, other.version)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
143 if (compare == 0): # have to compare prerelease
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
144
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
145 # case 1: neither has prerelease; they're equal
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
146 # 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
147 # 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
148 # case 4: both have prerelease: must compare them!
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
149
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
150 if (not self.prerelease and not other.prerelease):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
151 return 0
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
152 elif (self.prerelease and not 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 (not self.prerelease and other.prerelease):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
155 return 1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
156 elif (self.prerelease and other.prerelease):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
157 return cmp(self.prerelease, other.prerelease)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
158
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
159 else: # numeric versions don't match --
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
160 return compare # prerelease stuff doesn't matter
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
161
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 # end class StrictVersion
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
164
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
165
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
166 # The rules according to Greg Stein:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
167 # 1) a version number has 1 or more numbers separate by a period or by
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
168 # 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
169 # left-to-right to determine an ordering.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
170 # 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
171 # compared lexicographically
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
172 # 3) recognize the numeric components may have leading zeroes
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
173 #
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
174 # 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
175 # 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
176 # 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
177 # 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
178 # 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
179 # 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
180 # 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
181 # 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
182 # the most common purpose seems to be:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
183 # - indicating a "pre-release" version
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
184 # ('alpha', 'beta', 'a', 'b', 'pre', 'p')
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
185 # - indicating a post-release patch ('p', 'pl', 'patch')
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
186 # 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
187 # no way to know what a programmer means without asking him.
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
188 #
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
189 # 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
190 # 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
191 # 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
192 # 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
193 # an appended letter sequence implies something "post-release":
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
194 # 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
195 #
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
196 # 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
197 # 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
198 # "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
199 # implemented here, this just isn't so.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
200 #
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
201 # 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
202 # 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
203 # 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
204 # 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
205 # (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
206 # particular flavour of bondage and discipline provided by StrictVersion
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
207 # 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
208 # version numbering scheme to its domination. The free-thinking
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
209 # 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
210 # to be done to accommodate them.
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
211 #
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
212 # 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
213 # lets almost anything slide (syntactically), and makes some heuristic
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
214 # 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
215 # 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
216 # 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
217 # 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
218 # 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
219 # think I'm smart enough to do it right though.
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
220 #
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
221 # 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
222 # ../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
223 # "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
224 # 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
225 # complicated, hairy expectations for real-world version numbers. It
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
226 # 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
227 # 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
228 # 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
229
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
230 class LooseVersion (Version):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
231
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
232 """Version numbering for anarchists and software realists.
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
233 Implements the standard interface for version number classes as
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
234 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
235 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
236 version numbers, the numeric components will be compared
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
237 numerically, and the alphabetic components lexically. The following
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
238 are all valid version numbers, in no particular order:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
239
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
240 1.5.1
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
241 1.5.2b2
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
242 161
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
243 3.10a
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
244 8.02
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
245 3.4j
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
246 1996.07.12
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
247 3.2.pl0
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
248 3.1.1.6
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
249 2g6
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
250 11g
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
251 0.960923
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
252 2.2beta29
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
253 1.13++
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
254 5.5.kw
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
255 2.0b1pl0
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
256
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
257 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
258 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
259 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
260 of "want").
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
261 """
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 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
264
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
265 def __init__ (self, vstring=None):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
266 if vstring:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
267 self.parse(vstring)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
268
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
269
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
270 def parse (self, vstring):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
271 # 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
272 # 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
273 # use by __str__
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
274 self.vstring = vstring
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
275 components = filter(lambda x: x and x != '.',
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
276 self.component_re.split(vstring))
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
277 for i in range(len(components)):
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
278 try:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
279 components[i] = int(components[i])
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
280 except ValueError:
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
281 pass
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 self.version = components
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
284
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
285
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
286 def __str__ (self):
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
287 return self.vstring
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
288
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
289
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
290 def __repr__ (self):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
291 return "LooseVersion ('%s')" % str(self)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
292
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
293
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
294 def __cmp__ (self, other):
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
295 if isinstance(other, StringType):
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
296 other = LooseVersion(other)
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
297
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 13050
diff changeset
298 return cmp(self.version, other.version)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 17129
diff changeset
299
9654
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
300
917357c12552 Initial checkin of distutils source files.
Greg Ward <gward@python.net>
parents:
diff changeset
301 # end class LooseVersion