PageRenderTime 2305ms CodeModel.GetById 21ms RepoModel.GetById 4ms app.codeStats 1ms

/lib-python/2.7/distutils/version.py

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