with autoclean

This commit is contained in:
Aron Petau 2025-12-31 16:19:14 +01:00
parent 540a3cc884
commit 55a6ddb819
4 changed files with 54 additions and 1 deletions

34
autoclean_debug.py Normal file
View file

@ -0,0 +1,34 @@
import os
import time
from pathlib import Path
import logging
def autoclean_debug_material(data_dir="data", max_age_hours=48):
"""
Delete debug files (PNGs, HTMLs, etc.) in data/ and subfolders older than max_age_hours.
Does NOT delete listings, applications, state, or CSV/JSON/LOG files.
"""
logger = logging.getLogger()
now = time.time()
max_age = max_age_hours * 3600
# File extensions considered debug material
debug_exts = {".png", ".html"}
# Always skip these files (listing, state, applications, logs, csv, json, ttf, etc.)
safe_exts = {".json", ".csv", ".log", ".ttf"}
safe_names = {"listings.json", "applications.json", "state.json", "wgcompany_listings.json", "wgcompany_times.csv", "listing_times.csv"}
data_path = Path(data_dir)
deleted = []
for root, dirs, files in os.walk(data_path):
for fname in files:
fpath = Path(root) / fname
ext = fpath.suffix.lower()
if ext in debug_exts and ext not in safe_exts and fname not in safe_names:
try:
mtime = fpath.stat().st_mtime
if now - mtime > max_age:
fpath.unlink()
deleted.append(str(fpath))
except Exception as e:
logger.warning(f"Could not delete {fpath}: {e}")
logger.info(f"Autocleaned {len(deleted)} debug files older than {max_age_hours}h.")
return deleted