@@ -939,6 +939,25 @@ def remove(self, dest, *args, **kwargs):
939
939
pass
940
940
return self .scm .remove (dest , * args , ** kwargs )
941
941
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
+
942
961
def getlibs (self ):
943
962
for root , dirs , files in os .walk (self .path ):
944
963
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):
1359
1378
if cwd_type != "directory" :
1360
1379
error ("Cannot import program in the specified location \" %s\" because it's already part of a program.\n "
1361
1380
"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 )
1365
1383
1366
1384
if os .path .isdir (repo .path ) and len (os .listdir (repo .path )) > 1 :
1367
1385
error ("Directory \" %s\" is not empty. Please ensure that the destination folder is empty." % repo .path , 1 )
1368
1386
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
-
1373
1387
text = "Importing program" if top else "Adding library"
1374
1388
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 + ("\n The --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 + ("\n The --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 ])
1397
1402
else :
1398
1403
err = "Unable to clone repository (%s)" % url
1399
1404
if ignore :
0 commit comments