Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated importlib.resources.*. #249

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/shiv/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from itertools import chain
from pathlib import Path
from stat import S_IFMT, S_IMODE, S_IXGRP, S_IXOTH, S_IXUSR
from typing import Generator, IO, Any, List, Optional, Tuple
from types import ModuleType
from typing import Any, Generator, IO, Iterator, List, Optional, Tuple, Union

from . import bootstrap
from .bootstrap.environment import Environment
Expand All @@ -27,6 +28,20 @@
# noinspection PyUnresolvedReferences
import importlib_resources # type: ignore

# N.B.: `importlib.resources.{contents,is_resource,path}` are deprecated in 3.11 and gone in 3.13.
if sys.version_info < (3, 11):
def iter_package_files(package: Union[str, ModuleType]) -> Iterator[Path]:
for bootstrap_file in importlib_resources.contents(bootstrap):
if importlib_resources.is_resource(bootstrap, bootstrap_file):
with importlib_resources.path(bootstrap, bootstrap_file) as path:
yield path
else:
def iter_package_files(package: Union[str, ModuleType]) -> Iterator[Path]:
for resource in importlib_resources.files(package).iterdir():
if resource.is_file():
with importlib_resources.as_file(resource) as path:
yield path

# Typical maximum length for a shebang line
BINPRM_BUF_SIZE = 128

Expand Down Expand Up @@ -149,22 +164,17 @@ def create_archive(
# now let's add the shiv bootstrap code.
bootstrap_target = Path("_bootstrap")

for bootstrap_file in importlib_resources.contents(bootstrap): # type: ignore

if importlib_resources.is_resource(bootstrap, bootstrap_file): # type: ignore

with importlib_resources.path(bootstrap, bootstrap_file) as path: # type: ignore

data = path.read_bytes()

write_to_zipapp(
archive,
str(bootstrap_target / path.name),
data,
zipinfo_datetime,
compression,
stat=path.stat(),
)
for path in iter_package_files(bootstrap):
data = path.read_bytes()

write_to_zipapp(
archive,
str(bootstrap_target / path.name),
data,
zipinfo_datetime,
compression,
stat=path.stat(),
)

# Write environment info in json file.
#
Expand Down