Skip to content

Commit 707e1cb

Browse files
committed
Refactor the Bld.clone(), init(), update() logic to match Hg and Git routines. Added ability to continue deploy/add/import/update process and ignore errors via --ignore switch (e.g. continue checking out other libraries even if one was deleted or revision log rewritten) (#146)
1 parent 9a83bb0 commit 707e1cb

File tree

1 file changed

+63
-35
lines changed

1 file changed

+63
-35
lines changed

mbed/mbed.py

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
# mbed url is subset of hg. mbed doesn't support ssh transport
8787
regex_mbed_url = r'^(https?)://([\w\-\.]*mbed\.(co\.uk|org|com))/(users|teams)/([\w\-]{1,32})/(repos|code)/([\w\-]+)/?$'
88-
regex_build_url = r'^(https?://([\w\-\.]*mbed\.(co\.uk|org|com))/(users|teams)/([\w\-]{1,32})/(repos|code)/([\w\-]+))/builds/?([\w\-]{12,40})?/?$'
88+
regex_build_url = r'^(https?://([\w\-\.]*mbed\.(co\.uk|org|com))/(users|teams)/([\w\-]{1,32})/(repos|code)/([\w\-]+))/builds/?([\w\-]{12,40}|tip)?/?$'
8989

9090
# default mbed OS url
9191
mbed_os_url = 'https://github.com/ARMmbed/mbed-os'
@@ -231,34 +231,17 @@ def init(path, url):
231231
if not os.path.exists(path):
232232
os.mkdir(path)
233233
with cd(path):
234-
if not os.path.exists('.'+Bld.name):
235-
os.mkdir('.'+Bld.name)
236-
237-
fl = os.path.join('.'+Bld.name, 'bldrc')
238-
try:
239-
with open(fl, 'w') as f:
240-
f.write(url)
241-
except IOError:
242-
error("Unable to write bldrc file in \"%s\"" % fl, 1)
234+
Bld.seturl(url)
243235

244236
def clone(url, path=None, rev=None, depth=None, protocol=None):
245237
m = Bld.isurl(url)
246238
if not m:
247239
raise ProcessException(1, "Not an mbed library build URL")
248240
return False
249241

250-
arch_url = m.group(1) + '/archive/' + rev + '.zip'
251-
arch_dir = m.group(7) + '-' + rev
252-
253242
try:
254-
Bld.init(path, url+'/'+rev)
255-
with cd(path):
256-
if not os.path.exists(arch_dir):
257-
Bld.dlunzip(arch_url, rev)
243+
Bld.init(path, url+'/tip')
258244
except Exception as e:
259-
with cd(path):
260-
if os.path.exists(arch_dir):
261-
rmtree_readonly(arch_dir)
262245
error(e[1], e[0])
263246

264247
def dlunzip(url, rev):
@@ -284,7 +267,6 @@ def dlunzip(url, rev):
284267
rmtree_readonly(arch_dir)
285268
raise Exception(128, "An error occurred while unpacking mbed library archive \"%s\" in \"%s\"" % (tmp_file, os.getcwd()))
286269

287-
288270
def add(dest):
289271
return True
290272

@@ -302,22 +284,37 @@ def publish(repo, all=None):
302284
def fetch(repo):
303285
error("mbed library builds do not support pushing")
304286

287+
def checkout(repo, rev, clean=False):
288+
url = Bld.geturl()
289+
m = Bld.isurl(url)
290+
if not m:
291+
raise ProcessException(1, "Not an mbed library build URL")
292+
return False
293+
294+
arch_url = m.group(1) + '/archive/' + rev + '.zip'
295+
arch_dir = m.group(7) + '-' + rev
296+
297+
try:
298+
if not os.path.exists(arch_dir):
299+
Bld.dlunzip(arch_url, rev)
300+
except Exception as e:
301+
if os.path.exists(arch_dir):
302+
rmtree_readonly(arch_dir)
303+
error(e[1], e[0])
304+
Bld.seturl(url+'/'+rev)
305+
305306
def update(repo, rev=None, clean=False):
306307
m = Bld.isurl(repo.url)
307308
rev = Hg.remoteid(m.group(1), rev)
308-
309309
if not rev:
310310
error("Unable to fetch late mbed library revision")
311311

312312
if rev != repo.rev:
313-
log("Cleaning up build folder")
314-
for fl in os.listdir(repo.path):
313+
log("Cleaning up library build folder")
314+
for fl in os.listdir('.'):
315315
if not fl.startswith('.'):
316-
if os.path.isfile(os.path.join(repo.path, fl)):
317-
os.remove(os.path.join(repo.path, fl))
318-
else:
319-
shutil.rmtree(os.path.join(repo.path, fl))
320-
return Bld.clone(repo.url, repo.path, rev)
316+
os.remove(fl) if os.path.isfile(fl) else shutil.rmtree(fl)
317+
return Bld.checkout(repo, rev)
321318

322319
def status():
323320
return False
@@ -334,17 +331,28 @@ def outgoing():
334331
def isdetached():
335332
return False
336333

334+
def seturl(url):
335+
if not os.path.exists('.'+Bld.name):
336+
os.mkdir('.'+Bld.name)
337+
338+
fl = os.path.join('.'+Bld.name, 'bldrc')
339+
try:
340+
with open(fl, 'w') as f:
341+
f.write(url)
342+
except IOError:
343+
error("Unable to write bldrc file in \"%s\"" % fl, 1)
344+
337345
def geturl():
338346
with open(os.path.join('.bld', 'bldrc')) as f:
339347
url = f.read().strip()
340348
m = Bld.isurl(url)
341-
return m.group(1)+'/builds'
349+
return m.group(1)+'/builds' if m else ''
342350

343351
def getrev():
344352
with open(os.path.join('.bld', 'bldrc')) as f:
345353
url = f.read().strip()
346354
m = Bld.isurl(url)
347-
return m.group(8)
355+
return m.group(8) if m else ''
348356

349357
def getbranch():
350358
return "default"
@@ -414,6 +422,8 @@ def fetch(repo):
414422
popen([hg_cmd, 'pull'] + (['-v'] if verbose else ['-q']))
415423

416424
def checkout(repo, rev, clean=False):
425+
if not rev:
426+
return False
417427
if repo:
418428
log("Checkout \"%s\" to %s" % (repo.name, repo.revtype(rev, True)))
419429
popen([hg_cmd, 'update'] + (['-r', rev] if rev else []) + (['-C'] if clean else []) + (['-v'] if verbose else ['-q']))
@@ -613,6 +623,8 @@ def discard(repo):
613623
popen([git_cmd, 'clean', '-fdq'] + ([] if verbose else ['-q'])) # cleans up untracked files and folders
614624

615625
def checkout(repo, rev, clean=False):
626+
if not rev:
627+
return False
616628
if repo:
617629
log("Checkout \"%s\" to %s" % (repo.name, repo.revtype(rev, True)))
618630
popen([git_cmd, 'checkout', rev] + ([] if verbose else ['-q']))
@@ -1332,7 +1344,15 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
13321344
action("%s \"%s\" from \"%s/\"%s" % (text, relpath(cwd_root, repo.path), repo.url, ' at '+(repo.revtype(repo.rev, True))))
13331345
for _, scm in sorted_scms:
13341346
try:
1335-
scm.clone(repo.url, repo.path, repo.rev, depth=depth, protocol=protocol)
1347+
scm.clone(repo.url, repo.path, depth=depth, protocol=protocol)
1348+
with cd(repo.path):
1349+
try:
1350+
scm.checkout(repo, repo.rev, True)
1351+
except ProcessException as e:
1352+
err = "Unable to checkout \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
1353+
if depth:
1354+
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
1355+
warning(err) if ignore else error(err, e[0])
13361356
break
13371357
except ProcessException:
13381358
if os.path.isdir(repo.path):
@@ -1346,7 +1366,7 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
13461366
cwd_root = repo.path
13471367

13481368
with cd(repo.path):
1349-
deploy(ignore, depth=depth, protocol=protocol, top=False)
1369+
deploy(ignore=ignore, depth=depth, protocol=protocol, top=False)
13501370

13511371
if top:
13521372
program = Program(repo.path)
@@ -1385,7 +1405,7 @@ def deploy(ignore=False, depth=None, protocol=None, top=True):
13851405
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
13861406
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
13871407
help='Add a library and its dependencies into the current %s or specified destination path.' % cwd_type)
1388-
def add(url, path=None, depth=None, protocol=None):
1408+
def add(url, path=None, ignore=False, depth=None, protocol=None):
13891409
repo = Repo.fromrepo()
13901410

13911411
lib = Repo.fromurl(url, path)
@@ -1480,7 +1500,15 @@ def update(rev=None, clean=False, force=False, ignore=False, top=True, depth=Non
14801500
cwd_type if top else cwd_dest,
14811501
os.path.basename(repo.path) if top else relpath(cwd_root, repo.path),
14821502
repo.revtype(rev, True)))
1483-
repo.scm.update(repo, rev, clean)
1503+
1504+
try:
1505+
repo.scm.update(repo, rev, clean)
1506+
except ProcessException as e:
1507+
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev, True))
1508+
if depth:
1509+
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
1510+
warning(err) if ignore else error(err, e[0])
1511+
14841512
repo.rm_untracked()
14851513
if top and cwd_type == 'library':
14861514
repo.sync()

0 commit comments

Comments
 (0)