Skip to content

Commit a314b33

Browse files
committed
Change mbed cache --dir <path> to mbed cache dir <path> for consistency
Also improve help (a lot)
1 parent c476875 commit a314b33

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

mbed/mbed.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,47 +2780,48 @@ def toolchain_(name=None, global_cfg=False, supported=False):
27802780

27812781

27822782
@subcommand('cache',
2783-
dict(name='on', nargs='?', help='Turn repository caching on. Will use either a default cache location or the specified cache directory to store repositories.'),
2783+
dict(name='on', nargs='?', help='Turn repository caching on. Will use either the default or the user specified cache directory.'),
27842784
dict(name='off', nargs='?', help='Turn repository caching off. Note that this doesn\'t purge cached repositories. See "purge".'),
2785-
dict(name='ls', nargs='?', help='List cached repositories'),
2785+
dict(name='dir', nargs='?', help='Set cache directory. Set to "default" to let mbed CLI determine the cache directory location. Typically this is "~/.mbed/mbed-cache/" on UNIX, or "%%userprofile%%/.mbed/mbed-cache/" on Windows.'),
2786+
dict(name='ls', nargs='?', help='List cached repositories and their sizes.'),
27862787
dict(name='purge', nargs='?', help='Purge cached repositories. Note that this doesn\'t turn caching off'),
2787-
dict(name=['-D', '--dir'], dest='cache_dir', help='Set cache directory. Set to "default" to let mbed CLI determine the cache directory location.'),
27882788
help='Repository cache management\n\n',
27892789
description=(
2790-
"Set or get default toolchain\n"))
2791-
def cache_(on=False, off=False, ls=False, purge=False, cache_dir=None, global_cfg=False):
2790+
"Repository cache management\n"
2791+
"To minimize traffic and reduce import times, Mbed CLI can cache repositories by storing their indexes.\n"
2792+
"By default repository caching is turned on. Turn it off if you experience any problems.\n"))
2793+
def cache_(on=False, off=False, dir=None, ls=False, purge=False, global_cfg=False):
27922794
cmd = str(on).lower()
27932795
argument = off
27942796
g = Global()
27952797

2796-
if cache_dir:
2797-
if not os.path.exists(cache_dir):
2798-
try:
2799-
os.makedirs(cache_dir)
2800-
except (IOError, ImportError, OSError):
2801-
error("Unable to create cache directory \"%s\"" % cache_dir, 128)
2802-
elif not os.path.isdir(cache_dir):
2803-
error("The specified location \"%s\" is not a directory" % cache_dir, 128)
2804-
elif len(os.listdir(cache_dir)) > 1:
2805-
warning("Directory \"%s\" is not empty." % d_path)
2806-
2807-
g.set_cfg('CACHE_DIR', cache_dir)
2808-
action('Repository cache location set to \"%s\"' % cache_dir)
2809-
28102798
cfg = g.cache_cfg()
28112799
if cmd == 'off' or cmd == 'on':
28122800
g.set_cfg('CACHE', 'enabled' if cmd == 'on' else 'disabled')
28132801
cfg = g.cache_cfg()
28142802
action("Repository cache is now %s." % str(cfg['cache']).upper())
28152803
action("Cache location \"%s\"" % cfg['cache_dir'])
2804+
elif cmd == 'dir':
2805+
if not argument:
2806+
error("Please specify directory or path to cache repositories. Alternatively specify \"default\" to cache repositories in the default user home location.")
2807+
if not os.path.exists(argument):
2808+
try:
2809+
os.makedirs(argument)
2810+
except (IOError, ImportError, OSError):
2811+
error("Unable to create cache directory \"%s\"" % argument, 128)
2812+
elif not os.path.isdir(argument):
2813+
error("The specified location \"%s\" is not a directory" % argument, 128)
2814+
elif len(os.listdir(argument)) > 1:
2815+
warning("Directory \"%s\" is not empty." % argument)
2816+
g.set_cfg('CACHE_DIR', argument)
2817+
action('Repository cache location set to \"%s\"' % argument)
28162818
elif cmd == 'ls':
28172819
def get_size_(path):
28182820
size = 0
28192821
for dirpath, dirs, files in os.walk(path):
28202822
for f in files:
28212823
size += os.path.getsize(os.path.join(dirpath, f))
28222824
return size
2823-
28242825
action("Listing cached repositories in \"%s\"" % cfg['cache_base'])
28252826
repos = []
28262827
total_size = 0
@@ -2831,21 +2832,22 @@ def get_size_(path):
28312832
url = repo.url
28322833
size = get_size_(repo.path)
28332834
total_size += size
2834-
log("* %s %s\n" % ('{:64}'.format(url), sizeof_fmt(size).rjust(8)))
2835+
log("* %s %s\n" % ('{:68}'.format(url), sizeof_fmt(size).rjust(8)))
28352836
for d in dirs:
28362837
dirs.remove(d)
2837-
log("---------------------------------------------------------------------------\n")
2838-
log("%s %s\n" % ('{:66}'.format('Total size:'), sizeof_fmt(total_size).rjust(8)))
2839-
2838+
log(("-" * 79) + "\n")
2839+
log("%s %s\n" % ('{:70}'.format('Total size:'), sizeof_fmt(total_size).rjust(8)))
28402840
elif cmd == 'purge':
28412841
action("Purging cached repositories in \"%s\"..." % cfg['cache_base'])
28422842
if os.path.isdir(cfg['cache_dir']):
28432843
rmtree_readonly(cfg['cache_dir'])
2844-
28452844
action("Purge complete!")
2846-
else:
2845+
elif cmd == "false":
28472846
action("Repository cache is %s." % str(cfg['cache']).upper())
28482847
action("Cache location \"%s\"" % cfg['cache_base'])
2848+
else:
2849+
print cmd
2850+
error("Invalid cache command. Please see \"mbed cache --help\" for valid commands.")
28492851

28502852

28512853
@subcommand('help',

0 commit comments

Comments
 (0)