1010
1111import argparse
1212import contextlib
13+ import hashlib
1314import os
1415import shutil
1516import subprocess
1819
1920def get (url , path , verbose = False ):
2021 print ("downloading " + url )
21- # see http://serverfault.com/questions/301128/how-to-download
22- if sys .platform == 'win32' :
23- run (["PowerShell.exe" , "/nologo" , "-Command" ,
24- "(New-Object System.Net.WebClient).DownloadFile('" + url +
25- "', '" + path + "')" ], verbose = verbose )
26- else :
27- run (["curl" , "-o" , path , url ], verbose = verbose )
22+ sha_url = url + ".sha256"
23+ sha_path = path + ".sha256"
24+ for _url , _path in ((url , path ), (sha_url , sha_path )):
25+ # see http://serverfault.com/questions/301128/how-to-download
26+ if sys .platform == 'win32' :
27+ run (["PowerShell.exe" , "/nologo" , "-Command" ,
28+ "(New-Object System.Net.WebClient)"
29+ ".DownloadFile('{}', '{}')" .format (_url , _path )],
30+ verbose = verbose )
31+ else :
32+ run (["curl" , "-o" , _path , _url ], verbose = verbose )
33+ print ("verifying " + path )
34+ with open (path , "rb" ) as f :
35+ found = hashlib .sha256 (f .read ()).hexdigest ()
36+ with open (sha_path , "r" ) as f :
37+ expected , _ = f .readline ().split ()
38+ if found != expected :
39+ err = ("invalid checksum:\n "
40+ " found: {}\n "
41+ " expected: {}" .format (found , expected ))
42+ if verbose :
43+ raise RuntimeError (err )
44+ sys .exit (err )
2845
2946def unpack (tarball , dst , verbose = False , match = None ):
3047 print ("extracting " + tarball )
@@ -57,9 +74,10 @@ def run(args, verbose=False):
5774 ret = subprocess .Popen (args )
5875 code = ret .wait ()
5976 if code != 0 :
60- if not verbose :
61- print ("failed to run: " + ' ' .join (args ))
62- raise RuntimeError ("failed to run command" )
77+ err = "failed to run: " + ' ' .join (args )
78+ if verbose :
79+ raise RuntimeError (err )
80+ sys .exit (err )
6381
6482class RustBuild :
6583 def download_rust_nightly (self ):
@@ -210,7 +228,10 @@ def build_triple(self):
210228 if sys .platform == 'win32' :
211229 return 'x86_64-pc-windows-msvc'
212230 else :
213- raise
231+ err = "uname not found"
232+ if self .verbose :
233+ raise Exception (err )
234+ sys .exit (err )
214235
215236 # Darwin's `uname -s` lies and always returns i386. We have to use
216237 # sysctl instead.
@@ -253,7 +274,10 @@ def build_triple(self):
253274 cputype = 'x86_64'
254275 ostype = 'pc-windows-gnu'
255276 else :
256- raise ValueError ("unknown OS type: " + ostype )
277+ err = "unknown OS type: " + ostype
278+ if self .verbose :
279+ raise ValueError (err )
280+ sys .exit (err )
257281
258282 if cputype in {'i386' , 'i486' , 'i686' , 'i786' , 'x86' }:
259283 cputype = 'i686'
@@ -269,7 +293,10 @@ def build_triple(self):
269293 elif cputype in {'amd64' , 'x86_64' , 'x86-64' , 'x64' }:
270294 cputype = 'x86_64'
271295 else :
272- raise ValueError ("unknown cpu type: " + cputype )
296+ err = "unknown cpu type: " + cputype
297+ if self .verbose :
298+ raise ValueError (err )
299+ sys .exit (err )
273300
274301 return cputype + '-' + ostype
275302
0 commit comments