5
5
import logging
6
6
import os
7
7
import shutil
8
- import ssl
9
8
import urllib .error
10
9
import urllib .request
11
10
import zipfile
@@ -29,15 +28,6 @@ def sha256sum(filepath: str): # noqa: ANN201, D103
29
28
return sha256 .hexdigest ()
30
29
31
30
32
- def get_file (url : str , target_path : str , auto_unzip = True ): # noqa: ANN001, ANN201, D103
33
- target_url = url
34
- try :
35
- download_file (target_url , target_path , auto_unzip )
36
- except Exception :
37
- logger .info ("cannot find the file from local mirror. try to get it from the original url" )
38
- download_file (url , target_path , auto_unzip )
39
-
40
-
41
31
def download_file (url : str , target_path : str , auto_unzip = True ): # noqa: ANN001, ANN201, D103
42
32
logger .info (f"Downloading file: { url } " )
43
33
url_original_filename = os .path .basename (url )
@@ -47,13 +37,8 @@ def download_file(url: str, target_path: str, auto_unzip=True): # noqa: ANN001,
47
37
target_dir_path = os .path .dirname (target_path )
48
38
download_temp_target_path = os .path .join (target_dir_path , url_original_filename )
49
39
50
- # Disable SSL check
51
- ctx = ssl .create_default_context ()
52
- ctx .check_hostname = False
53
- ctx .verify_mode = ssl .CERT_NONE
54
-
55
40
with (
56
- urllib .request .urlopen (url , context = ctx ) as response , # noqa: S310
41
+ urllib .request .urlopen (url ) as response , # noqa: S310
57
42
open (download_temp_target_path , "wb" ) as out_file ,
58
43
):
59
44
shutil .copyfileobj (response , out_file )
@@ -89,7 +74,7 @@ def retry_call(call: Callable, retries: int = RETRIES, **kwargs): # noqa: ANN20
89
74
call (** kwargs )
90
75
break
91
76
except Exception :
92
- logging .exception (f"Failed try { i + 1 } /{ retries } " )
77
+ logger .exception (f"Failed try { i + 1 } /{ retries } " )
93
78
else :
94
79
raise MaxTriesExhausted
95
80
@@ -106,19 +91,19 @@ def download_pretrained_model(model_spec: dict, target_dir: str, weights_url: st
106
91
107
92
if os .path .exists (target_download_path ):
108
93
if sha_sum is None :
109
- logging .warning (f"Model already existed: { target_download_path } but sha_sum is not specified" )
110
- logging .warning (f"consider to add sha_sum to the model spec: { sha256sum (target_download_path )} " )
94
+ logger .warning (f"Model already existed: { target_download_path } but sha_sum is not specified" )
95
+ logger .warning (f"consider to add sha_sum to the model spec: { sha256sum (target_download_path )} " )
111
96
elif sha256sum (target_download_path ) == sha_sum :
112
97
logger .info (f"Model already downloaded: { target_download_path } " )
113
98
return
114
99
else :
115
- logging .warning (f"Model already downloaded but SHA mismatch: { target_download_path } " )
116
- logging .warning ("Redownloading..." )
100
+ logger .warning (f"Model already downloaded but SHA mismatch: { target_download_path } " )
101
+ logger .warning ("Redownloading..." )
117
102
os .remove (target_download_path )
118
103
119
104
try :
120
105
retry_call (
121
- get_file ,
106
+ download_file ,
122
107
url = model_external_url ,
123
108
target_path = target_download_path ,
124
109
auto_unzip = auto_unzip ,
0 commit comments