Skip to content

Commit 11645c0

Browse files
committed
Add 'mbed cache ls' implementation and related functions
1 parent 835debb commit 11645c0

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

mbed/mbed.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ def remove_readonly(func, path, _):
255255
func(path)
256256
shutil.rmtree(directory, onerror=remove_readonly)
257257

258+
def sizeof_fmt(num, suffix='B'):
259+
for unit in ['','K','M','G','T','P','E','Z']:
260+
if abs(num) < 1024.0:
261+
return "%3.1f%s%s" % (num, unit, suffix)
262+
num /= 1024.0
263+
return "%.1f%s%s" % (num, 'Yi', suffix)
258264

259265
# Directory navigation
260266
@contextlib.contextmanager
@@ -2801,21 +2807,42 @@ def cache_(on=False, off=False, ls=False, purge=False, cache_dir=None, global_cf
28012807
g.set_cfg('CACHE_DIR', cache_dir)
28022808
action('Repository cache location set to \"%s\"' % cache_dir)
28032809

2810+
cfg = g.cache_cfg()
28042811
if cmd == 'off' or cmd == 'on':
2805-
g.set_cfg('CACHE', 'enabled' if cmd == 'on' else 'disabled');
2812+
g.set_cfg('CACHE', 'enabled' if cmd == 'on' else 'disabled')
28062813
cfg = g.cache_cfg()
28072814
action('Repository cache is now %s.' % str(cfg['cache']).upper())
28082815
action('Cache location \"%s\"' % cfg['cache_dir'])
28092816
elif cmd == 'ls':
2810-
print 'list'
2817+
def get_size_(path):
2818+
size = 0
2819+
for dirpath, dirs, files in os.walk(path):
2820+
for f in files:
2821+
size += os.path.getsize(os.path.join(dirpath, f))
2822+
return size
2823+
2824+
action('Listing cached repositories in \"%s\"' % cfg['cache_base'])
2825+
repos = []
2826+
total_size = 0
2827+
for dirpath, dirs, files in os.walk(cfg['cache_dir']):
2828+
dirs[:] = [d for d in dirs if not d.startswith('.')]
2829+
if Repo.isrepo(dirpath):
2830+
repo = Repo().fromrepo(dirpath)
2831+
url = repo.url
2832+
size = get_size_(repo.path)
2833+
total_size += size
2834+
log('* %s %s\n' % ('{:60}'.format(url), sizeof_fmt(size).rjust(8)))
2835+
for d in dirs:
2836+
dirs.remove(d)
2837+
log('%s %s\n' % ('{:62}'.format('Total:'), sizeof_fmt(total_size).rjust(8)))
2838+
28112839
elif cmd == 'purge':
2812-
cfg = g.cache_cfg()
28132840
action('Purging cached repositories in \"%s\"' % cfg['cache_base'])
2814-
if os.path.isdir(path):
2841+
if os.path.isdir(cfg['cache_dir']):
28152842
rmtree_readonly(cfg['cache_dir'])
2816-
action('Complete')
2843+
2844+
action('Purge complete')
28172845
else:
2818-
cfg = g.cache_cfg()
28192846
action('Repository cache is %s.' % str(cfg['cache']).upper())
28202847
action('Cache location \"%s\"' % cfg['cache_base'])
28212848

0 commit comments

Comments
 (0)