fix a bug where computers would fail SSL certification when running download_tool.py

This commit is contained in:
CreateSource 2024-11-20 17:19:31 -05:00 committed by GitHub
parent 065fc7b715
commit c1ce90d1ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 15 deletions

View File

@ -91,6 +91,21 @@ TOOLS: Dict[str, Callable[[str], str]] = {
"wibo": wibo_url, "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: def main() -> None:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -104,21 +119,22 @@ def main() -> None:
print(f"Downloading {url} to {output}") print(f"Downloading {url} to {output}")
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req) as response: try:
if url.endswith(".zip"): with urllib.request.urlopen(req) as response:
data = io.BytesIO(response.read()) download(url, response, output)
with zipfile.ZipFile(data) as f: except urllib.error.URLError:
f.extractall(output) try:
# Make all files executable import certifi
for root, _, files in os.walk(output): import ssl
for name in files: except:
os.chmod(os.path.join(root, name), 0o755) import sys
output.touch(mode=0o755) # Update dir modtime import subprocess
else:
with open(output, "wb") as f: curr_py = sys.executable
shutil.copyfileobj(response, f) subprocess.check_call([curr_py, '-m', 'pip', 'install', 'certifi', 'ssl'], stdout=subprocess.DEVNULL)
st = os.stat(output)
os.chmod(output, st.st_mode | stat.S_IEXEC) with urllib.request.urlopen(req, context=ssl.create_default_context(cafile=certifi.where())) as response:
download(url, response, output)
if __name__ == "__main__": if __name__ == "__main__":