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.
This commit is contained in:
Matthieu Gautier 2017-04-26 18:11:05 +02:00
parent 7d62d16154
commit c8f5acb41f
2 changed files with 25 additions and 1 deletions

View File

@ -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:

View File

@ -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)