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.

__init__.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Versions for Python packages.
  5. See L{Version}.
  6. """
  7. from __future__ import division, absolute_import
  8. import sys
  9. import warnings
  10. from typing import TYPE_CHECKING, Any, TypeVar, Union, Optional, Dict
  11. #
  12. # Compat functions
  13. #
  14. _T = TypeVar("_T", contravariant=True)
  15. if TYPE_CHECKING:
  16. from typing_extensions import Literal
  17. from distutils.dist import Distribution as _Distribution
  18. else:
  19. _Distribution = object
  20. if sys.version_info > (3,):
  21. def _cmp(a, b): # type: (Any, Any) -> int
  22. """
  23. Compare two objects.
  24. Returns a negative number if C{a < b}, zero if they are equal, and a
  25. positive number if C{a > b}.
  26. """
  27. if a < b:
  28. return -1
  29. elif a == b:
  30. return 0
  31. else:
  32. return 1
  33. else:
  34. _cmp = cmp # noqa: F821
  35. #
  36. # Versioning
  37. #
  38. class _Inf(object):
  39. """
  40. An object that is bigger than all other objects.
  41. """
  42. def __cmp__(self, other): # type: (object) -> int
  43. """
  44. @param other: Another object.
  45. @type other: any
  46. @return: 0 if other is inf, 1 otherwise.
  47. @rtype: C{int}
  48. """
  49. if other is _inf:
  50. return 0
  51. return 1
  52. if sys.version_info >= (3,):
  53. def __lt__(self, other): # type: (object) -> bool
  54. return self.__cmp__(other) < 0
  55. def __le__(self, other): # type: (object) -> bool
  56. return self.__cmp__(other) <= 0
  57. def __gt__(self, other): # type: (object) -> bool
  58. return self.__cmp__(other) > 0
  59. def __ge__(self, other): # type: (object) -> bool
  60. return self.__cmp__(other) >= 0
  61. _inf = _Inf()
  62. class IncomparableVersions(TypeError):
  63. """
  64. Two versions could not be compared.
  65. """
  66. class Version(object):
  67. """
  68. An encapsulation of a version for a project, with support for outputting
  69. PEP-440 compatible version strings.
  70. This class supports the standard major.minor.micro[rcN] scheme of
  71. versioning.
  72. """
  73. def __init__(
  74. self,
  75. package, # type: str
  76. major, # type: Union[Literal["NEXT"], int]
  77. minor, # type: int
  78. micro, # type: int
  79. release_candidate=None, # type: Optional[int]
  80. prerelease=None, # type: Optional[int]
  81. post=None, # type: Optional[int]
  82. dev=None, # type: Optional[int]
  83. ):
  84. """
  85. @param package: Name of the package that this is a version of.
  86. @type package: C{str}
  87. @param major: The major version number.
  88. @type major: C{int} or C{str} (for the "NEXT" symbol)
  89. @param minor: The minor version number.
  90. @type minor: C{int}
  91. @param micro: The micro version number.
  92. @type micro: C{int}
  93. @param release_candidate: The release candidate number.
  94. @type release_candidate: C{int}
  95. @param prerelease: The prerelease number. (Deprecated)
  96. @type prerelease: C{int}
  97. @param post: The postrelease number.
  98. @type post: C{int}
  99. @param dev: The development release number.
  100. @type dev: C{int}
  101. """
  102. if release_candidate and prerelease:
  103. raise ValueError("Please only return one of these.")
  104. elif prerelease and not release_candidate:
  105. release_candidate = prerelease
  106. warnings.warn(
  107. "Passing prerelease to incremental.Version was "
  108. "deprecated in Incremental 16.9.0. Please pass "
  109. "release_candidate instead.",
  110. DeprecationWarning,
  111. stacklevel=2,
  112. )
  113. if major == "NEXT":
  114. if minor or micro or release_candidate or post or dev:
  115. raise ValueError(
  116. "When using NEXT, all other values except Package must be 0."
  117. )
  118. self.package = package
  119. self.major = major
  120. self.minor = minor
  121. self.micro = micro
  122. self.release_candidate = release_candidate
  123. self.post = post
  124. self.dev = dev
  125. @property
  126. def prerelease(self): # type: () -> Optional[int]
  127. warnings.warn(
  128. "Accessing incremental.Version.prerelease was "
  129. "deprecated in Incremental 16.9.0. Use "
  130. "Version.release_candidate instead.",
  131. DeprecationWarning,
  132. stacklevel=2,
  133. ),
  134. return self.release_candidate
  135. def public(self): # type: () -> str
  136. """
  137. Return a PEP440-compatible "public" representation of this L{Version}.
  138. Examples:
  139. - 14.4.0
  140. - 1.2.3rc1
  141. - 14.2.1rc1dev9
  142. - 16.04.0dev0
  143. """
  144. if self.major == "NEXT":
  145. return self.major
  146. if self.release_candidate is None:
  147. rc = ""
  148. else:
  149. rc = ".rc%s" % (self.release_candidate,)
  150. if self.post is None:
  151. post = ""
  152. else:
  153. post = ".post%s" % (self.post,)
  154. if self.dev is None:
  155. dev = ""
  156. else:
  157. dev = ".dev%s" % (self.dev,)
  158. return "%r.%d.%d%s%s%s" % (self.major, self.minor, self.micro, rc, post, dev)
  159. base = public
  160. short = public
  161. local = public
  162. def __repr__(self): # type: () -> str
  163. if self.release_candidate is None:
  164. release_candidate = ""
  165. else:
  166. release_candidate = ", release_candidate=%r" % (self.release_candidate,)
  167. if self.post is None:
  168. post = ""
  169. else:
  170. post = ", post=%r" % (self.post,)
  171. if self.dev is None:
  172. dev = ""
  173. else:
  174. dev = ", dev=%r" % (self.dev,)
  175. return "%s(%r, %r, %d, %d%s%s%s)" % (
  176. self.__class__.__name__,
  177. self.package,
  178. self.major,
  179. self.minor,
  180. self.micro,
  181. release_candidate,
  182. post,
  183. dev,
  184. )
  185. def __str__(self): # type: () -> str
  186. return "[%s, version %s]" % (self.package, self.short())
  187. def __cmp__(self, other): # type: (Version) -> int
  188. """
  189. Compare two versions, considering major versions, minor versions, micro
  190. versions, then release candidates, then postreleases, then dev
  191. releases. Package names are case insensitive.
  192. A version with a release candidate is always less than a version
  193. without a release candidate. If both versions have release candidates,
  194. they will be included in the comparison.
  195. Likewise, a version with a dev release is always less than a version
  196. without a dev release. If both versions have dev releases, they will
  197. be included in the comparison.
  198. @param other: Another version.
  199. @type other: L{Version}
  200. @return: NotImplemented when the other object is not a Version, or one
  201. of -1, 0, or 1.
  202. @raise IncomparableVersions: when the package names of the versions
  203. differ.
  204. """
  205. if not isinstance(other, self.__class__):
  206. return NotImplemented
  207. if self.package.lower() != other.package.lower():
  208. raise IncomparableVersions("%r != %r" % (self.package, other.package))
  209. if self.major == "NEXT":
  210. major = _inf # type: Union[int, _Inf]
  211. else:
  212. major = self.major
  213. if self.release_candidate is None:
  214. release_candidate = _inf # type: Union[int, _Inf]
  215. else:
  216. release_candidate = self.release_candidate
  217. if self.post is None:
  218. post = -1
  219. else:
  220. post = self.post
  221. if self.dev is None:
  222. dev = _inf # type: Union[int, _Inf]
  223. else:
  224. dev = self.dev
  225. if other.major == "NEXT":
  226. othermajor = _inf # type: Union[int, _Inf]
  227. else:
  228. othermajor = other.major
  229. if other.release_candidate is None:
  230. otherrc = _inf # type: Union[int, _Inf]
  231. else:
  232. otherrc = other.release_candidate
  233. if other.post is None:
  234. otherpost = -1
  235. else:
  236. otherpost = other.post
  237. if other.dev is None:
  238. otherdev = _inf # type: Union[int, _Inf]
  239. else:
  240. otherdev = other.dev
  241. x = _cmp(
  242. (major, self.minor, self.micro, release_candidate, post, dev),
  243. (othermajor, other.minor, other.micro, otherrc, otherpost, otherdev),
  244. )
  245. return x
  246. if sys.version_info >= (3,):
  247. def __eq__(self, other): # type: (Any) -> bool
  248. c = self.__cmp__(other)
  249. if c is NotImplemented:
  250. return c # type: ignore[return-value]
  251. return c == 0
  252. def __ne__(self, other): # type: (Any) -> bool
  253. c = self.__cmp__(other)
  254. if c is NotImplemented:
  255. return c # type: ignore[return-value]
  256. return c != 0
  257. def __lt__(self, other): # type: (Version) -> bool
  258. c = self.__cmp__(other)
  259. if c is NotImplemented:
  260. return c # type: ignore[return-value]
  261. return c < 0
  262. def __le__(self, other): # type: (Version) -> bool
  263. c = self.__cmp__(other)
  264. if c is NotImplemented:
  265. return c # type: ignore[return-value]
  266. return c <= 0
  267. def __gt__(self, other): # type: (Version) -> bool
  268. c = self.__cmp__(other)
  269. if c is NotImplemented:
  270. return c # type: ignore[return-value]
  271. return c > 0
  272. def __ge__(self, other): # type: (Version) -> bool
  273. c = self.__cmp__(other)
  274. if c is NotImplemented:
  275. return c # type: ignore[return-value]
  276. return c >= 0
  277. def getVersionString(version): # type: (Version) -> str
  278. """
  279. Get a friendly string for the given version object.
  280. @param version: A L{Version} object.
  281. @return: A string containing the package and short version number.
  282. """
  283. result = "%s %s" % (version.package, version.short())
  284. return result
  285. def _get_version(dist, keyword, value): # type: (_Distribution, object, object) -> None
  286. """
  287. Get the version from the package listed in the Distribution.
  288. """
  289. if not value:
  290. return
  291. from distutils.command import build_py
  292. sp_command = build_py.build_py(dist)
  293. sp_command.finalize_options()
  294. for item in sp_command.find_all_modules(): # type: ignore[attr-defined]
  295. if item[1] == "_version":
  296. version_file = {} # type: Dict[str, Version]
  297. with open(item[2]) as f:
  298. exec(f.read(), version_file)
  299. dist.metadata.version = version_file["__version__"].public()
  300. return None
  301. raise Exception("No _version.py found.")
  302. from ._version import __version__ # noqa: E402
  303. def _setuptools_version(): # type: () -> str
  304. return __version__.public()
  305. __all__ = ["__version__", "Version", "getVersionString"]