@@ -110,29 +110,42 @@ def download(path, url, probably_big, verbose, help_on_error=None):
110
110
111
111
112
112
def _download (path , url , probably_big , verbose , exception , help_on_error = None ):
113
+ # Try to use curl (potentially available on win32
114
+ # https://devblogs.microsoft.com/commandline/tar-and-curl-come-to-windows/)
115
+ # If an error occurs:
116
+ # - If we are on win32 fallback to powershell
117
+ # - Otherwise raise the error if appropriate
113
118
if probably_big or verbose :
114
119
print ("downloading {}" .format (url ))
115
- # see https://serverfault.com/questions/301128/how-to-download
116
- if sys .platform == 'win32' :
117
- run (["PowerShell.exe" , "/nologo" , "-Command" ,
118
- "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;" ,
119
- "(New-Object System.Net.WebClient).DownloadFile('{}', '{}')" .format (url , path )],
120
- verbose = verbose ,
121
- exception = exception )
122
- else :
120
+
121
+ platform_is_win32 = sys .platform == 'win32'
122
+ try :
123
123
if probably_big or verbose :
124
124
option = "-#"
125
125
else :
126
126
option = "-s"
127
- require (["curl" , "--version" ])
127
+ # If curl is not present on Win32, we shoud not sys.exit
128
+ # but raise `CalledProcessError` or `OSError` instead
129
+ require (["curl" , "--version" ], exception = platform_is_win32 )
128
130
run (["curl" , option ,
129
131
"-L" , # Follow redirect.
130
132
"-y" , "30" , "-Y" , "10" , # timeout if speed is < 10 bytes/sec for > 30 seconds
131
133
"--connect-timeout" , "30" , # timeout if cannot connect within 30 seconds
132
134
"--retry" , "3" , "-Sf" , "-o" , path , url ],
133
135
verbose = verbose ,
134
- exception = exception ,
136
+ exception = True , # Will raise RuntimeError on failure
135
137
help_on_error = help_on_error )
138
+ except (subprocess .CalledProcessError , OSError , RuntimeError ):
139
+ # see http://serverfault.com/questions/301128/how-to-download
140
+ if platform_is_win32 :
141
+ run (["PowerShell.exe" , "/nologo" , "-Command" ,
142
+ "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;" ,
143
+ "(New-Object System.Net.WebClient).DownloadFile('{}', '{}')" .format (url , path )],
144
+ verbose = verbose ,
145
+ exception = exception )
146
+ # Check if the RuntimeError raised by run(curl) should be silenced
147
+ elif verbose or exception :
148
+ raise
136
149
137
150
138
151
def verify (path , expected , verbose ):
@@ -198,19 +211,23 @@ def run(args, verbose=False, exception=False, is_bootstrap=False, help_on_error=
198
211
sys .exit (err )
199
212
200
213
201
- def require (cmd , exit = True ):
214
+ def require (cmd , exit = True , exception = False ):
202
215
'''Run a command, returning its output.
203
216
On error,
204
- If `exit` is `True`, exit the process.
205
- Otherwise, return None.'''
217
+ If `exception` is `True`, raise the error
218
+ Otherwise If `exit` is `True`, exit the process
219
+ Else return None.'''
206
220
try :
207
221
return subprocess .check_output (cmd ).strip ()
208
222
except (subprocess .CalledProcessError , OSError ) as exc :
209
- if not exit :
210
- return None
211
- print ("error: unable to run `{}`: {}" .format (' ' .join (cmd ), exc ))
212
- print ("Please make sure it's installed and in the path." )
213
- sys .exit (1 )
223
+ if exception :
224
+ raise
225
+ elif exit :
226
+ print ("error: unable to run `{}`: {}" .format (' ' .join (cmd ), exc ))
227
+ print ("Please make sure it's installed and in the path." )
228
+ sys .exit (1 )
229
+ return None
230
+
214
231
215
232
216
233
def format_build_time (duration ):
0 commit comments