From 078aac17586f4e08805b6f1d68cd90a3ba6b58e2 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Mon, 22 Aug 2022 16:58:29 +0300 Subject: [PATCH 1/2] Update get.py to support python 3.10+ --- tools/get.py | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/tools/get.py b/tools/get.py index bec2b51cb1b..38f50792e9e 100755 --- a/tools/get.py +++ b/tools/get.py @@ -54,9 +54,17 @@ def mkdir_p(path): def report_progress(count, blockSize, totalSize): if sys.stdout.isatty(): - percent = int(count*blockSize*100/totalSize) - percent = min(100, percent) - sys.stdout.write("\r%d%%" % percent) + if totalSize > 0: + percent = int(count*blockSize*100/totalSize) + percent = min(100, percent) + sys.stdout.write("\r%d%%" % percent) + else: + sofar = (count*blockSize) / 1024 + if sofar >= 1000: + sofar /= 1024 + sys.stdout.write("\r%dMB" % (sofar)) + else: + sys.stdout.write("\r%dKB" % (sofar)) sys.stdout.flush() def unpack(filename, destination): @@ -82,6 +90,32 @@ def unpack(filename, destination): shutil.rmtree(rename_to) shutil.move(dirname, rename_to) +def download_file_with_progress(url,filename): + import ssl + import contextlib + ctx = ssl.create_default_context() + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + with contextlib.closing(urlopen(url,context=ctx)) as fp: + total_size = int(fp.getheader("Content-Length",fp.getheader("Content-length","0"))) + block_count = 0 + block_size = 1024 * 8 + block = fp.read(block_size) + if block: + with open(filename,'wb') as out_file: + out_file.write(block) + block_count += 1 + report_progress(block_count, block_size, total_size) + while True: + block = fp.read(block_size) + if not block: + break + out_file.write(block) + block_count += 1 + report_progress(block_count, block_size, total_size) + else: + raise Exception ('nonexisting file or connection error') + def download_file(url,filename): import ssl import contextlib @@ -126,8 +160,11 @@ def get_tool(tool): if is_ci: download_file(url, local_path) else: - urlretrieve(url, local_path, report_progress) - sys.stdout.write("\rDone\n") + if (sys.version_info[0] == 3 and sys.version_info[1] >= 10) or sys.version_info[0] > 3: + download_file_with_progress(url, local_path) + else: + urlretrieve(url, local_path, report_progress) + sys.stdout.write("\rDone \n") sys.stdout.flush() else: print('Tool {0} already downloaded'.format(archive_name)) From e5f626fcd842c2fbe34b7a8aa4860f3e0ca9969c Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Mon, 22 Aug 2022 17:15:18 +0300 Subject: [PATCH 2/2] Use try/except to remove version check --- tools/get.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/get.py b/tools/get.py index 38f50792e9e..088e2f67139 100755 --- a/tools/get.py +++ b/tools/get.py @@ -62,7 +62,7 @@ def report_progress(count, blockSize, totalSize): sofar = (count*blockSize) / 1024 if sofar >= 1000: sofar /= 1024 - sys.stdout.write("\r%dMB" % (sofar)) + sys.stdout.write("\r%dMB " % (sofar)) else: sys.stdout.write("\r%dKB" % (sofar)) sys.stdout.flush() @@ -160,10 +160,10 @@ def get_tool(tool): if is_ci: download_file(url, local_path) else: - if (sys.version_info[0] == 3 and sys.version_info[1] >= 10) or sys.version_info[0] > 3: - download_file_with_progress(url, local_path) - else: + try: urlretrieve(url, local_path, report_progress) + except: + download_file_with_progress(url, local_path) sys.stdout.write("\rDone \n") sys.stdout.flush() else: