Skip to content

Commit

Permalink
staticx: Use importlib.metadata over pkg_resources for getting version (
Browse files Browse the repository at this point in the history
#271)

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 #264).

Fixes #266
  • Loading branch information
JonathonReinhart authored Jan 8, 2024
1 parent a4348ce commit a0080c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ classifiers = [
]
dependencies = [
"pyelftools",
# TODO(#264): Remove when Python 3.7 support is removed.
"importlib_metadata; python_version<'3.8'",
]
description = "Build static self-extracting app from dynamic executable"
license = {file = "LICENSE.txt"}
Expand All @@ -44,4 +46,6 @@ changelog = "https://github.com/JonathonReinhart/staticx/blob/main/CHANGELOG.md"
requires = [
"wheel",
"setuptools >= 42.0.0",
# TODO(#264): Remove when Python 3.7 support is removed.
"importlib_metadata; python_version<'3.8'",
]
18 changes: 9 additions & 9 deletions staticx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
PACKAGEPATH = Path(__file__).absolute().parent
PROJPATH = PACKAGEPATH.parent

DIST_SPEC = 'staticx'

# Base version, which will be augmented with Git information
BASE_VERSION = '0.14.1'

Expand Down Expand Up @@ -57,19 +59,17 @@ def get_version():
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('staticx').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()
Expand Down

0 comments on commit a0080c9

Please sign in to comment.