32
32
import urllib2
33
33
import zipfile
34
34
import argparse
35
+ import tempfile
35
36
36
37
37
38
# Application version
120
121
verbose = False
121
122
very_verbose = False
122
123
install_requirements = True
124
+ cache_repositories = True
123
125
124
126
# stores current working directory for recursive operations
125
127
cwd_root = ""
@@ -338,6 +340,7 @@ def untracked():
338
340
return ""
339
341
340
342
def seturl (url ):
343
+ info ("Setting url to \" %s\" in %s" % (url , os .getcwd ()))
341
344
if not os .path .exists ('.' + Bld .name ):
342
345
os .mkdir ('.' + Bld .name )
343
346
@@ -444,11 +447,36 @@ def outgoing():
444
447
raise e
445
448
return 0
446
449
450
+ def seturl (url ):
451
+ info ("Setting url to \" %s\" in %s" % (url , os .getcwd ()))
452
+ hgrc = os .path .join ('.hg' , 'hgrc' )
453
+ tagpaths = '[paths]'
454
+ remote = 'default'
455
+ lines = []
456
+
457
+ try :
458
+ with open (hgrc ) as f :
459
+ lines = f .read ().splitlines ()
460
+ except IOError :
461
+ pass
462
+
463
+ if tagpaths in lines :
464
+ idx = lines .index (tagpaths )
465
+ m = re .match (r'^([\w_]+)\s*=\s*(.*)$' , lines [idx + 1 ])
466
+ if m :
467
+ remote = m .group (1 )
468
+ del lines [idx + 1 ]
469
+ lines .insert (idx , remote + ' = ' + url )
470
+ else :
471
+ lines .append (tagpaths )
472
+ lines .append (remote + ' = ' + url )
473
+
447
474
def geturl ():
448
475
tagpaths = '[paths]'
449
476
default_url = ''
450
477
url = ''
451
- if os .path .isfile (os .path .join ('.hg' , 'hgrc' )):
478
+
479
+ try :
452
480
with open (os .path .join ('.hg' , 'hgrc' )) as f :
453
481
lines = f .read ().splitlines ()
454
482
if tagpaths in lines :
@@ -459,8 +487,11 @@ def geturl():
459
487
default_url = m .group (2 )
460
488
else :
461
489
url = m .group (2 )
462
- if default_url :
463
- url = default_url
490
+ except IOError :
491
+ pass
492
+
493
+ if default_url :
494
+ url = default_url
464
495
465
496
return formaturl (url or pquery ([hg_cmd , 'paths' , 'default' ]).strip ())
466
497
@@ -697,6 +728,10 @@ def getremotes(rtype='fetch'):
697
728
result .append ([remote [0 ], remote [1 ], t ])
698
729
return result
699
730
731
+ def seturl (url ):
732
+ info ("Setting url to \" %s\" in %s" % (url , os .getcwd ()))
733
+ return pquery ([git_cmd , 'remote' , 'set-url' , 'origin' , url ]).strip ()
734
+
700
735
def geturl ():
701
736
url = ""
702
737
remotes = Git .getremotes ()
@@ -814,7 +849,8 @@ def fromurl(cls, url, path=None):
814
849
else :
815
850
error ('Invalid repository (%s)' % url .strip (), - 1 )
816
851
817
- repo .cache = Program (repo .path ).get_cfg ('CACHE' )
852
+ if cache_repositories :
853
+ repo .cache = Program (repo .path ).get_cfg ('CACHE' ) or os .path .join (tempfile .gettempdir (), 'mbed-repo-cache' )
818
854
819
855
return repo
820
856
@@ -845,7 +881,8 @@ def fromrepo(cls, path=None):
845
881
846
882
repo .path = os .path .abspath (path )
847
883
repo .name = os .path .basename (repo .path )
848
- repo .cache = Program (repo .path ).get_cfg ('CACHE' )
884
+ if cache_repositories :
885
+ repo .cache = Program (repo .path ).get_cfg ('CACHE' ) or os .path .join (tempfile .gettempdir (), 'mbed-repo-cache' )
849
886
850
887
repo .sync ()
851
888
@@ -1003,10 +1040,12 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
1003
1040
shutil .copytree (cache , path )
1004
1041
1005
1042
with cd (path ):
1043
+ scm .seturl (formaturl (url , protocol ))
1006
1044
info ("Update cached copy from remote repository" )
1007
1045
scm .update (rev , True )
1008
1046
main = False
1009
1047
except (ProcessException , IOError ):
1048
+ info ("Discarding cached repository" )
1010
1049
if os .path .isdir (path ):
1011
1050
rmtree_readonly (path )
1012
1051
@@ -1652,7 +1691,8 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
1652
1691
with cd (repo .path ):
1653
1692
Program (repo .path ).set_root ()
1654
1693
try :
1655
- repo .checkout (repo .rev , True )
1694
+ if repo .getrev () != repo .rev :
1695
+ repo .checkout (repo .rev , True )
1656
1696
except ProcessException as e :
1657
1697
err = "Unable to update \" %s\" to %s" % (repo .name , repo .revtype (repo .rev , True ))
1658
1698
if depth :
0 commit comments