From 6ed55ff4626a37114ac3e3959691bb0841f18e9c Mon Sep 17 00:00:00 2001 From: Dhinak G <17605561+dhinakg@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:10:17 -0500 Subject: [PATCH] Verify checksum while downloading instead of after Should speed up checksum calculations by not looping over the file twice --- resources/utilities.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/resources/utilities.py b/resources/utilities.py index f2a63eb06..97c5f8673 100644 --- a/resources/utilities.py +++ b/resources/utilities.py @@ -407,12 +407,15 @@ def download_file(link, location, is_gui=None, verify_checksum=False): dl = 0 total_downloaded_string = "" global clear + checksum = hashlib.sha256() if verify_checksum else None with location.open("wb") as file: count = 0 start = time.perf_counter() for chunk in response.iter_content(1024 * 1024 * 4): dl += len(chunk) file.write(chunk) + if checksum: + checksum.update(chunk) count += len(chunk) if is_gui is None: if clear: @@ -425,19 +428,8 @@ def download_file(link, location, is_gui=None, verify_checksum=False): total_downloaded_string = f" ({round(float(dl / total_file_size * 100), 2)}%)" print(f"{round(count / 1024 / 1024, 2)}MB Downloaded{file_size_string}{total_downloaded_string}\nAverage Download Speed: {round(dl//(time.perf_counter() - start) / 100000 / 8, 2)} MB/s") - if verify_checksum is True: - # Verify checksum - # Note that this can be quite taxing on slower Macs - checksum = hashlib.sha256() - with location.open("rb") as file: - chunk = file.read(1024 * 1024 * 16) - while chunk: - checksum.update(chunk) - chunk = file.read(1024 * 1024 * 16) - enable_sleep_after_running() - return checksum enable_sleep_after_running() - return True + return checksum.hexdigest() if checksum else True else: cls() header = "# Could not establish Network Connection with provided link! #"