@@ -816,64 +816,39 @@ def get_size(start_path: str = ".") -> int:
816816 return total_size
817817
818818
819- def clean_cache (cache_path : str , cache_limit : int , verbose : bool ):
819+ def clean_cache (cache_path : str , cache_limit : int , verbose : bool ) -> None :
820+ if not cache_limit :
821+ return
822+
820823 files = glob .glob (os .path .join (cache_path , "*" , "*" ))
821824 if not files :
822825 return
823826
824- # Remove all text files, store binary files in list of (filename, size, atime).
825- purge = []
826- texts = []
827+ # Store files in list of (filename, size, atime).
827828 stats = []
828829 for file in files :
829830 try :
830- # Save file stats to rewrite after modifying.
831- tmp_stat = os .stat (file )
832- # Failing a utf-8 decode is the easiest way to determine if a file is binary.
833- try :
834- with open (file , encoding = "utf-8" ) as out :
835- out .read (1024 )
836- except UnicodeDecodeError :
837- stats .append ((file , * tmp_stat [6 :8 ]))
838- # Restore file stats after reading.
839- os .utime (file , (tmp_stat [7 ], tmp_stat [8 ]))
840- else :
841- texts .append (file )
831+ stats .append ((file , * os .stat (file )[6 :8 ]))
842832 except OSError :
843833 print_error (f'Failed to access cache file "{ file } "; skipping.' )
844834
845- if texts :
846- count = len (texts )
847- for file in texts :
848- try :
849- os .remove (file )
850- except OSError :
851- print_error (f'Failed to remove cache file "{ file } "; skipping.' )
852- count -= 1
853- if verbose :
854- print ("Purging %d text %s from cache..." % (count , "files" if count > 1 else "file" ))
855-
856- if cache_limit :
857- # Sort by most recent access (most sensible to keep) first. Search for the first entry where
858- # the cache limit is reached.
859- stats .sort (key = lambda x : x [2 ], reverse = True )
860- sum = 0
861- for index , stat in enumerate (stats ):
862- sum += stat [1 ]
863- if sum > cache_limit :
864- purge .extend ([x [0 ] for x in stats [index :]])
865- break
866-
867- if purge :
868- count = len (purge )
869- for file in purge :
870- try :
871- os .remove (file )
872- except OSError :
873- print_error (f'Failed to remove cache file "{ file } "; skipping.' )
874- count -= 1
875- if verbose :
876- print ("Purging %d %s from cache..." % (count , "files" if count > 1 else "file" ))
835+ # Sort by most recent access (most sensible to keep) first. Search for the first entry where
836+ # the cache limit is reached.
837+ stats .sort (key = lambda x : x [2 ], reverse = True )
838+ sum = 0
839+ for index , stat in enumerate (stats ):
840+ sum += stat [1 ]
841+ if sum > cache_limit :
842+ purge = [x [0 ] for x in stats [index :]]
843+ count = len (purge )
844+ for file in purge :
845+ try :
846+ os .remove (file )
847+ except OSError :
848+ print_error (f'Failed to remove cache file "{ file } "; skipping.' )
849+ count -= 1
850+ if verbose :
851+ print_info (f"Purged { count } file{ 's' if count else '' } from cache." )
877852
878853
879854def prepare_cache (env ) -> None :
0 commit comments