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:
122
emsdk
122
emsdk
@@ -110,49 +110,54 @@ 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
|
||||||
with zipfile.ZipFile(source_filename) as zf:
|
try:
|
||||||
# Implement '--strip 1' behavior to unzipping by testing if all the files in the zip reside in a common subdirectory, and if so,
|
with zipfile.ZipFile(source_filename) as zf:
|
||||||
# we move the output tree at the end of uncompression step.
|
# Implement '--strip 1' behavior to unzipping by testing if all the files in the zip reside in a common subdirectory, and if so,
|
||||||
for member in zf.infolist():
|
# we move the output tree at the end of uncompression step.
|
||||||
words = member.filename.split('/')
|
for member in zf.infolist():
|
||||||
if common_subdir == None:
|
words = member.filename.split('/')
|
||||||
common_subdir = words[0]
|
if len(words) > 1: # If there is a directory component?
|
||||||
elif common_subdir != words[0]:
|
if common_subdir == None:
|
||||||
common_subdir = ''
|
common_subdir = words[0]
|
||||||
break
|
elif common_subdir != words[0]:
|
||||||
|
common_subdir = ''
|
||||||
|
break
|
||||||
|
|
||||||
unzip_to_dir = dest_dir
|
unzip_to_dir = dest_dir
|
||||||
if common_subdir:
|
if common_subdir:
|
||||||
unzip_to_dir = os.path.join('/'.join(dest_dir.split('/')[:-1]), 'unzip_temp')
|
unzip_to_dir = os.path.join('/'.join(dest_dir.split('/')[:-1]), 'unzip_temp')
|
||||||
|
|
||||||
# Now do the actual decompress.
|
# Now do the actual decompress.
|
||||||
for member in zf.infolist():
|
for member in zf.infolist():
|
||||||
# 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)
|
zf.extract(member, unzip_to_dir)
|
||||||
# print "Extracting " + member.filename + " to " + path
|
|
||||||
# zf.extract(member, path)
|
|
||||||
# print "Extracting " + member.filename + " to " + unzip_to_dir
|
|
||||||
zf.extract(member, unzip_to_dir)
|
|
||||||
|
|
||||||
if common_subdir:
|
if common_subdir:
|
||||||
try:
|
try:
|
||||||
if os.path.exists(dest_dir):
|
if os.path.exists(dest_dir):
|
||||||
shutil.rmtree(dest_dir)
|
shutil.rmtree(dest_dir)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
shutil.copytree(os.path.join(unzip_to_dir, common_subdir), dest_dir)
|
shutil.copytree(os.path.join(unzip_to_dir, common_subdir), dest_dir)
|
||||||
try:
|
try:
|
||||||
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,27 +195,30 @@ 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)
|
||||||
|
|
||||||
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()
|
|
||||||
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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user