Auto-fix day logs are deleted a calendar month after their date
Only files named exactly '<YYYY-MM-DD> <Weekday> Auto-Fix.log' in that folder; checked when a new day's file starts and when the watchdog starts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ Stdlib only; runs on the macOS /usr/bin/python3 (3.9).
|
||||
status_panel.py --dry-run run, but never write to Companion
|
||||
"""
|
||||
import argparse
|
||||
import calendar
|
||||
import http.client
|
||||
import json
|
||||
import logging
|
||||
@@ -585,10 +586,41 @@ class AutoLog:
|
||||
e.g. "~/Desktop/Auto-Fix Logs/2026-10-11 Sunday Auto-Fix.log" (a .log opens in Console, which
|
||||
shows new lines live)."""
|
||||
|
||||
NAME = re.compile(r"^(\d{4})-(\d{2})-(\d{2}) [A-Za-z]+ Auto-Fix\.log$")
|
||||
|
||||
def __init__(self, directory):
|
||||
self.dir = os.path.expanduser(directory)
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def cleanup(self, now):
|
||||
"""Delete day files once they are a calendar month old (the 11 Oct file goes on 11 Nov).
|
||||
Only files named exactly like these logs are ever touched."""
|
||||
today = datetime.fromtimestamp(now).date()
|
||||
removed = []
|
||||
try:
|
||||
names = os.listdir(self.dir)
|
||||
except OSError:
|
||||
return removed
|
||||
for name in names:
|
||||
m = self.NAME.match(name)
|
||||
if not m:
|
||||
continue
|
||||
y, mo, d = (int(x) for x in m.groups())
|
||||
y2, mo2 = (y + 1, 1) if mo == 12 else (y, mo + 1)
|
||||
try:
|
||||
due = datetime(y2, mo2, min(d, calendar.monthrange(y2, mo2)[1])).date()
|
||||
except ValueError:
|
||||
continue
|
||||
if today >= due:
|
||||
try:
|
||||
os.remove(os.path.join(self.dir, name))
|
||||
removed.append(name)
|
||||
except OSError as e:
|
||||
log.warning("auto-fix log cleanup %s: %s", name, e)
|
||||
if removed:
|
||||
log.info("auto-fix logs older than a month deleted: %s", ", ".join(sorted(removed)))
|
||||
return removed
|
||||
|
||||
def path(self, now):
|
||||
return os.path.join(self.dir, datetime.fromtimestamp(now).strftime("%Y-%m-%d %A") + " Auto-Fix.log")
|
||||
|
||||
@@ -600,6 +632,8 @@ class AutoLog:
|
||||
os.makedirs(self.dir, exist_ok=True)
|
||||
path = self.path(now)
|
||||
new = not os.path.exists(path)
|
||||
if new:
|
||||
self.cleanup(now) # a new day: drop files a month old
|
||||
with open(path, "a", encoding="utf-8") as f:
|
||||
if new:
|
||||
f.write("Status panel auto-fixes - %s\n"
|
||||
@@ -697,6 +731,8 @@ class Engine:
|
||||
self.auto_tries = {} # fix identity -> [tries, last try time] for the fault now going on
|
||||
self.auto_seen = {} # fix identity -> last time it was in an auto plan
|
||||
self.autolog = AutoLog(self.auto.get("log_dir", "~/Desktop/Auto-Fix Logs")) if self.auto else None
|
||||
if self.autolog:
|
||||
self.autolog.cleanup(time.time())
|
||||
self.auto_watch = {} # "BUTTON FAULT" auto-fix went after -> when (to log when it clears)
|
||||
self.auto_paused = False
|
||||
self.auto_on = False # inside the window (for the "auto-fix on" line)
|
||||
|
||||
Reference in New Issue
Block a user