version.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # Copyright 2012 OpenStack Foundation
  2. # Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """
  16. Utilities for consuming the version from importlib-metadata.
  17. """
  18. import itertools
  19. import operator
  20. import sys
  21. # TODO(stephenfin): Remove this once we drop support for Python < 3.8
  22. if sys.version_info >= (3, 8):
  23. from importlib import metadata as importlib_metadata
  24. use_importlib = True
  25. else:
  26. try:
  27. import importlib_metadata
  28. use_importlib = True
  29. except ImportError:
  30. use_importlib = False
  31. def _is_int(string):
  32. try:
  33. int(string)
  34. return True
  35. except ValueError:
  36. return False
  37. class SemanticVersion(object):
  38. """A pure semantic version independent of serialisation.
  39. See the pbr doc 'semver' for details on the semantics.
  40. """
  41. def __init__(
  42. self, major, minor=0, patch=0, prerelease_type=None,
  43. prerelease=None, dev_count=None):
  44. """Create a SemanticVersion.
  45. :param major: Major component of the version.
  46. :param minor: Minor component of the version. Defaults to 0.
  47. :param patch: Patch level component. Defaults to 0.
  48. :param prerelease_type: What sort of prerelease version this is -
  49. one of a(alpha), b(beta) or rc(release candidate).
  50. :param prerelease: For prerelease versions, what number prerelease.
  51. Defaults to 0.
  52. :param dev_count: How many commits since the last release.
  53. """
  54. self._major = major
  55. self._minor = minor
  56. self._patch = patch
  57. self._prerelease_type = prerelease_type
  58. self._prerelease = prerelease
  59. if self._prerelease_type and not self._prerelease:
  60. self._prerelease = 0
  61. self._dev_count = dev_count or 0 # Normalise 0 to None.
  62. def __eq__(self, other):
  63. if not isinstance(other, SemanticVersion):
  64. return False
  65. return self.__dict__ == other.__dict__
  66. def __hash__(self):
  67. return sum(map(hash, self.__dict__.values()))
  68. def _sort_key(self):
  69. """Return a key for sorting SemanticVersion's on."""
  70. # key things:
  71. # - final is after rc's, so we make that a/b/rc/z
  72. # - dev==None is after all other devs, so we use sys.maxsize there.
  73. # - unqualified dev releases come before any pre-releases.
  74. # So we do:
  75. # (major, minor, patch) - gets the major grouping.
  76. # (0|1) unqualified dev flag
  77. # (a/b/rc/z) - release segment grouping
  78. # pre-release level
  79. # dev count, maxsize for releases.
  80. rc_lookup = {'a': 'a', 'b': 'b', 'rc': 'rc', None: 'z'}
  81. if self._dev_count and not self._prerelease_type:
  82. uq_dev = 0
  83. else:
  84. uq_dev = 1
  85. return (
  86. self._major, self._minor, self._patch,
  87. uq_dev,
  88. rc_lookup[self._prerelease_type], self._prerelease,
  89. self._dev_count or sys.maxsize)
  90. def __lt__(self, other):
  91. """Compare self and other, another Semantic Version."""
  92. # NB(lifeless) this could perhaps be rewritten as
  93. # lt (tuple_of_one, tuple_of_other) with a single check for
  94. # the typeerror corner cases - that would likely be faster
  95. # if this ever becomes performance sensitive.
  96. if not isinstance(other, SemanticVersion):
  97. raise TypeError("ordering to non-SemanticVersion is undefined")
  98. return self._sort_key() < other._sort_key()
  99. def __le__(self, other):
  100. return self == other or self < other
  101. def __ge__(self, other):
  102. return not self < other
  103. def __gt__(self, other):
  104. return not self <= other
  105. def __ne__(self, other):
  106. return not self == other
  107. def __repr__(self):
  108. return "pbr.version.SemanticVersion(%s)" % self.release_string()
  109. @classmethod
  110. def from_pip_string(klass, version_string):
  111. """Create a SemanticVersion from a pip version string.
  112. This method will parse a version like 1.3.0 into a SemanticVersion.
  113. This method is responsible for accepting any version string that any
  114. older version of pbr ever created.
  115. Therefore: versions like 1.3.0a1 versions are handled, parsed into a
  116. canonical form and then output - resulting in 1.3.0.0a1.
  117. Pre pbr-semver dev versions like 0.10.1.3.g83bef74 will be parsed but
  118. output as 0.10.1.dev3.g83bef74.
  119. :raises ValueError: Never tagged versions sdisted by old pbr result in
  120. just the git hash, e.g. '1234567' which poses a substantial problem
  121. since they collide with the semver versions when all the digits are
  122. numerals. Such versions will result in a ValueError being thrown if
  123. any non-numeric digits are present. They are an exception to the
  124. general case of accepting anything we ever output, since they were
  125. never intended and would permanently mess up versions on PyPI if
  126. ever released - we're treating that as a critical bug that we ever
  127. made them and have stopped doing that.
  128. """
  129. try:
  130. return klass._from_pip_string_unsafe(version_string)
  131. except IndexError:
  132. raise ValueError("Invalid version %r" % version_string)
  133. @classmethod
  134. def _from_pip_string_unsafe(klass, version_string):
  135. # Versions need to start numerically, ignore if not
  136. version_string = version_string.lstrip('vV')
  137. if not version_string[:1].isdigit():
  138. raise ValueError("Invalid version %r" % version_string)
  139. input_components = version_string.split('.')
  140. # decimals first (keep pre-release and dev/hashes to the right)
  141. components = [c for c in input_components if c.isdigit()]
  142. digit_len = len(components)
  143. if digit_len == 0:
  144. raise ValueError("Invalid version %r" % version_string)
  145. elif digit_len < 3:
  146. if (digit_len < len(input_components) and
  147. input_components[digit_len][0].isdigit()):
  148. # Handle X.YaZ - Y is a digit not a leadin to pre-release.
  149. mixed_component = input_components[digit_len]
  150. last_component = ''.join(itertools.takewhile(
  151. lambda x: x.isdigit(), mixed_component))
  152. components.append(last_component)
  153. input_components[digit_len:digit_len + 1] = [
  154. last_component, mixed_component[len(last_component):]]
  155. digit_len += 1
  156. components.extend([0] * (3 - digit_len))
  157. components.extend(input_components[digit_len:])
  158. major = int(components[0])
  159. minor = int(components[1])
  160. dev_count = None
  161. post_count = None
  162. prerelease_type = None
  163. prerelease = None
  164. def _parse_type(segment):
  165. # Discard leading digits (the 0 in 0a1)
  166. isdigit = operator.methodcaller('isdigit')
  167. segment = ''.join(itertools.dropwhile(isdigit, segment))
  168. isalpha = operator.methodcaller('isalpha')
  169. prerelease_type = ''.join(itertools.takewhile(isalpha, segment))
  170. prerelease = segment[len(prerelease_type)::]
  171. return prerelease_type, int(prerelease)
  172. if _is_int(components[2]):
  173. patch = int(components[2])
  174. else:
  175. # legacy version e.g. 1.2.0a1 (canonical is 1.2.0.0a1)
  176. # or 1.2.dev4.g1234 or 1.2.b4
  177. patch = 0
  178. components[2:2] = [0]
  179. remainder = components[3:]
  180. remainder_starts_with_int = False
  181. try:
  182. if remainder and int(remainder[0]):
  183. remainder_starts_with_int = True
  184. except ValueError:
  185. pass
  186. if remainder_starts_with_int:
  187. # old dev format - 0.1.2.3.g1234
  188. dev_count = int(remainder[0])
  189. else:
  190. if remainder and (remainder[0][0] == '0' or
  191. remainder[0][0] in ('a', 'b', 'r')):
  192. # Current RC/beta layout
  193. prerelease_type, prerelease = _parse_type(remainder[0])
  194. remainder = remainder[1:]
  195. while remainder:
  196. component = remainder[0]
  197. if component.startswith('dev'):
  198. dev_count = int(component[3:])
  199. elif component.startswith('post'):
  200. dev_count = None
  201. post_count = int(component[4:])
  202. else:
  203. raise ValueError(
  204. 'Unknown remainder %r in %r'
  205. % (remainder, version_string))
  206. remainder = remainder[1:]
  207. result = SemanticVersion(
  208. major, minor, patch, prerelease_type=prerelease_type,
  209. prerelease=prerelease, dev_count=dev_count)
  210. if post_count:
  211. if dev_count:
  212. raise ValueError(
  213. 'Cannot combine postN and devN - no mapping in %r'
  214. % (version_string,))
  215. result = result.increment().to_dev(post_count)
  216. return result
  217. def brief_string(self):
  218. """Return the short version minus any alpha/beta tags."""
  219. return "%s.%s.%s" % (self._major, self._minor, self._patch)
  220. def debian_string(self):
  221. """Return the version number to use when building a debian package.
  222. This translates the PEP440/semver precedence rules into Debian version
  223. sorting operators.
  224. """
  225. return self._long_version("~")
  226. def decrement(self):
  227. """Return a decremented SemanticVersion.
  228. Decrementing versions doesn't make a lot of sense - this method only
  229. exists to support rendering of pre-release versions strings into
  230. serialisations (such as rpm) with no sort-before operator.
  231. The 9999 magic version component is from the spec on this - pbr-semver.
  232. :return: A new SemanticVersion object.
  233. """
  234. if self._patch:
  235. new_patch = self._patch - 1
  236. new_minor = self._minor
  237. new_major = self._major
  238. else:
  239. new_patch = 9999
  240. if self._minor:
  241. new_minor = self._minor - 1
  242. new_major = self._major
  243. else:
  244. new_minor = 9999
  245. if self._major:
  246. new_major = self._major - 1
  247. else:
  248. new_major = 0
  249. return SemanticVersion(
  250. new_major, new_minor, new_patch)
  251. def increment(self, minor=False, major=False):
  252. """Return an incremented SemanticVersion.
  253. The default behaviour is to perform a patch level increment. When
  254. incrementing a prerelease version, the patch level is not changed
  255. - the prerelease serial is changed (e.g. beta 0 -> beta 1).
  256. Incrementing non-pre-release versions will not introduce pre-release
  257. versions - except when doing a patch incremental to a pre-release
  258. version the new version will only consist of major/minor/patch.
  259. :param minor: Increment the minor version.
  260. :param major: Increment the major version.
  261. :return: A new SemanticVersion object.
  262. """
  263. if self._prerelease_type:
  264. new_prerelease_type = self._prerelease_type
  265. new_prerelease = self._prerelease + 1
  266. new_patch = self._patch
  267. else:
  268. new_prerelease_type = None
  269. new_prerelease = None
  270. new_patch = self._patch + 1
  271. if minor:
  272. new_minor = self._minor + 1
  273. new_patch = 0
  274. new_prerelease_type = None
  275. new_prerelease = None
  276. else:
  277. new_minor = self._minor
  278. if major:
  279. new_major = self._major + 1
  280. new_minor = 0
  281. new_patch = 0
  282. new_prerelease_type = None
  283. new_prerelease = None
  284. else:
  285. new_major = self._major
  286. return SemanticVersion(
  287. new_major, new_minor, new_patch,
  288. new_prerelease_type, new_prerelease)
  289. def _long_version(self, pre_separator, rc_marker=""):
  290. """Construct a long string version of this semver.
  291. :param pre_separator: What separator to use between components
  292. that sort before rather than after. If None, use . and lower the
  293. version number of the component to preserve sorting. (Used for
  294. rpm support)
  295. """
  296. if ((self._prerelease_type or self._dev_count) and
  297. pre_separator is None):
  298. segments = [self.decrement().brief_string()]
  299. pre_separator = "."
  300. else:
  301. segments = [self.brief_string()]
  302. if self._prerelease_type:
  303. segments.append(
  304. "%s%s%s%s" % (pre_separator, rc_marker, self._prerelease_type,
  305. self._prerelease))
  306. if self._dev_count:
  307. if not self._prerelease_type:
  308. segments.append(pre_separator)
  309. else:
  310. segments.append('.')
  311. segments.append('dev')
  312. segments.append(self._dev_count)
  313. return "".join(str(s) for s in segments)
  314. def release_string(self):
  315. """Return the full version of the package.
  316. This including suffixes indicating VCS status.
  317. """
  318. return self._long_version(".", "0")
  319. def rpm_string(self):
  320. """Return the version number to use when building an RPM package.
  321. This translates the PEP440/semver precedence rules into RPM version
  322. sorting operators. Because RPM has no sort-before operator (such as the
  323. ~ operator in dpkg), we show all prerelease versions as being versions
  324. of the release before.
  325. """
  326. return self._long_version(None)
  327. def to_dev(self, dev_count):
  328. """Return a development version of this semver.
  329. :param dev_count: The number of commits since the last release.
  330. """
  331. return SemanticVersion(
  332. self._major, self._minor, self._patch, self._prerelease_type,
  333. self._prerelease, dev_count=dev_count)
  334. def version_tuple(self):
  335. """Present the version as a version_info tuple.
  336. For documentation on version_info tuples see the Python
  337. documentation for sys.version_info.
  338. Since semver and PEP-440 represent overlapping but not subsets of
  339. versions, we have to have some heuristic / mapping rules, and have
  340. extended the releaselevel field to have alphadev, betadev and
  341. candidatedev values. When they are present the dev count is used
  342. to provide the serial.
  343. - a/b/rc take precedence.
  344. - if there is no pre-release version the dev version is used.
  345. - serial is taken from the dev/a/b/c component.
  346. - final non-dev versions never get serials.
  347. """
  348. segments = [self._major, self._minor, self._patch]
  349. if self._prerelease_type:
  350. type_map = {('a', False): 'alpha',
  351. ('b', False): 'beta',
  352. ('rc', False): 'candidate',
  353. ('a', True): 'alphadev',
  354. ('b', True): 'betadev',
  355. ('rc', True): 'candidatedev',
  356. }
  357. segments.append(
  358. type_map[(self._prerelease_type, bool(self._dev_count))])
  359. segments.append(self._dev_count or self._prerelease)
  360. elif self._dev_count:
  361. segments.append('dev')
  362. segments.append(self._dev_count - 1)
  363. else:
  364. segments.append('final')
  365. segments.append(0)
  366. return tuple(segments)
  367. class VersionInfo(object):
  368. def __init__(self, package):
  369. """Object that understands versioning for a package
  370. :param package: name of the python package, such as glance, or
  371. python-glanceclient
  372. """
  373. self.package = package
  374. self.version = None
  375. self._cached_version = None
  376. self._semantic = None
  377. def __str__(self):
  378. """Make the VersionInfo object behave like a string."""
  379. return self.version_string()
  380. def __repr__(self):
  381. """Include the name."""
  382. return "pbr.version.VersionInfo(%s:%s)" % (
  383. self.package, self.version_string())
  384. def _get_version_from_pkg_resources(self):
  385. """Obtain a version from pkg_resources or setup-time logic if missing.
  386. This will try to get the version of the package from the pkg_resources
  387. This will try to get the version of the package from the
  388. record associated with the package, and if there is no such record
  389. importlib_metadata record associated with the package, and if there
  390. falls back to the logic sdist would use.
  391. is no such record falls back to the logic sdist would use.
  392. """
  393. import pkg_resources
  394. try:
  395. requirement = pkg_resources.Requirement.parse(self.package)
  396. provider = pkg_resources.get_provider(requirement)
  397. result_string = provider.version
  398. except pkg_resources.DistributionNotFound:
  399. # The most likely cause for this is running tests in a tree
  400. # produced from a tarball where the package itself has not been
  401. # installed into anything. Revert to setup-time logic.
  402. from pbr import packaging
  403. result_string = packaging.get_version(self.package)
  404. return SemanticVersion.from_pip_string(result_string)
  405. def _get_version_from_importlib_metadata(self):
  406. """Obtain a version from importlib or setup-time logic if missing.
  407. This will try to get the version of the package from the
  408. importlib_metadata record associated with the package, and if there
  409. is no such record falls back to the logic sdist would use.
  410. """
  411. try:
  412. distribution = importlib_metadata.distribution(self.package)
  413. result_string = distribution.version
  414. except importlib_metadata.PackageNotFoundError:
  415. # The most likely cause for this is running tests in a tree
  416. # produced from a tarball where the package itself has not been
  417. # installed into anything. Revert to setup-time logic.
  418. from pbr import packaging
  419. result_string = packaging.get_version(self.package)
  420. return SemanticVersion.from_pip_string(result_string)
  421. def release_string(self):
  422. """Return the full version of the package.
  423. This including suffixes indicating VCS status.
  424. """
  425. return self.semantic_version().release_string()
  426. def semantic_version(self):
  427. """Return the SemanticVersion object for this version."""
  428. if self._semantic is None:
  429. # TODO(damami): simplify this once Python 3.8 is the oldest
  430. # we support
  431. if use_importlib:
  432. self._semantic = self._get_version_from_importlib_metadata()
  433. else:
  434. self._semantic = self._get_version_from_pkg_resources()
  435. return self._semantic
  436. def version_string(self):
  437. """Return the short version minus any alpha/beta tags."""
  438. return self.semantic_version().brief_string()
  439. # Compatibility functions
  440. canonical_version_string = version_string
  441. version_string_with_vcs = release_string
  442. def cached_version_string(self, prefix=""):
  443. """Return a cached version string.
  444. This will return a cached version string if one is already cached,
  445. irrespective of prefix. If none is cached, one will be created with
  446. prefix and then cached and returned.
  447. """
  448. if not self._cached_version:
  449. self._cached_version = "%s%s" % (prefix,
  450. self.version_string())
  451. return self._cached_version