From c5f6862d118d7d69210f0e73bea1b055f5f21f2b Mon Sep 17 00:00:00 2001 From: Wenyu Date: Fri, 15 Dec 2023 13:29:18 +0800 Subject: [PATCH] fix wget download (#59957) * fix wget download * fix wget --- python/paddle/utils/download.py | 38 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/python/paddle/utils/download.py b/python/paddle/utils/download.py index 59efb656f6691..b9ca1f35976c6 100644 --- a/python/paddle/utils/download.py +++ b/python/paddle/utils/download.py @@ -21,6 +21,7 @@ import tarfile import time import zipfile +from urllib.parse import urlparse import httpx @@ -196,22 +197,31 @@ def _get_download(url, fullname): return False -def _wget_download(url, fullname): - # using wget to download url - tmp_fullname = fullname + "_tmp" - # –user-agent - command = f'wget -O {tmp_fullname} -t {DOWNLOAD_RETRY_LIMIT} {url}' - subprc = subprocess.Popen( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - _ = subprc.communicate() - - if subprc.returncode != 0: - raise RuntimeError( - f'{command} failed. Please make sure `wget` is installed or {url} exists' +def _wget_download(url: str, fullname: str): + try: + assert urlparse(url).scheme in ( + 'http', + 'https', + ), 'Only support https and http url' + # using wget to download url + tmp_fullname = fullname + "_tmp" + # –user-agent + command = f'wget -O {tmp_fullname} -t {DOWNLOAD_RETRY_LIMIT} {url}' + subprc = subprocess.Popen( + command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) + _ = subprc.communicate() + + if subprc.returncode != 0: + raise RuntimeError( + f'{command} failed. Please make sure `wget` is installed or {url} exists' + ) + + shutil.move(tmp_fullname, fullname) - shutil.move(tmp_fullname, fullname) + except Exception as e: # requests.exceptions.ConnectionError + logger.info(f"Downloading {url} failed with exception {str(e)}") + return False return fullname