@@ -110,29 +110,42 @@ def download(path, url, probably_big, verbose, help_on_error=None):
110110
111111
112112def _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
113118 if probably_big or verbose :
114119 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 :
123123 if probably_big or verbose :
124124 option = "-#"
125125 else :
126126 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 )
128130 run (["curl" , option ,
129131 "-L" , # Follow redirect.
130132 "-y" , "30" , "-Y" , "10" , # timeout if speed is < 10 bytes/sec for > 30 seconds
131133 "--connect-timeout" , "30" , # timeout if cannot connect within 30 seconds
132134 "--retry" , "3" , "-Sf" , "-o" , path , url ],
133135 verbose = verbose ,
134- exception = exception ,
136+ exception = True , # Will raise RuntimeError on failure
135137 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
136149
137150
138151def verify (path , expected , verbose ):
@@ -198,19 +211,23 @@ def run(args, verbose=False, exception=False, is_bootstrap=False, help_on_error=
198211 sys .exit (err )
199212
200213
201- def require (cmd , exit = True ):
214+ def require (cmd , exit = True , exception = False ):
202215 '''Run a command, returning its output.
203216 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.'''
206220 try :
207221 return subprocess .check_output (cmd ).strip ()
208222 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+
214231
215232
216233def format_build_time (duration ):
0 commit comments