Skip to content

Commit 835debb

Browse files
committed
Introduce 'mbed cache' command to allow cache management and manipulation
1 parent ca46b42 commit 835debb

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

mbed/mbed.py

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,9 @@ def fromurl(cls, url, path=None):
995995
else:
996996
error('Invalid repository (%s)' % url.strip(), -1)
997997

998-
cache_cfg = Global().get_cfg('CACHE', '')
999-
if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled':
1000-
loc = cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'enabled') else None
1001-
repo.cache = loc or os.path.join(tempfile.gettempdir(), 'mbed-repo-cache')
998+
cache_cfg = Global().cache_cfg()
999+
if cache_repositories and cache_cfg['cache'] == 'enabled':
1000+
repo.cache = cache_cfg['cache_dir']
10021001

10031002
return repo
10041003

@@ -1030,10 +1029,9 @@ def fromrepo(cls, path=None):
10301029
repo.path = os.path.abspath(path)
10311030
repo.name = os.path.basename(repo.path)
10321031

1033-
cache_cfg = Global().get_cfg('CACHE', '')
1034-
if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled':
1035-
loc = cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'enabled') else None
1036-
repo.cache = loc or os.path.join(tempfile.gettempdir(), 'mbed-repo-cache')
1032+
cache_cfg = Global().cache_cfg()
1033+
if cache_repositories and cache_cfg['cache'] == 'enabled':
1034+
repo.cache = cache_cfg['cache_dir']
10371035

10381036
repo.sync()
10391037

@@ -1636,6 +1634,10 @@ def set_cfg(self, *args, **kwargs):
16361634
def list_cfg(self, *args, **kwargs):
16371635
return Cfg(self.path).list(*args, **kwargs)
16381636

1637+
def cache_cfg(self, *args, **kwargs):
1638+
return Cfg(self.path).cache(*args, **kwargs)
1639+
1640+
16391641
# Cfg classed used for handling the config backend
16401642
class Cfg(object):
16411643
path = None
@@ -1701,6 +1703,16 @@ def list(self):
17011703
vars[m.group(1)] = m.group(2)
17021704
return vars
17031705

1706+
# Get cache configuration
1707+
def cache(self):
1708+
cache_cfg = self.get('CACHE', None)
1709+
cache_val = 'enabled' if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled' else 'disabled'
1710+
1711+
cache_dir_cfg = self.get('CACHE_DIR', None)
1712+
loc = cache_dir_cfg if cache_dir_cfg != 'default' else (cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'off' and cache_cfg != 'none' and cache_cfg != 'enabled' and cache_cfg != 'disabled') else None)
1713+
cache_base = loc or tempfile.gettempdir()
1714+
return {'cache': cache_val, 'cache_base': cache_base, 'cache_dir': os.path.join(cache_base, 'mbed-cache')}
1715+
17041716

17051717
def formaturl(url, format="default"):
17061718
url = "%s" % url
@@ -2746,11 +2758,12 @@ def target_(name=None, global_cfg=False, supported=False):
27462758
return compile_(supported=supported)
27472759
return config_('target', name, global_cfg=global_cfg)
27482760

2761+
27492762
@subcommand('toolchain',
27502763
dict(name='name', nargs='?', help='Default toolchain name. Example: ARM, GCC_ARM, IAR'),
27512764
dict(name=['-G', '--global'], dest='global_cfg', action='store_true', help='Use global settings, not local'),
27522765
dict(name=['-S', '--supported'], dest='supported', action='store_true', help='Shows supported matrix of targets and toolchains'),
2753-
help='Set or get default toolchain\n\n',
2766+
help='Set or get default toolchain',
27542767
description=(
27552768
"Set or get default toolchain\n"
27562769
"This is an alias to 'mbed config [--global] toolchain [name]'\n"))
@@ -2759,6 +2772,54 @@ def toolchain_(name=None, global_cfg=False, supported=False):
27592772
return compile_(supported=supported)
27602773
return config_('toolchain', name, global_cfg=global_cfg)
27612774

2775+
2776+
@subcommand('cache',
2777+
dict(name='on', nargs='?', help='Turn repository caching on. Will use either a default cache location or the specified cache directory to store repositories.'),
2778+
dict(name='off', nargs='?', help='Turn repository caching off. Note that this doesn\'t purge cached repositories. See "purge".'),
2779+
dict(name='ls', nargs='?', help='List cached repositories'),
2780+
dict(name='purge', nargs='?', help='Purge cached repositories. Note that this doesn\'t turn caching off'),
2781+
dict(name=['-D', '--dir'], dest='cache_dir', help='Set cache directory. Set to "default" to let mbed CLI determine the cache directory location.'),
2782+
help='Repository cache management\n\n',
2783+
description=(
2784+
"Set or get default toolchain\n"))
2785+
def cache_(on=False, off=False, ls=False, purge=False, cache_dir=None, global_cfg=False):
2786+
cmd = str(on).lower()
2787+
argument = off
2788+
g = Global()
2789+
2790+
if cache_dir:
2791+
if not os.path.exists(cache_dir):
2792+
try:
2793+
os.makedirs(cache_dir)
2794+
except (IOError, ImportError, OSError):
2795+
error("Unable to create cache directory \"%s\"" % cache_dir, 128)
2796+
elif not os.path.isdir(cache_dir):
2797+
error("The specified location \"%s\" is not a directory" % cache_dir, 128)
2798+
elif len(os.listdir(cache_dir)) > 1:
2799+
warning("Directory \"%s\" is not empty." % d_path)
2800+
2801+
g.set_cfg('CACHE_DIR', cache_dir)
2802+
action('Repository cache location set to \"%s\"' % cache_dir)
2803+
2804+
if cmd == 'off' or cmd == 'on':
2805+
g.set_cfg('CACHE', 'enabled' if cmd == 'on' else 'disabled');
2806+
cfg = g.cache_cfg()
2807+
action('Repository cache is now %s.' % str(cfg['cache']).upper())
2808+
action('Cache location \"%s\"' % cfg['cache_dir'])
2809+
elif cmd == 'ls':
2810+
print 'list'
2811+
elif cmd == 'purge':
2812+
cfg = g.cache_cfg()
2813+
action('Purging cached repositories in \"%s\"' % cfg['cache_base'])
2814+
if os.path.isdir(path):
2815+
rmtree_readonly(cfg['cache_dir'])
2816+
action('Complete')
2817+
else:
2818+
cfg = g.cache_cfg()
2819+
action('Repository cache is %s.' % str(cfg['cache']).upper())
2820+
action('Cache location \"%s\"' % cfg['cache_base'])
2821+
2822+
27622823
@subcommand('help',
27632824
help='This help screen')
27642825
def help_():

0 commit comments

Comments
 (0)