?wongkai@hnu.edu.cn
# Download external libraries that are needed for the Cmake build
# but cmake cannot download due to network issues.
# make sure you can visit download link via your browser
import hashlib
import re
import sys
from pathlib import Path
import requests
def get_size(file_url):
head = requests.head(file_url)
return int(head.headers['Content-Length']) # in bytes
def format_size(size):
import math
p = int(math.log(size, 1024))
return str(round(size / math.pow(1024, p), 2)) + " " + ["B", "KB", "MB", "GB", "TB", "PB", "EB"][p]
log_path = Path(input("Enter the path to the log file: "))
# log_path = Path(sys.argv[1])
with open(log_path, "r") as log_file:
log = log_file.read()
tasks = [[Path(eval(task[0])), eval(task[1])] for task in
[s.split()[1:] for s in re.findall(r'#cmake_download\s\".*\"', log)]]
print("Found {} failed task".format(len(tasks)) + "s" * bool(len(tasks) > 1))
if len(tasks) == 0:
print("Done")
sys.exit(0)
total_size = 0
for task in tasks:
file_size = get_size(task[1])
total_size += file_size
print(f"{task[0].name.split('-')[-1]:<50}{format_size(file_size)}")
print("==============================================================================")
print(f"Total size: {format_size(total_size)}")
if input("Continue? [Y/n]: ") == "n":
print("Done")
sys.exit(0)
print("==============================================================================")
for task in tasks:
path = task[0]
url = task[1]
file_name = path.name.split('-')[-1]
file_md5 = path.name.split('-')[0]
if path.exists() and hashlib.md5(path.read_bytes()).hexdigest() == file_md5:
print("{} already exists, skipping...".format(path.name))
continue
print(f"Downloading {file_name} from {url}")
r = requests.get(url)
if r.raise_for_status():
print(f"Failed to download {file_name} ({r.status_code})")
continue
else:
print(f"Downloaded {file_name} successfully")
with open(path, "wb") as f:
f.write(r.content)
print(f"Checking md5sum of {file_name}: {file_md5}")
if file_md5 == hashlib.md5(r.content).hexdigest():
print(f"md5sum of {file_name} is correct:{file_md5}")
print(f"{file_name} has been written to {path}")
print("==============================================================================")
print("\nDone")
|