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

Replace pkg_resources with importlib.resources #109

Merged
merged 12 commits into from
Mar 8, 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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ jobs:
python -m pip install --upgrade pip
pip install psutil
pip install invoke pytest pytest-cov
invoke get-ffmpeg-binary;
invoke get-ffmpeg-binary
pip install .
rm -r ./imageio_ffmpeg
- name: Test with pytest
run: |
python -c "import sys; print(sys.version, '\n', sys.prefix)";
python -c 'import imageio_ffmpeg; print(imageio_ffmpeg.get_ffmpeg_version())'
invoke test
pytest tests -v --cov=imageio_ffmpeg --cov-report=term
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include LICENSE
include README.md
include imageio_ffmpeg/binaries/*.*
18 changes: 14 additions & 4 deletions imageio_ffmpeg/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import subprocess
import sys
from functools import lru_cache

from pkg_resources import resource_filename
import importlib.resources

from ._definitions import FNAME_PER_PLATFORM, get_platform

Expand Down Expand Up @@ -42,8 +41,7 @@ def _get_ffmpeg_exe():
plat = get_platform()

# 2. Try from here
bin_dir = resource_filename("imageio_ffmpeg", "binaries")
exe = os.path.join(bin_dir, FNAME_PER_PLATFORM.get(plat, ""))
exe = os.path.join(_get_bin_dir(), FNAME_PER_PLATFORM.get(plat, ""))
if exe and os.path.isfile(exe) and _is_valid_exe(exe):
return exe

Expand All @@ -64,6 +62,18 @@ def _get_ffmpeg_exe():
return None


def _get_bin_dir():
if sys.version_info < (3, 9):
context = importlib.resources.path("imageio_ffmpeg.binaries", "__init__.py")
else:
ref = importlib.resources.files("imageio_ffmpeg.binaries") / "__init__.py"
context = importlib.resources.as_file(ref)
with context as path:
pass
# Return the dir. We assume that the data files are on a normal dir on the fs.
return str(path.parent)


def _popen_kwargs(prevent_sigint=False):
startupinfo = None
preexec_fn = None
Expand Down
1 change: 1 addition & 0 deletions imageio_ffmpeg/binaries/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Just here to make importlib.resources work
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@
python_requires=">=3.5",
setup_requires=[],
install_requires=["setuptools"],
packages=["imageio_ffmpeg"],
package_dir={"imageio_ffmpeg": "imageio_ffmpeg"},
package_data={"imageio_ffmpeg": ["binaries/*.*"]},
packages=["imageio_ffmpeg", "imageio_ffmpeg.binaries"],
package_data={"imageio_ffmpeg.binaries": ["*.*"]},
include_package_data=True,
zip_safe=False,
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def black_wrapper(writeback):
def clear_binaries_dir(target_dir):
assert os.path.isdir(target_dir)
for fname in os.listdir(target_dir):
if fname != "README.md":
if fname not in ["README.md", "__init__.py"]:
print("Removing", fname, "...", end="")
os.remove(os.path.join(target_dir, fname))
print("done")
Expand Down
Loading