Skip to content

Commit e9257c3

Browse files
committed
Introduce SCM.seturl(url) method for Git, Hg and Bld scms
Caching is enabled by default and uses the system/user default temp directory. Can be overriden via the 'CACHE' config setting
1 parent 0a191b9 commit e9257c3

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

mbed/mbed.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import urllib2
3333
import zipfile
3434
import argparse
35+
import tempfile
3536

3637

3738
# Application version
@@ -120,6 +121,7 @@
120121
verbose = False
121122
very_verbose = False
122123
install_requirements = True
124+
cache_repositories = True
123125

124126
# stores current working directory for recursive operations
125127
cwd_root = ""
@@ -338,6 +340,7 @@ def untracked():
338340
return ""
339341

340342
def seturl(url):
343+
info("Setting url to \"%s\" in %s" % (url, os.getcwd()))
341344
if not os.path.exists('.'+Bld.name):
342345
os.mkdir('.'+Bld.name)
343346

@@ -444,11 +447,36 @@ def outgoing():
444447
raise e
445448
return 0
446449

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+
447474
def geturl():
448475
tagpaths = '[paths]'
449476
default_url = ''
450477
url = ''
451-
if os.path.isfile(os.path.join('.hg', 'hgrc')):
478+
479+
try:
452480
with open(os.path.join('.hg', 'hgrc')) as f:
453481
lines = f.read().splitlines()
454482
if tagpaths in lines:
@@ -459,8 +487,11 @@ def geturl():
459487
default_url = m.group(2)
460488
else:
461489
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
464495

465496
return formaturl(url or pquery([hg_cmd, 'paths', 'default']).strip())
466497

@@ -697,6 +728,10 @@ def getremotes(rtype='fetch'):
697728
result.append([remote[0], remote[1], t])
698729
return result
699730

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+
700735
def geturl():
701736
url = ""
702737
remotes = Git.getremotes()
@@ -814,7 +849,8 @@ def fromurl(cls, url, path=None):
814849
else:
815850
error('Invalid repository (%s)' % url.strip(), -1)
816851

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')
818854

819855
return repo
820856

@@ -845,7 +881,8 @@ def fromrepo(cls, path=None):
845881

846882
repo.path = os.path.abspath(path)
847883
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')
849886

850887
repo.sync()
851888

@@ -1003,10 +1040,12 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
10031040
shutil.copytree(cache, path)
10041041

10051042
with cd(path):
1043+
scm.seturl(formaturl(url, protocol))
10061044
info("Update cached copy from remote repository")
10071045
scm.update(rev, True)
10081046
main = False
10091047
except (ProcessException, IOError):
1048+
info("Discarding cached repository")
10101049
if os.path.isdir(path):
10111050
rmtree_readonly(path)
10121051

@@ -1652,7 +1691,8 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
16521691
with cd(repo.path):
16531692
Program(repo.path).set_root()
16541693
try:
1655-
repo.checkout(repo.rev, True)
1694+
if repo.getrev() != repo.rev:
1695+
repo.checkout(repo.rev, True)
16561696
except ProcessException as e:
16571697
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
16581698
if depth:

0 commit comments

Comments
 (0)