|
| 1 | +import os |
| 2 | +import shutil |
| 3 | + |
| 4 | + |
| 5 | +def get_cache_dirs(): |
| 6 | + home = os.path.expanduser("~") |
| 7 | + cache_dirs = [ |
| 8 | + os.path.join(home, ".cache", "huggingface"), |
| 9 | + os.path.join(home, ".cache", "torch"), # PyTorch cache |
| 10 | + os.path.join(home, ".cache", "pip"), # pip cache |
| 11 | + os.path.join(home, ".cache", "diffusers"), # Diffusers cache |
| 12 | + os.path.join(home, ".huggingface"), # Alternative Hugging Face cache |
| 13 | + ] |
| 14 | + return list(set(cache_dirs)) # Remove duplicates |
| 15 | + |
| 16 | + |
| 17 | +def print_cache_sizes(cache_dirs): |
| 18 | + for directory in cache_dirs: |
| 19 | + if os.path.exists(directory): |
| 20 | + size = sum( |
| 21 | + os.path.getsize(os.path.join(dirpath, filename)) for dirpath, dirnames, filenames in os.walk(directory) for filename in filenames |
| 22 | + ) |
| 23 | + print(f"Cache directory: {directory}") |
| 24 | + print(f"Size: {size / (1024 * 1024):.2f} MB") |
| 25 | + else: |
| 26 | + print(f"Cache directory does not exist: {directory}") |
| 27 | + print() |
| 28 | + |
| 29 | + |
| 30 | +def clear_cache(cache_dirs): |
| 31 | + for directory in cache_dirs: |
| 32 | + if os.path.exists(directory): |
| 33 | + try: |
| 34 | + shutil.rmtree(directory) |
| 35 | + print(f"Cleared cache directory: {directory}") |
| 36 | + except Exception as e: |
| 37 | + print(f"Error clearing {directory}: {e}") |
| 38 | + else: |
| 39 | + print(f"Cache directory does not exist: {directory}") |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + cache_dirs = get_cache_dirs() |
| 44 | + print("Current cache directories and sizes:") |
| 45 | + print_cache_sizes(cache_dirs) |
| 46 | + |
| 47 | + user_input = input("Do you want to clear these cache directories? (yes/no): ").lower() |
| 48 | + if user_input == "yes": |
| 49 | + clear_cache(cache_dirs) |
| 50 | + else: |
| 51 | + print("Cache clearing aborted.") |
0 commit comments