Skip to content

Commit

Permalink
utils.py, bldr.py: support Python 3.12
Browse files Browse the repository at this point in the history
By migrating from the deprecated pkg_resources API to
importlib_resources.
  • Loading branch information
wferi committed Jul 19, 2024
1 parent 32200fa commit 41111ef
Show file tree
Hide file tree
Showing 6 changed files with 17 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
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,3 +24,5 @@ urllib3==2.2.2
# via
# docker
# requests
zipp==3.19.2
# via importlib-resources
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def get_package_data():
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: System :: Systems Administration",
"Topic :: Utilities",
],
Expand Down
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 41111ef

Please sign in to comment.