diff --git a/usr/lib/python3.8/spoc/imagebuilder.py b/usr/lib/python3.8/spoc/imagebuilder.py index 003a324..6786186 100644 --- a/usr/lib/python3.8/spoc/imagebuilder.py +++ b/usr/lib/python3.8/spoc/imagebuilder.py @@ -1,10 +1,12 @@ # -*- coding: utf-8 -*- import os +import requests import shutil import stat -import subprocess +import tarfile import tempfile +import zipfile from .container import Container from .image import Image @@ -121,16 +123,21 @@ class ImageBuilder: def unpack_http_archive(src, dst): # Decompress an archive downloaded via http(s) - # TODO: Rewrite to python (requests, tarfile) - xf = 'xzf' - if src.endswith('.bz2'): - xf = 'xjf' - elif src.endswith('.xz'): - xf = 'xJf' - with subprocess.Popen(['wget', src, '-O', '-'], stdout=subprocess.PIPE) as wget: - with subprocess.Popen(['tar', xf, '-', '-C', dst], stdin=wget.stdout) as tar: - wget.stdout.close() - tar.wait() + with tempfile.TemporaryFile() as tmp_archive: + with requests.Session() as session: + resource = session.get(src, stream=True) + for chunk in resource.iter_content(chunk_size=None): + if chunk: + tmp_archive.write(chunk) + tmp_archive.seek(0) + is_zip = zipfile.is_zipfile(tmp_archive) + tmp_archive.seek(0) + if is_zip: + with zipfile.ZipFile(tmp_archive) as zip: + zip.extractall(dst) + else: + with tarfile.open(fileobj=tmp_archive) as tar: + tar.extractall(dst, numeric_owner=True) def copy_tree(src, dst): # Copies files from the host