-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanup_cache.py
More file actions
executable file
·110 lines (86 loc) · 3.28 KB
/
Copy pathcleanup_cache.py
File metadata and controls
executable file
·110 lines (86 loc) · 3.28 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
#!/usr/bin/env python3
"""
Cache and Charts Cleanup Script for Finance Bro
This script cleans up temporary files from:
- ./cache/*.db (database cache files)
- ./exports/charts/*.png (generated chart images)
- ./exports/tearsheets/*.html (generated tearsheet reports)
- ./exports/reports/*.xlsx (generated Excel reports)
- ./pandasai.log (PandasAI log file)
Usage:
python cleanup_cache.py
"""
import os
import glob
import sys
from pathlib import Path
def cleanup_directory(directory, file_pattern, description):
"""
Clean up files matching a pattern in a directory.
Args:
directory (str): Directory path to clean
file_pattern (str): File pattern to match (e.g., "*.png")
description (str): Description for logging
Returns:
int: Number of files deleted
"""
if not os.path.exists(directory):
print(f"📁 Directory {directory} does not exist, skipping...")
return 0
# Find files matching the pattern
pattern_path = os.path.join(directory, file_pattern)
files_to_delete = glob.glob(pattern_path)
if not files_to_delete:
print(f"✅ No {description} found in {directory}")
return 0
deleted_count = 0
for file_path in files_to_delete:
try:
os.remove(file_path)
print(f"🗑️ Deleted: {file_path}")
deleted_count += 1
except OSError as e:
print(f"❌ Error deleting {file_path}: {e}")
print(f"✅ Cleaned up {deleted_count} {description} from {directory}")
return deleted_count
def main():
"""Main cleanup function."""
print("🧹 Finance Bro Cache & Charts Cleanup")
print("=" * 40)
# Get script directory as base path
script_dir = Path(__file__).parent
total_deleted = 0
# Clean cache directory - *.db files
cache_dir = script_dir / "cache"
deleted = cleanup_directory(str(cache_dir), "*.db", "database cache files")
total_deleted += deleted
# Clean exports/charts directory - *.png files
charts_dir = script_dir / "exports" / "charts"
deleted = cleanup_directory(str(charts_dir), "*.png", "chart image files")
total_deleted += deleted
# Clean exports/tearsheets directory - *.html files
tearsheets_dir = script_dir / "exports" / "tearsheets"
deleted = cleanup_directory(str(tearsheets_dir), "*.html", "tearsheet HTML files")
total_deleted += deleted
# Clean exports/reports directory - *.xlsx files
reports_dir = script_dir / "exports" / "reports"
deleted = cleanup_directory(str(reports_dir), "*.xlsx", "Excel report files")
total_deleted += deleted
# Clean pandasai.log file in project root
deleted = cleanup_directory(str(script_dir), "pandasai.log", "PandasAI log file")
total_deleted += deleted
print("=" * 40)
print(f"🎉 Cleanup complete! Total files deleted: {total_deleted}")
if total_deleted == 0:
print("💡 No files needed cleanup - directories are already clean!")
else:
print("✨ Cache, charts, tearsheets, reports, and log files have been cleaned!")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n⚠️ Cleanup interrupted by user")
sys.exit(1)
except Exception as e:
print(f"❌ Unexpected error: {e}")
sys.exit(1)