@@ -255,6 +255,12 @@ def remove_readonly(func, path, _):
255
255
func (path )
256
256
shutil .rmtree (directory , onerror = remove_readonly )
257
257
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 )
258
264
259
265
# Directory navigation
260
266
@contextlib .contextmanager
@@ -2801,21 +2807,42 @@ def cache_(on=False, off=False, ls=False, purge=False, cache_dir=None, global_cf
2801
2807
g .set_cfg ('CACHE_DIR' , cache_dir )
2802
2808
action ('Repository cache location set to \" %s\" ' % cache_dir )
2803
2809
2810
+ cfg = g .cache_cfg ()
2804
2811
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' )
2806
2813
cfg = g .cache_cfg ()
2807
2814
action ('Repository cache is now %s.' % str (cfg ['cache' ]).upper ())
2808
2815
action ('Cache location \" %s\" ' % cfg ['cache_dir' ])
2809
2816
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
+
2811
2839
elif cmd == 'purge' :
2812
- cfg = g .cache_cfg ()
2813
2840
action ('Purging cached repositories in \" %s\" ' % cfg ['cache_base' ])
2814
- if os .path .isdir (path ):
2841
+ if os .path .isdir (cfg [ 'cache_dir' ] ):
2815
2842
rmtree_readonly (cfg ['cache_dir' ])
2816
- action ('Complete' )
2843
+
2844
+ action ('Purge complete' )
2817
2845
else :
2818
- cfg = g .cache_cfg ()
2819
2846
action ('Repository cache is %s.' % str (cfg ['cache' ]).upper ())
2820
2847
action ('Cache location \" %s\" ' % cfg ['cache_base' ])
2821
2848
0 commit comments