mirror of
https://github.com/encounter/dtk-template.git
synced 2025-07-13 16:45:57 +00:00
fix a bug where computers would fail SSL certification when running ... (#42)
* fix a bug where computers would fail SSL certification when running download_tool.py * Remove auto install script * Determine type of URLError * Update download_tool.py
This commit is contained in:
parent
e72bd7a99e
commit
ca32d3f429
@ -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,22 +119,21 @@ 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 as e:
|
||||
if str(e).find("CERTIFICATE_VERIFY_FAILED") == -1:
|
||||
raise e
|
||||
try:
|
||||
import certifi
|
||||
import ssl
|
||||
except:
|
||||
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)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user