Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version.py 12KB

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