-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_caches.py
More file actions
114 lines (90 loc) · 3.15 KB
/
clear_caches.py
File metadata and controls
114 lines (90 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Clear all cached and generated data files.
Use this when:
- Changing data processing/export format
- Switching data sources
- Debugging data inconsistencies
- Want a clean slate
This ensures no stale data with old transformations.
"""
import sys
import shutil
from pathlib import Path
def clear_caches(confirm: bool = False):
"""Clear all cached and generated data."""
cache_dirs = [
Path("data/.cache"),
Path("generated"),
]
print("\n" + "=" * 70)
print(" CACHE CLEARING UTILITY")
print("=" * 70)
print("\nPRESERVED (will NOT be deleted):")
print(" - data/*.tif (original GeoTIFF files)")
print(" - data/regions/*.tif (regional GeoTIFF files)")
print(" - data/usa_elevation/*.tif (USA elevation data)")
print("\nWill DELETE (cached/generated):")
total_size = 0
file_count = 0
for cache_dir in cache_dirs:
if cache_dir.exists():
# Calculate size
for item in cache_dir.rglob("*"):
if item.is_file():
total_size += item.stat().st_size
file_count += 1
print(f"\n {cache_dir}/")
if cache_dir.exists():
contents = list(cache_dir.rglob("*"))
print(f" - {len([f for f in contents if f.is_file()])} files")
print(f"\nTotal: {file_count} files ({total_size / (1024**2):.1f} MB)")
if not confirm:
print("\n" + "=" * 70)
response = input("\nProceed with deletion? [y/N]: ").strip().lower()
if response not in ('y', 'yes'):
print("\nCancelled.")
return False
print("\n" + "=" * 70)
print("Clearing caches...")
deleted_dirs = []
for cache_dir in cache_dirs:
if cache_dir.exists():
try:
shutil.rmtree(cache_dir)
print(f" [OK] Deleted: {cache_dir}/")
deleted_dirs.append(cache_dir)
except Exception as e:
print(f" [ERROR] Failed to delete {cache_dir}: {e}")
# Recreate directories
print("\nRecreating directories...")
for cache_dir in deleted_dirs:
cache_dir.mkdir(parents=True, exist_ok=True)
print(f" [OK] Created: {cache_dir}/")
print("\n" + "=" * 70)
print("CACHE CLEARING COMPLETE")
print("=" * 70)
print("\nNext steps:")
print(" 1. Re-export data: python export_for_web_viewer.py")
print(" 2. Or download regions: python download_regions.py")
print("\n")
return True
def main():
import argparse
parser = argparse.ArgumentParser(
description='Clear all cached and generated data files',
epilog='Use this when changing data processing format to ensure consistency'
)
parser.add_argument(
'--yes', '-y',
action='store_true',
help='Skip confirmation prompt'
)
args = parser.parse_args()
try:
success = clear_caches(confirm=args.yes)
return 0 if success else 1
except KeyboardInterrupt:
print("\n\nCancelled by user.")
return 1
if __name__ == "__main__":
sys.exit(main())