Pretty-print HTTP errors when downloading a file.
This commit is contained in:
40
emsdk
40
emsdk
@@ -114,27 +114,31 @@ def download_file(url, dstpath, download_even_if_exists=False):
|
|||||||
if os.path.exists(file_name) and not download_even_if_exists:
|
if os.path.exists(file_name) and not download_even_if_exists:
|
||||||
print "File '" + file_name + "' already downloaded, skipping."
|
print "File '" + file_name + "' already downloaded, skipping."
|
||||||
return True
|
return True
|
||||||
u = urllib2.urlopen(url)
|
try:
|
||||||
mkdir_p(os.path.dirname(file_name))
|
u = urllib2.urlopen(url)
|
||||||
f = open(file_name, 'wb')
|
mkdir_p(os.path.dirname(file_name))
|
||||||
meta = u.info()
|
f = open(file_name, 'wb')
|
||||||
file_size = int(meta.getheaders("Content-Length")[0])
|
meta = u.info()
|
||||||
print "Downloading: %s from %s Bytes: %s" % (file_name, url, file_size)
|
file_size = int(meta.getheaders("Content-Length")[0])
|
||||||
|
print "Downloading: %s from %s Bytes: %s" % (file_name, url, file_size)
|
||||||
|
|
||||||
file_size_dl = 0
|
file_size_dl = 0
|
||||||
block_sz = 8192
|
block_sz = 8192
|
||||||
while True:
|
while True:
|
||||||
buffer = u.read(block_sz)
|
buffer = u.read(block_sz)
|
||||||
if not buffer:
|
if not buffer:
|
||||||
break
|
break
|
||||||
|
|
||||||
file_size_dl += len(buffer)
|
file_size_dl += len(buffer)
|
||||||
f.write(buffer)
|
f.write(buffer)
|
||||||
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
|
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
|
||||||
status = status + chr(8)*(len(status)+1)
|
status = status + chr(8)*(len(status)+1)
|
||||||
print status,
|
print status,
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
except urllib2.HTTPError, e:
|
||||||
|
print "HTTP error with URL '" + url + "': " + str(e)
|
||||||
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def run_get_output(cmd, cwd=None):
|
def run_get_output(cmd, cwd=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user