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