Skip to content

Commit

Permalink
Replace deprecated importlib.resources.*. (#249)
Browse files Browse the repository at this point in the history
* Replace deprecated `importlib.resources.contents`.

This was deprecated in Python 3.11, which shiv officially supports, and
it's gone in 3.13.

* Fix all deprecated `importlib.resources` bits.
  • Loading branch information
jsirois authored Feb 5, 2024
1 parent 3ccca9e commit 094b31a
Showing 1 changed file with 27 additions and 17 deletions.
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

0 comments on commit 094b31a

Please sign in to comment.