From bbe850924e953b498c157adf199984194f6fd4d7 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 the deprecated pkg_resources to importlib_resources --- bldr/bldr.py | 8 ++++++-- bldr/utils.py | 5 ----- requirements.in | 1 + requirements.txt | 4 ++++ test/unit/test_resources.py | 5 +++++ 5 files changed, 16 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..84cd493 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,8 @@ dockerpty==0.4.1 # via -r requirements.in idna==3.7 # via requests +importlib-resources==6.4.0 + # via -r requirements.in requests==2.32.3 # via docker six==1.16.0 @@ -22,3 +24,5 @@ urllib3==2.2.2 # via # docker # requests +zipp==3.19.2 + # via importlib-resources 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()