From 0447700f90dbc3fdd5e16e5f768c3f16db6763e0 Mon Sep 17 00:00:00 2001 From: Jonathon Reinhart Date: Wed, 3 Jan 2024 00:31:10 -0500 Subject: [PATCH] scuba: Use importlib.metadata over pkg_resources for getting version (#247) pkg_resources is deprecated: https://setuptools.pypa.io/en/latest/pkg_resources.html This prefers importlib.metadata (introduced in Python 3.8) and uses the backport importlib_metadata for Python 3.7 (to be removed in #242). Fixes #245 --- CHANGELOG.md | 5 +++++ VERSIONING.md | 3 ++- pyproject.toml | 4 ++++ requirements.txt | 1 - scuba/version.py | 16 +++++++--------- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03de2bf4..9bd7af4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Changed +- Removed use of deprecated `pkg_resources` (#247) + + ## [2.12.0] - 2023-09-15 ### Added - Enable the use of relative paths in a volume hostpath (#227) diff --git a/VERSIONING.md b/VERSIONING.md index b107f5a9..45f0f8d8 100644 --- a/VERSIONING.md +++ b/VERSIONING.md @@ -52,4 +52,5 @@ versioning scheme. Consider a base version of `1.2.3`: created via `python setup.py sdist`. In this case, the version comes from the egg info generated by setuptools. -In any case, when Scuba is installed, the version comes from `pkg_resources`. +In any case, when Scuba is installed, the version comes from +`importlib.metadata`. diff --git a/pyproject.toml b/pyproject.toml index 9bdfc6a4..0c3a8354 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,8 @@ classifiers = [ ] dependencies = [ "PyYAML", + # TODO(#242): Remove when Python 3.7 support is removed. + "importlib_metadata; python_version<'3.8'", ] description = "Simplify use of Docker containers for building software" keywords = ["docker"] @@ -42,6 +44,8 @@ changelog = "https://github.com/JonathonReinhart/scuba/blob/main/CHANGELOG.md" requires = [ "wheel", "setuptools >= 42.0.0", + # TODO(#242): Remove when Python 3.7 support is removed. + "importlib_metadata; python_version<'3.8'", ] diff --git a/requirements.txt b/requirements.txt index 5722978c..88207350 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,6 @@ PyYAML pytest pytest-cov types-PyYAML -types-pkg-resources coverage black~=23.0 # Must match .github/workflows/black.yml -r docs/requirements.txt diff --git a/scuba/version.py b/scuba/version.py index f606d312..44f607ed 100644 --- a/scuba/version.py +++ b/scuba/version.py @@ -67,19 +67,17 @@ def get_version() -> str: if not git_archive_rev.startswith("$"): return f"{BASE_VERSION}+g{git_archive_rev}" - # Package resource # Otherwise, we're either installed (e.g. via pip), or running from # an 'sdist' source distribution, and have a local PKG_INFO file. - import pkg_resources - try: - return pkg_resources.get_distribution(DIST_SPEC).version - except pkg_resources.DistributionNotFound: - pass + # TODO(#242): Remove backport when Python 3.7 support is removed. + if sys.version_info >= (3, 8): + import importlib.metadata as importlib_metadata + else: + import importlib_metadata # backport - # This shouldn't be able to happen - sys.stderr.write("WARNING: Failed to determine version!\n") - return BASE_VERSION + # Can raise importlib.metadata.PackageNotFoundError + return importlib_metadata.version(DIST_SPEC) __version__ = get_version()