If URL download fails, delete the partially downloaded file (if it exists). If unzipping fails, print an error, and if the zip was corrupted, delete the corrupted file. Fixes https://github.com/kripken/emscripten/issues/1687.

This commit is contained in:
Jukka Jylänki
2013-11-19 15:26:45 +02:00
parent a6508d1479
commit 082f334c22

22
emsdk
View File

@@ -110,11 +110,13 @@ def unzip(source_filename, dest_dir, unpack_even_if_exists=False):
print "Unpacking '" + source_filename + "' to '" + dest_dir + "'" print "Unpacking '" + source_filename + "' to '" + dest_dir + "'"
mkdir_p(dest_dir) mkdir_p(dest_dir)
common_subdir = None common_subdir = None
try:
with zipfile.ZipFile(source_filename) as zf: with zipfile.ZipFile(source_filename) as zf:
# Implement '--strip 1' behavior to unzipping by testing if all the files in the zip reside in a common subdirectory, and if so, # Implement '--strip 1' behavior to unzipping by testing if all the files in the zip reside in a common subdirectory, and if so,
# we move the output tree at the end of uncompression step. # we move the output tree at the end of uncompression step.
for member in zf.infolist(): for member in zf.infolist():
words = member.filename.split('/') words = member.filename.split('/')
if len(words) > 1: # If there is a directory component?
if common_subdir == None: if common_subdir == None:
common_subdir = words[0] common_subdir = words[0]
elif common_subdir != words[0]: elif common_subdir != words[0]:
@@ -130,16 +132,12 @@ def unzip(source_filename, dest_dir, unpack_even_if_exists=False):
# Path traversal defense copied from # Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789 # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = member.filename.split('/') words = member.filename.split('/')
# print "words: " + str(words)
path = unzip_to_dir path = unzip_to_dir
for word in words[:-1]: for word in words[:-1]:
drive, word = os.path.splitdrive(word) drive, word = os.path.splitdrive(word)
head, word = os.path.split(word) head, word = os.path.split(word)
if word in (os.curdir, os.pardir, ''): continue if word in (os.curdir, os.pardir, ''): continue
path = os.path.join(path, word) path = os.path.join(path, word)
# print "Extracting " + member.filename + " to " + path
# zf.extract(member, path)
# print "Extracting " + member.filename + " to " + unzip_to_dir
zf.extract(member, unzip_to_dir) zf.extract(member, unzip_to_dir)
if common_subdir: if common_subdir:
@@ -153,6 +151,13 @@ def unzip(source_filename, dest_dir, unpack_even_if_exists=False):
shutil.rmtree(unzip_to_dir) shutil.rmtree(unzip_to_dir)
except: except:
pass pass
except zipfile.BadZipfile, e:
print "Unzipping file '" + source_filename + "' failed due to reason: " + str(e) + "! Removing the corrupted zip file."
rmfile(source_filename)
return False
except Exception, e:
print "Unzipping file '" + source_filename + "' failed due to reason: " + str(e)
return False
return True return True
@@ -190,7 +195,7 @@ def download_file(url, dstpath, download_even_if_exists=False):
try: try:
u = urllib2.urlopen(url) u = urllib2.urlopen(url)
mkdir_p(os.path.dirname(file_name)) mkdir_p(os.path.dirname(file_name))
f = open(file_name, 'wb') with open(file_name, 'wb') as f:
meta = u.info() meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0]) file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s from %s Bytes: %s" % (file_name, url, file_size) print "Downloading: %s from %s Bytes: %s" % (file_name, url, file_size)
@@ -207,10 +212,13 @@ def download_file(url, dstpath, download_even_if_exists=False):
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()
except urllib2.HTTPError, e: except urllib2.HTTPError, e:
print "HTTP error with URL '" + url + "': " + str(e) print "HTTP error with URL '" + url + "': " + str(e)
rmfile(file_name)
return None
except:
print "Error downloading URL '" + url + "'!"
rmfile(file_name)
return None return None
return file_name return file_name