22
33import  logging 
44import  os 
5+ import  textwrap 
56
67from  pip .basecommand  import  Command 
78from  pip .status_codes  import  SUCCESS , ERROR 
9+ from  pip .utils .filesystem  import  tree_statistics , human_size 
810
911
1012logger  =  logging .getLogger (__name__ )
@@ -15,8 +17,9 @@ class CacheCommand(Command):
1517
1618
1719    Subcommands: 
18-         location: Print the location of the cache.""" 
19-     actions  =  ["location" ]
20+         location: Print the location of the cache. 
21+         info: Show statistics on the cache.""" 
22+     actions  =  ["location" , "info" ]
2023    name  =  "cache" 
2124    usage  =  """ 
2225      %%prog [options] %s"""  %  "|" .join (actions )
@@ -45,10 +48,32 @@ def run(self, options, args):
4548        method  =  getattr (self , "action_%s"  %  args [0 ])
4649        return  method (options , args [1 :])
4750
48-     def  action_location (self , options ,  args ):
49-         location  =  options . cache_dir 
51+     def  get_cache_location (self , cache_root ,  cache_type ):
52+         location  =  cache_root 
5053        suffix  =  {"wheel" : "wheels" , "http" : "http" }
51-         if  options .type  !=  "all" :
52-             location  =  os .path .join (location , suffix [options .type ])
53-         logger .info (location )
54+         if  cache_type  !=  "all" :
55+             location  =  os .path .join (location , suffix [cache_type ])
56+         return  location 
57+ 
58+     def  action_location (self , options , args ):
59+         logger .info (self .get_cache_location (options .cache_dir , options .type ))
60+         return  SUCCESS 
61+ 
62+     def  action_info (self , options , args ):
63+         caches  =  ["http" , "wheel" ] if  options .type  ==  "all"  else  [options .type ]
64+         result  =  []
65+         for  cache_type  in  caches :
66+             location  =  self .get_cache_location (options .cache_dir , cache_type )
67+             stats  =  tree_statistics (location )
68+             name  =  {"wheel" : "Wheel cache" , "http" : "HTTP cache" }
69+             result .append (textwrap .dedent (
70+                 """\  
71+ 
72+                    Location: %s 
73+                    Number of files: %s 
74+                    Size: %s"""  % 
75+                 (name [cache_type ], location , stats ["files" ],
76+                  human_size (stats ["size" ]))
77+             ))
78+         logger .info ("\n \n " .join (result ))
5479        return  SUCCESS 
0 commit comments