From cc5df7cfcd14275b6648f6b4a54ce62fe79dc259 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 11 Jan 2017 14:51:31 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20impl=C3=A9mentation=20wrapper=20Py?= =?UTF-8?q?thon=20sur=20rkt/acbuild?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rkt.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 rkt.py diff --git a/rkt.py b/rkt.py new file mode 100644 index 0000000..9ca4530 --- /dev/null +++ b/rkt.py @@ -0,0 +1,58 @@ +from urllib import request +from os import environ +import sys +import math +import tarfile + +def print_progress_bar(percent_progress=0, char_size=50, clear_line=True): + + bar_progress = math.floor(char_size*(percent_progress/100)) + bar = "=" * bar_progress + " " * (char_size - bar_progress) + + if clear_line: + sys.stdout.write(u"\u001b[1000D") + sys.stdout.write("[{:s}] {:d}%".format(bar, int(percent_progress))) + sys.stdout.flush() + +def download_file(file_url, dest_path, bulk_size = 8192): + + req = request.urlopen(file_url) + meta = req.info() + file_size = int(meta.get('Content-Length')) + + with open(dest_path, 'wb') as dest_file: + print('Downloading "{:s}". Size: {:d}b'.format(file_url, file_size)) + downloaded_size = 0 + while True: + buff = req.read(bulk_size) + if not buff: + break + downloaded_size += len(buff) + dest_file.write(buff) + progress = downloaded_size/file_size*100 + print_progress_bar(progress) + dest_file.close() + # Add linebreak + print("") + +def extract_tar(file_path, dest_dir = "."): + print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir)) + with tarfile.open(file_path) as tar: + tar.extractall(dest_dir) + tar.close() + +def download_rkt(): + url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz" + file_path="rkt.tar.gz" + download_file(file_url=url, dest_path=file_path) + extract_tar(file_path) + +def download_acbuild(): + url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz" + file_path="acbuild.tar.gz" + download_file(file_url=url, dest_path=file_path) + extract_tar(file_path) + +if __name__ == "__main__": + download_rkt() + download_acbuild()