From c1ce90d1ce101fe6cf323ea377d5745fad2afe21 Mon Sep 17 00:00:00 2001 From: CreateSource <72283721+abnormalhare@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:19:31 -0500 Subject: [PATCH 1/3] fix a bug where computers would fail SSL certification when running download_tool.py --- tools/download_tool.py | 46 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/tools/download_tool.py b/tools/download_tool.py index f4512d0..d695fed 100644 --- a/tools/download_tool.py +++ b/tools/download_tool.py @@ -91,6 +91,21 @@ TOOLS: Dict[str, Callable[[str], str]] = { "wibo": wibo_url, } +def download(url, response, output) -> None: + if url.endswith(".zip"): + data = io.BytesIO(response.read()) + with zipfile.ZipFile(data) as f: + f.extractall(output) + # Make all files executable + for root, _, files in os.walk(output): + for name in files: + os.chmod(os.path.join(root, name), 0o755) + output.touch(mode=0o755) # Update dir modtime + else: + with open(output, "wb") as f: + shutil.copyfileobj(response, f) + st = os.stat(output) + os.chmod(output, st.st_mode | stat.S_IEXEC) def main() -> None: parser = argparse.ArgumentParser() @@ -104,21 +119,22 @@ def main() -> None: print(f"Downloading {url} to {output}") req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) - with urllib.request.urlopen(req) as response: - if url.endswith(".zip"): - data = io.BytesIO(response.read()) - with zipfile.ZipFile(data) as f: - f.extractall(output) - # Make all files executable - for root, _, files in os.walk(output): - for name in files: - os.chmod(os.path.join(root, name), 0o755) - output.touch(mode=0o755) # Update dir modtime - else: - with open(output, "wb") as f: - shutil.copyfileobj(response, f) - st = os.stat(output) - os.chmod(output, st.st_mode | stat.S_IEXEC) + try: + with urllib.request.urlopen(req) as response: + download(url, response, output) + except urllib.error.URLError: + try: + import certifi + import ssl + except: + import sys + import subprocess + + curr_py = sys.executable + subprocess.check_call([curr_py, '-m', 'pip', 'install', 'certifi', 'ssl'], stdout=subprocess.DEVNULL) + + with urllib.request.urlopen(req, context=ssl.create_default_context(cafile=certifi.where())) as response: + download(url, response, output) if __name__ == "__main__": From b26b99e9e319f29f5d81af737c57e9c9c6ec1872 Mon Sep 17 00:00:00 2001 From: CreateSource <72283721+abnormalhare@users.noreply.github.com> Date: Sat, 23 Nov 2024 13:35:03 -0500 Subject: [PATCH 2/3] Remove auto install script --- tools/download_tool.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/download_tool.py b/tools/download_tool.py index d695fed..b15d238 100644 --- a/tools/download_tool.py +++ b/tools/download_tool.py @@ -122,16 +122,13 @@ def main() -> None: try: with urllib.request.urlopen(req) as response: download(url, response, output) - except urllib.error.URLError: + except urllib.error.URLError as e: try: import certifi import ssl except: - import sys - import subprocess - - curr_py = sys.executable - subprocess.check_call([curr_py, '-m', 'pip', 'install', 'certifi', 'ssl'], stdout=subprocess.DEVNULL) + print("\"certifi\" module not found. Please install it using \"python -m pip install certifi\".") + return with urllib.request.urlopen(req, context=ssl.create_default_context(cafile=certifi.where())) as response: download(url, response, output) From 9a4fb87d44065223522947698abcba4f398731be Mon Sep 17 00:00:00 2001 From: CreateSource <72283721+abnormalhare@users.noreply.github.com> Date: Sat, 23 Nov 2024 14:11:44 -0500 Subject: [PATCH 3/3] Determine type of URLError --- tools/download_tool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/download_tool.py b/tools/download_tool.py index b15d238..27312c6 100644 --- a/tools/download_tool.py +++ b/tools/download_tool.py @@ -123,6 +123,8 @@ def main() -> None: with urllib.request.urlopen(req) as response: download(url, response, output) except urllib.error.URLError as e: + if str(e).find("CERTIFICATE_VERIFY_FAILED") == -1: + return try: import certifi import ssl @@ -133,6 +135,5 @@ def main() -> None: with urllib.request.urlopen(req, context=ssl.create_default_context(cafile=certifi.where())) as response: download(url, response, output) - if __name__ == "__main__": main()