Skip to content

Commit 64d8ef1

Browse files
committed
Improved error handling when download failed or zip archive is corrupt
1 parent b110dbc commit 64d8ef1

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

mbed/mbed.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,31 +255,31 @@ def clone(url, path=None, hash=None, depth=None, protocol=None):
255255
if not os.path.exists(arch_dir):
256256
action("Downloading mbed library build \"%s\" (might take a minute)" % hash)
257257
Bld.dlunzip(arch_url, hash)
258-
259258
except Exception as e:
260-
#with cd(path):
261-
# if os.path.exists(arch_dir):
262-
# rmtree_readonly(arch_dir)
263-
print e[0]
259+
with cd(path):
260+
if os.path.exists(arch_dir):
261+
rmtree_readonly(arch_dir)
264262
error(e[1], e[0])
265263

266264
def dlunzip(url, hash):
267-
tmp_file = '.temp-' + hash + '.zip'
265+
tmp_file = '.rev-' + hash + '.zip'
268266
arch_dir = 'mbed-' + hash
269267
try:
270268
if not os.path.exists(tmp_file):
271269
urllib.urlretrieve(url, tmp_file)
272-
except Exception as e:
270+
except:
273271
if os.path.isfile(tmp_file):
274272
os.remove(tmp_file)
275273
raise Exception(128, "Download failed!\nPlease try again later.")
276274

277-
with zipfile.ZipFile(tmp_file) as zf:
278-
try:
275+
try:
276+
with zipfile.ZipFile(tmp_file) as zf:
279277
action("Unpacking mbed library build \"%s\" in \"%s\"" % (hash, os.getcwd()))
280278
zf.extractall()
281-
except:
282-
raise Exception("An error occurred while unpacking mbed library ZIP \"%s\" in \"%s\"" % (tmp_file, os.getcwd()))
279+
except:
280+
if os.path.isfile(tmp_file):
281+
os.remove(tmp_file)
282+
raise Exception(128, "An error occurred while unpacking mbed library archive \"%s\" in \"%s\"" % (tmp_file, os.getcwd()))
283283

284284

285285
def add(file):

0 commit comments

Comments
 (0)