Skip to content

Commit

Permalink
utils.py: migrate from pkg_resources to importlib_resources
Browse files Browse the repository at this point in the history
The former is being phased out, it already provokes (when? where?)

    DeprecationWarning: pkg_resources is deprecated as an API
  • Loading branch information
wferi committed Jul 18, 2024
1 parent 93f2d32 commit 48e30b6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
8 changes: 6 additions & 2 deletions bldr/bldr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib_resources
import logging
import os
import pwd
Expand All @@ -10,7 +11,7 @@
from tempfile import TemporaryDirectory

from .docker_utils import create_docker_client, DockerImageBuilder, DockerImage, DockerContainer, DEFAULT_DOCKER_TIMEOUT
from .utils import BLDRError, BLDRSetupFailed, escape_docker_image_tag, get_resource
from .utils import BLDRError, BLDRSetupFailed, escape_docker_image_tag


PRE_BUILD_HOOK = "/hooks/pre-build"
Expand Down Expand Up @@ -106,7 +107,10 @@ def _build_image(self, tag: str, control_file: Optional[Path] = None) -> DockerI

with TemporaryDirectory(prefix="bldr_docker_dir_") as tmp_dir:
docker_files_dir = Path(tmp_dir).joinpath('docker_files')
shutil.copytree(str(get_resource('.')), str(docker_files_dir))
with importlib_resources.as_file(
importlib_resources.files().joinpath('data')
) as resource_dir:
shutil.copytree(resource_dir, docker_files_dir)
if control_file is None:
docker_files_dir.joinpath('control').write_text('')
else:
Expand Down
5 changes: 0 additions & 5 deletions bldr/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import pwd
from pathlib import Path
from pkg_resources import resource_filename


class BLDRError(Exception):
Expand All @@ -19,10 +18,6 @@ def __init__(self, msg: str, exitcode: int = 1) -> None:
super().__init__(msg, exitcode)


def get_resource(path: str) -> Path:
return Path(resource_filename('bldr', str(Path('data', path))))


def escape_docker_image_tag(tag: str) -> str:
return tag.replace(":", "-").replace("/", "-")

Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docker
dockerpty
importlib_resources
5 changes: 5 additions & 0 deletions test/unit/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import importlib_resources


def test_data_resource():
assert importlib_resources.files('bldr').joinpath('data').is_dir()

0 comments on commit 48e30b6

Please sign in to comment.