From 5ee47a661c5ec760908ff27336cdb41e8faa4588 Mon Sep 17 00:00:00 2001 From: cc <18856836718@163.com> Date: Wed, 27 Mar 2024 10:56:49 +0800 Subject: [PATCH] =?UTF-8?q?download=5Ftool=20=E5=B7=A5=E5=85=B7=E6=96=AD?= =?UTF-8?q?=E7=82=B9=E7=BB=AD=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../download_dependency/src/download_utils.py | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/tools/download_dependency/src/download_utils.py b/tools/download_dependency/src/download_utils.py index 6f42217..f3879b2 100644 --- a/tools/download_dependency/src/download_utils.py +++ b/tools/download_dependency/src/download_utils.py @@ -1,7 +1,11 @@ import os +import requests import sys import urllib.error import wget +import warnings + +warnings.filterwarnings("ignore", message='Unverified HTTPS request') def download_dependence_handler(shell_dict): @@ -14,24 +18,70 @@ def download_dependence_handler(shell_dict): def download_dependence_file(shell_cmd, shell_dict): ret = True url_and_save_path = shell_dict.get(shell_cmd) + url_ = url_and_save_path.get("url") + save_path = url_and_save_path.get("save_path") try: - print(f"Downloading from {url_and_save_path.get('url')}") - download_result = wget.download( - url_and_save_path.get('url'), url_and_save_path.get('save_path') - ) - print() + if url_.startswith("https://gitee.com"): + download_result = wget_download(save_path, url_) + else: + download_result = requests_download(url_, save_path) + except (requests.exceptions.Timeout, + requests.exceptions.ConnectionError, + requests.exceptions.HTTPError, + requests.exceptions.TooManyRedirects, + requests.exceptions.RequestException) as e: + print(f"[ERROR] download error occurs: {str(e)} " + f"\nPlease visit following url and download dependencies to default directory." + f"\n\t{url_}" + ) + raise OSError(f"download error occurs: {str(e)}") + except (TimeoutError, urllib.error.URLError, OSError) as e: print(f"[ERROR] download error occurs: {str(e)} " f"\nPlease visit following url and download dependencies to default directory." - f"\n\t{url_and_save_path.get('url')}" + f"\n\t{url_}" ) raise OSError(f"download error occurs: {str(e)}") if not os.path.isfile(download_result): print(f"[ERROR] Download dependencies failed. " f"Please visit following url and download dependencies to default directory." - f"\n\t{url_and_save_path.get('url')}" + f"\n\t{url_}" ) ret = False return ret + +def wget_download(save_path, url_): + print(f"Downloading from {url_}") + download_result = wget.download(url_, save_path) + print() + return download_result + + +def requests_download(url, save_path): + req_get_total_size = requests.get(url, stream=True, verify=False) + total_size = int(req_get_total_size.headers.get("Content-Length")) + + if os.path.exists(save_path): + temp_size = os.path.getsize(save_path) + else: + temp_size = 0 + print(f"Downloading from {url}, total size: {total_size}, temp size: {temp_size}") + + headers = {'Range': f'bytes={temp_size}-{total_size}'} + req = requests.get(url, stream=True, verify=False, headers=headers) + + with open(save_path, "ab") as file: + for chunk in req.iter_content(chunk_size=1024): + if chunk: + temp_size += len(chunk) + file.write(chunk) + file.flush() + + done = int(50 * temp_size/total_size) + sys.stdout.write(f"\r[{'.' * done}{' ' * (50 - done)}] {int(100 * temp_size/total_size)}%") + sys.stdout.flush() + print() + return save_path + -- Gitee