Skip to content

Commit

Permalink
Rely on backports.tarfile for consistent tarfile behavior and streami…
Browse files Browse the repository at this point in the history
…ng capability.
  • Loading branch information
jaraco committed Apr 6, 2024
1 parent e9acc05 commit e13fc7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
22 changes: 8 additions & 14 deletions jaraco/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import contextlib
import functools
import io
import operator
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
import warnings
from typing import Iterator


if sys.version_info < (3, 12):
from backports import tarfile
else:
import tarfile


@contextlib.contextmanager
def pushd(dir: str | os.PathLike) -> Iterator[str | os.PathLike]:
"""
Expand Down Expand Up @@ -57,18 +61,8 @@ def tarball(
os.mkdir(target_dir)
try:
req = urllib.request.urlopen(url)
if sys.version_info < (3, 12):
# In order to getmembers + extract requires a seekable file descriptor
tf = tarfile.open(fileobj=io.BytesIO(req.read()), mode='r:gz')
else:
tf = tarfile.open(fileobj=req, mode='r|gz')
with tf:
if sys.version_info < (3, 12):
stripper = functools.partial(strip_first_component, path=target_dir)
for member in map(stripper, tf.getmembers()):
tf.extract(member, path=target_dir)
else:
tf.extractall(path=target_dir, filter=strip_first_component)
with tarfile.open(fileobj=req, mode='r|gz') as tf:
tf.extractall(path=target_dir, filter=strip_first_component)
yield target_dir
finally:
shutil.rmtree(target_dir)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ classifiers =
include_package_data = true
python_requires = >=3.8
install_requires =
backports.tarfile; python_version < "3.12"

[options.extras_require]
testing =
Expand Down

0 comments on commit e13fc7f

Please sign in to comment.