From 27f777be463159cc35a8b1dd63484a5f1b4d1ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= Date: Thu, 18 Jul 2024 16:19:32 +0200 Subject: [PATCH] utils.py: migrate from pkg_resources to importlib_resources The former is being phased out, it already provokes (when? where?) DeprecationWarning: pkg_resources is deprecated as an API --- bldr/bldr.py | 8 ++++++-- bldr/utils.py | 5 ----- requirements.in | 1 + requirements.txt | 1 + test/unit/test_resources.py | 5 +++++ 5 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 test/unit/test_resources.py diff --git a/bldr/bldr.py b/bldr/bldr.py index 85014cd..50fa092 100644 --- a/bldr/bldr.py +++ b/bldr/bldr.py @@ -1,3 +1,4 @@ +import importlib_resources import logging import os import pwd @@ -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" @@ -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: diff --git a/bldr/utils.py b/bldr/utils.py index c794a8d..f452870 100644 --- a/bldr/utils.py +++ b/bldr/utils.py @@ -1,7 +1,6 @@ import os import pwd from pathlib import Path -from pkg_resources import resource_filename class BLDRError(Exception): @@ -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("/", "-") diff --git a/requirements.in b/requirements.in index 97c0281..880f6b5 100644 --- a/requirements.in +++ b/requirements.in @@ -1,2 +1,3 @@ docker dockerpty +importlib_resources diff --git a/requirements.txt b/requirements.txt index e7178a5..d546954 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ docker==7.1.0 # via -r requirements.in dockerpty==0.4.1 # via -r requirements.in +importlib_resources==6.4.0 idna==3.7 # via requests requests==2.32.3 diff --git a/test/unit/test_resources.py b/test/unit/test_resources.py new file mode 100644 index 0000000..2e2ff3c --- /dev/null +++ b/test/unit/test_resources.py @@ -0,0 +1,5 @@ +import importlib_resources + + +def test_data_resource(): + assert importlib_resources.files('bldr').joinpath('data').is_dir()