Skip to content

Commit 34afc40

Browse files
committed
Refine handling of mbed library builds
* Separate cleanup, fetch, unpack and checkout routines * Respect -c/--clean flag for library builds as well (e.g. cleanup and unpack to ensure changes are discarded) * Fetch before cleaning up, which makes download errors non-fatal for the current state of the user code
1 parent 01266cd commit 34afc40

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

mbed/mbed.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def init(path):
266266
def clone(url, path=None, depth=None, protocol=None):
267267
m = Bld.isurl(url)
268268
if not m:
269-
raise ProcessException(1, "Not an mbed library build URL")
269+
raise ProcessException(1, "Not a library build URL")
270270

271271
try:
272272
Bld.init(path)
@@ -275,61 +275,61 @@ def clone(url, path=None, depth=None, protocol=None):
275275
except Exception as e:
276276
error(e[1], e[0])
277277

278+
def cleanup():
279+
info("Cleaning up library build folder")
280+
for fl in os.listdir('.'):
281+
if not fl.startswith('.'):
282+
if os.path.isfile(fl):
283+
os.remove(fl)
284+
else:
285+
shutil.rmtree(fl)
286+
278287
def fetch_rev(url, rev):
279-
tmp_file = os.path.join('.'+Bld.name, '.rev-' + rev + '.zip')
280-
arch_dir = 'mbed-' + rev
288+
rev_file = os.path.join('.'+Bld.name, '.rev-' + rev + '.zip')
281289
try:
282-
if not os.path.exists(tmp_file):
283-
action("Downloading mbed library build \"%s\" (might take a minute)" % rev)
290+
if not os.path.exists(rev_file):
291+
action("Downloading library build \"%s\" (might take a minute)" % rev)
284292
outfd = open(tmp_file, 'wb')
285293
inurl = urllib2.urlopen(url)
286294
outfd.write(inurl.read())
287295
outfd.close()
288296
except:
289-
if os.path.isfile(tmp_file):
290-
os.remove(tmp_file)
297+
if os.path.isfile(rev_file):
298+
os.remove(rev_file)
291299
raise Exception(128, "Download failed!\nPlease try again later.")
292300

301+
def unpack_rev(rev):
302+
rev_file = os.path.join('.'+Bld.name, '.rev-' + rev + '.zip')
293303
try:
294-
with zipfile.ZipFile(tmp_file) as zf:
295-
action("Unpacking mbed library build \"%s\" in \"%s\"" % (rev, os.getcwd()))
296-
zf.extractall()
304+
with zipfile.ZipFile(rev_file) as zf:
305+
action("Unpacking library build \"%s\" in \"%s\"" % (rev, os.getcwd()))
306+
zf.extractall('.')
297307
except:
298-
if os.path.isfile(tmp_file):
299-
os.remove(tmp_file)
300-
if os.path.isfile(arch_dir):
301-
rmtree_readonly(arch_dir)
302-
raise Exception(128, "An error occurred while unpacking mbed library archive \"%s\" in \"%s\"" % (tmp_file, os.getcwd()))
308+
if os.path.isfile(rev_file):
309+
os.remove(rev_file)
310+
raise Exception(128, "An error occurred while unpacking library archive \"%s\" in \"%s\"" % (rev_file, os.getcwd()))
303311

304312
def checkout(rev, clean=False):
305313
url = Bld.geturl()
306314
m = Bld.isurl(url)
307315
if not m:
308-
raise ProcessException(1, "Not an mbed library build URL")
316+
raise ProcessException(1, "Not a library build URL")
309317
rev = Hg.remoteid(m.group(1), rev)
310318
if not rev:
311-
error("Unable to fetch late mbed library revision")
312-
313-
if rev != Bld.getrev():
314-
info("Cleaning up library build folder")
315-
for fl in os.listdir('.'):
316-
if not fl.startswith('.'):
317-
if os.path.isfile(fl):
318-
os.remove(fl)
319-
else:
320-
shutil.rmtree(fl)
319+
error("Unable to fetch library build information")
320+
321+
arch_url = m.group(1) + '/archive/' + rev + '.zip'
322+
Bld.fetch_rev(arch_url, rev)
323+
324+
if rev != Bld.getrev() or clean:
325+
Bld.cleanup()
321326

322327
info("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
323-
arch_url = m.group(1) + '/archive/' + rev + '.zip'
324-
arch_dir = m.group(7) + '-' + rev
325328
try:
326-
if not os.path.exists(arch_dir):
327-
Bld.fetch_rev(arch_url, rev)
329+
Bld.unpack_rev(rev)
330+
Bld.seturl(url+'/'+rev)
328331
except Exception as e:
329-
if os.path.exists(arch_dir):
330-
rmtree_readonly(arch_dir)
331332
error(e[1], e[0])
332-
Bld.seturl(url+'/'+rev)
333333

334334
def update(rev=None, clean=False, clean_files=False, is_local=False):
335335
return Bld.checkout(rev, clean)

0 commit comments

Comments
 (0)