diff options
-rw-r--r-- | .pre-commit-config.yaml | 2 | ||||
-rw-r--r-- | doc/source/user/using.rst | 4 | ||||
-rw-r--r-- | pbr/version.py | 41 | ||||
-rw-r--r-- | pyproject.toml.future | 2 |
4 files changed, 41 insertions, 8 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2f6ad7b..319697b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ default_language_version: repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: ebc15addedad713c86ef18ae9632c88e187dd0af # v3.1.0 + rev: 9136088a246768144165fcc3ecc3d31bb686920a # v3.3.0 hooks: - id: trailing-whitespace # Replaces or checks mixed line ending diff --git a/doc/source/user/using.rst b/doc/source/user/using.rst index 21aa99b..11cdbf2 100644 --- a/doc/source/user/using.rst +++ b/doc/source/user/using.rst @@ -41,12 +41,12 @@ PBR can be configured as a PEP517 build-system in ``pyproject.toml``. This currently continues to rely on setuptools which means you need the above ``setup.py`` file to be present. The main benefits to using a ``pyproject.toml`` file with PBR are that you can control the versions of -PBR, setuptools, and wheel that are used avoiding easy_install invocation. +PBR and setuptools that are used avoiding easy_install invocation. Your build-system block in ``pyproject.toml`` will need to look something like this:: [build-system] - requires = ["pbr>=5.7.0", "setuptools>=36.6.0", "wheel"] + requires = ["pbr>=5.7.0", "setuptools>=36.6.0"] build-backend = "pbr.build" diff --git a/pbr/version.py b/pbr/version.py index 46c6020..658928e 100644 --- a/pbr/version.py +++ b/pbr/version.py @@ -15,13 +15,19 @@ # under the License. """ -Utilities for consuming the version from pkg_resources. +Utilities for consuming the version from importlib-metadata. """ import itertools import operator import sys +try: + import importlib_metadata + use_importlib = True +except ImportError: + use_importlib = False + def _is_int(string): try: @@ -431,12 +437,15 @@ class VersionInfo(object): """Obtain a version from pkg_resources or setup-time logic if missing. This will try to get the version of the package from the pkg_resources + This will try to get the version of the package from the record associated with the package, and if there is no such record + importlib_metadata record associated with the package, and if there falls back to the logic sdist would use. + + is no such record falls back to the logic sdist would use. """ - # Lazy import because pkg_resources is costly to import so defer until - # we absolutely need it. import pkg_resources + try: requirement = pkg_resources.Requirement.parse(self.package) provider = pkg_resources.get_provider(requirement) @@ -447,6 +456,25 @@ class VersionInfo(object): # installed into anything. Revert to setup-time logic. from pbr import packaging result_string = packaging.get_version(self.package) + + return SemanticVersion.from_pip_string(result_string) + + def _get_version_from_importlib_metadata(self): + """Obtain a version from importlib or setup-time logic if missing. + + This will try to get the version of the package from the + importlib_metadata record associated with the package, and if there + is no such record falls back to the logic sdist would use. + """ + try: + distribution = importlib_metadata.distribution(self.package) + result_string = distribution.version + except importlib_metadata.PackageNotFoundError: + # The most likely cause for this is running tests in a tree + # produced from a tarball where the package itself has not been + # installed into anything. Revert to setup-time logic. + from pbr import packaging + result_string = packaging.get_version(self.package) return SemanticVersion.from_pip_string(result_string) def release_string(self): @@ -459,7 +487,12 @@ class VersionInfo(object): def semantic_version(self): """Return the SemanticVersion object for this version.""" if self._semantic is None: - self._semantic = self._get_version_from_pkg_resources() + # TODO(damami): simplify this once Python 3.8 is the oldest + # we support + if use_importlib: + self._semantic = self._get_version_from_importlib_metadata() + else: + self._semantic = self._get_version_from_pkg_resources() return self._semantic def version_string(self): diff --git a/pyproject.toml.future b/pyproject.toml.future index 1989dfd..e5ae7bc 100644 --- a/pyproject.toml.future +++ b/pyproject.toml.future @@ -4,6 +4,6 @@ # once we are more confident it works generally. [build-system] -requires = ["setuptools>=36.6.0", "wheel"] +requires = ["setuptools>=36.6.0"] build-backend = "pbr.build" backend-path = ["."] |