Rewrite imagebuilder unpack_http_archive using python modules

This commit is contained in:
Disassembler 2020-02-12 06:54:35 +01:00
parent 19425cece9
commit de6b5e81ac
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499

View File

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import requests
import shutil import shutil
import stat import stat
import subprocess import tarfile
import tempfile import tempfile
import zipfile
from .container import Container from .container import Container
from .image import Image from .image import Image
@ -121,16 +123,21 @@ class ImageBuilder:
def unpack_http_archive(src, dst): def unpack_http_archive(src, dst):
# Decompress an archive downloaded via http(s) # Decompress an archive downloaded via http(s)
# TODO: Rewrite to python (requests, tarfile) with tempfile.TemporaryFile() as tmp_archive:
xf = 'xzf' with requests.Session() as session:
if src.endswith('.bz2'): resource = session.get(src, stream=True)
xf = 'xjf' for chunk in resource.iter_content(chunk_size=None):
elif src.endswith('.xz'): if chunk:
xf = 'xJf' tmp_archive.write(chunk)
with subprocess.Popen(['wget', src, '-O', '-'], stdout=subprocess.PIPE) as wget: tmp_archive.seek(0)
with subprocess.Popen(['tar', xf, '-', '-C', dst], stdin=wget.stdout) as tar: is_zip = zipfile.is_zipfile(tmp_archive)
wget.stdout.close() tmp_archive.seek(0)
tar.wait() 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): def copy_tree(src, dst):
# Copies files from the host # Copies files from the host