Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staticx: Use importlib.resources over pkg_resources for asset access #274

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Changed
- Introduced `pyproject.toml` and moved metadata from `setup.py` (#267)
- Removed use of deprecated `pkg_resources` (#271)
- Removed use of deprecated `pkg_resources` (#271, #274)

### Fixed
- Fixed an issue with non-ELF "binary" files in PyInstaller archives causing a crash (#270)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dependencies = [
"pyelftools",
# TODO(#264): Remove when Python 3.7 support is removed.
"importlib_metadata; python_version<'3.8'",
# TODO(#265): Remove when Python 3.8 support is removed.
"importlib_resources; python_version<'3.9'",
]
description = "Build static self-extracting app from dynamic executable"
license = {file = "LICENSE.txt"}
Expand Down
12 changes: 10 additions & 2 deletions staticx/assets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import pkg_resources
import sys
from .utils import copy_fileobj_to_tempfile

# TODO(#265): Remove backport when Python 3.8 support is removed.
# importlib.resources.files() was added in Python 3.9.
if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources # backport


def locate_asset(name, debug):
mode = 'debug' if debug else 'release'
path = '/'.join(('assets', mode, name))
try:
return pkg_resources.resource_stream(__name__, path)
return importlib_resources.files("staticx").joinpath(path).open("rb")
except FileNotFoundError:
raise KeyError(f"Asset not found: {name!r} (mode={mode!r})")

Expand Down
2 changes: 1 addition & 1 deletion staticx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_version():
# Otherwise, we're either installed (e.g. via pip), or running from
# an 'sdist' source distribution, and have a local PKG_INFO file.

# TODO(#242): Remove backport when Python 3.7 support is removed.
# TODO(#264): Remove backport when Python 3.7 support is removed.
if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
Expand Down