Skip to content

Commit

Permalink
Replace pkg_resources with importlib.resources (#109)
Browse files Browse the repository at this point in the history
* Replace pkg_resources with importlib.resources

* fix in setup.py

* fix ci for new resource mechanics

* ah

* try

* arg

* update manifest

* ?

* debug

* ah

* clean ci

* fix win
  • Loading branch information
almarklein authored Mar 8, 2024
1 parent 9ab981a commit b941256
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
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

0 comments on commit b941256

Please sign in to comment.