From c8f5acb41f139ae421c63a787c5a6a01b87538e2 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 26 Apr 2017 18:11:05 +0200 Subject: [PATCH] Add better progress information output for long operation. Checking sha256sum and downloading file can be pretty long. Let's print few information to the user. --- kiwix-build.py | 12 ++++++++++++ utils.py | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/kiwix-build.py b/kiwix-build.py index 36865b0..c4228bd 100755 --- a/kiwix-build.py +++ b/kiwix-build.py @@ -15,6 +15,7 @@ from utils import ( remove_duplicates, add_execution_right, get_sha256, + print_status, StopBuild, SkipCommand, Defaultdict, @@ -440,11 +441,22 @@ class BuildEnv: context = None batch_size = 1024 * 8 extra_args = {'context':context} if sys.version_info >= (3, 4, 3) else {} + progress_chars = "/-\|" with urllib.request.urlopen(file_url, **extra_args) as resource, open(file_path, 'wb') as file: + tsize = resource.getheader('Content-Length', None) + if tsize is not None: + tsize = int(tsize) + current = 0 while True: batch = resource.read(batch_size) if not batch: break + if tsize: + current += batch_size + print_status("{:.2%}".format(current/tsize)) + else: + print_status(progress_chars[current]) + current = (current+1)%4 file.write(batch) if not what.sha256: diff --git a/utils.py b/utils.py index f41a400..2d22c6c 100644 --- a/utils.py +++ b/utils.py @@ -27,11 +27,23 @@ def remove_duplicates(iterable, key_function=None): def get_sha256(path): + progress_chars = "/-\|" + current = 0 + batch_size = 1024 * 8 sha256 = hashlib.sha256() with open(path, 'br') as f: - sha256.update(f.read()) + while True: + batch = f.read(batch_size) + if not batch: + break + sha256.update(batch) + print_status(progress_chars[current]) + current = (current+1)%4 return sha256.hexdigest() +def print_status(status): + text = "{}\033[{}D".format(status, len(status)) + print(text, end="") def add_execution_right(file_path): current_permissions = stat.S_IMODE(os.lstat(file_path).st_mode)