From 1735d94e1b8a37bbf5275cd0da6bb3737f7d9da3 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Wed, 24 Oct 2018 12:24:50 +0200 Subject: [PATCH 1/2] Add progress bar Addresses part of #2 --- bazelisk.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index 64a150ba..634d1602 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -84,14 +84,41 @@ def determine_bazel_filename(version): return "bazel-{}-{}-{}".format(version, operating_system, machine) + +def to_mb(bytes): + r = float(bytes) + + r = r / 1024 + r = r / 1024 + + return r + def download_bazel_into_directory(version, directory): bazel_filename = determine_bazel_filename(version) url = "https://releases.bazel.build/{}/release/{}".format(version, bazel_filename) destination_path = os.path.join(directory, bazel_filename) if not os.path.exists(destination_path): - print("Downloading {}...".format(url)) - with urllib.request.urlopen(url) as response, open(destination_path, 'wb') as out_file: - shutil.copyfileobj(response, out_file) + print("Downloading {}".format(url)) + u = urllib.request.urlopen(url) + meta = u.info() + fileTotalbytes=int(meta.get("Content-Length")) + fileTotalMb = round(to_mb(fileTotalbytes)) + + data_blocks = [] + total = 0 + while True: + block = u.read(1024) + data_blocks.append(block) + total += len(block) + hash = ((60*total)//fileTotalbytes) + print("[{}{}] {}/{}MB".format('#' * hash, ' ' * (60-hash), round(to_mb(total), 1), fileTotalMb), end="\r") + if not len(block): + print() + break + + data = b"".join(data_blocks) + u.close() + open(destination_path, 'wb').write(data) os.chmod(destination_path, 0o755) return destination_path From ae12338185223638c28cf07af23f992d3e27f7aa Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Wed, 24 Oct 2018 12:27:03 +0200 Subject: [PATCH 2/2] fix rounding issue --- bazelisk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazelisk.py b/bazelisk.py index 634d1602..0d564d7a 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -102,7 +102,7 @@ def download_bazel_into_directory(version, directory): u = urllib.request.urlopen(url) meta = u.info() fileTotalbytes=int(meta.get("Content-Length")) - fileTotalMb = round(to_mb(fileTotalbytes)) + fileTotalMb = round(to_mb(fileTotalbytes), 1) data_blocks = [] total = 0