Skip to content

Commit c97579f

Browse files
committed
Migrate scm.clone() to Repo.clone() to unify repo cloning
1 parent 5c766dc commit c97579f

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

mbed/mbed.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,25 @@ def remove(self, dest, *args, **kwargs):
939939
pass
940940
return self.scm.remove(dest, *args, **kwargs)
941941

942+
def clone(self, url, path, depth=None, protocol=None, **kwargs):
943+
# Sorted so repositories that match urls are attempted first
944+
sorted_scms = [(scm.isurl(url), scm) for scm in scms.values()]
945+
sorted_scms = sorted(sorted_scms, key=lambda (m, _): not m)
946+
947+
for _, scm in sorted_scms:
948+
try:
949+
scm.clone(url, path, depth=depth, protocol=protocol, **kwargs)
950+
self.scm = scm
951+
self.url = url
952+
self.path = os.path.abspath(path)
953+
self.ignores()
954+
return True
955+
except ProcessException:
956+
if os.path.isdir(path):
957+
rmtree_readonly(path)
958+
else:
959+
return False
960+
942961
def getlibs(self):
943962
for root, dirs, files in os.walk(self.path):
944963
dirs[:] = [d for d in dirs if not d.startswith('.')]
@@ -1359,41 +1378,27 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
13591378
if cwd_type != "directory":
13601379
error("Cannot import program in the specified location \"%s\" because it's already part of a program.\n"
13611380
"Please change your working directory to a different location or use \"mbed add\" to import the URL as a library." % os.path.abspath(repo.path), 1)
1362-
else:
1363-
program = Program()
1364-
protocol = program.get_cfg('PROTOCOL', protocol)
1381+
1382+
protocol = Program().get_cfg('PROTOCOL', protocol)
13651383

13661384
if os.path.isdir(repo.path) and len(os.listdir(repo.path)) > 1:
13671385
error("Directory \"%s\" is not empty. Please ensure that the destination folder is empty." % repo.path, 1)
13681386

1369-
# Sorted so repositories that match urls are attempted first
1370-
sorted_scms = [(scm.isurl(url), scm) for scm in scms.values()]
1371-
sorted_scms = sorted(sorted_scms, key=lambda (m, _): not m)
1372-
13731387
text = "Importing program" if top else "Adding library"
13741388
action("%s \"%s\" from \"%s/\"%s" % (text, relpath(cwd_root, repo.path), repo.url, ' at '+(repo.revtype(repo.rev, True))))
1375-
for _, scm in sorted_scms:
1376-
try:
1377-
scm.clone(repo.url, repo.path, depth=depth, protocol=protocol)
1378-
repo.scm = scm
1379-
repo.ignores()
1380-
1381-
with cd(repo.path):
1382-
Program(repo.path).set_root()
1383-
try:
1384-
repo.checkout(repo.rev)
1385-
except ProcessException as e:
1386-
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
1387-
if depth:
1388-
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
1389-
if ignore:
1390-
warning(err)
1391-
else:
1392-
error(err, e[0])
1393-
break
1394-
except ProcessException:
1395-
if os.path.isdir(repo.path):
1396-
rmtree_readonly(repo.path)
1389+
if repo.clone(repo.url, repo.path, depth=depth, protocol=protocol):
1390+
with cd(repo.path):
1391+
Program(repo.path).set_root()
1392+
try:
1393+
repo.checkout(repo.rev)
1394+
except ProcessException as e:
1395+
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
1396+
if depth:
1397+
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
1398+
if ignore:
1399+
warning(err)
1400+
else:
1401+
error(err, e[0])
13971402
else:
13981403
err = "Unable to clone repository (%s)" % url
13991404
if ignore:

0 commit comments

Comments
 (0)