Skip to content

Commit

Permalink
Request and decompress the tarfile natively
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 5, 2024
1 parent dd2d8af commit ba5a1f1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions jaraco/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import os
import shutil
import subprocess
import tarfile
import tempfile
import urllib.request
import warnings
from typing import Iterator

Expand Down Expand Up @@ -52,15 +54,22 @@ def tarball(
# that we always know where the files were extracted.
os.mkdir(target_dir)
try:
getter = f'wget {url} -O -'
extract = f'tar x{infer_compression(url)} --strip-components=1 -C {target_dir}'
cmd = ' | '.join((getter, extract))
subprocess.check_call(cmd, shell=True)
req = urllib.request.urlopen(url)
with tarfile.open(fileobj=req, mode='r|gz') as tf:
for member in map(strip_first_component, tf.getmembers()):
tf.extract(member, path=target_dir)
yield target_dir
finally:
shutil.rmtree(target_dir)


def strip_first_component(
member: tarfile.TarInfo,
) -> tarfile.TarInfo:
_, member.name = member.name.split('/', 1)
return member


def _compose(*cmgrs):
"""
Compose any number of dependent context managers into a single one.
Expand Down

0 comments on commit ba5a1f1

Please sign in to comment.