#!/usr/bin/env python3 """Inkrementeller Maildir-Index-Update fuer alle User (stefan, kati). Aufruf: python3 index_maildirs.py # inkrementell, alle User python3 index_maildirs.py --full # erzwingt Reindex (fuer Tests) python3 index_maildirs.py --user kati # nur ein User Wird vom systemd-Timer mail-index.timer nach jedem mail-backup-Sync aufgerufen. """ import os import sys import time BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, BASE_DIR) sys.path.insert(0, os.path.join(BASE_DIR, "mail")) from common import load_config as _lc # noqa: E402 from mail.mailindex import update_user, connect, _SCHEMA # noqa: E402 LOG_FILE = "/var/log/mail-index.log" def log(msg): line = f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}" print(line, flush=True) try: with open(LOG_FILE, "a") as f: f.write(line + "\n") except OSError: pass def main(): args = [a for a in sys.argv[1:]] full = "--full" in args only_user = None if "--user" in args: only_user = args[args.index("--user") + 1] cfg = _lc() roots = cfg.get("mail_roots", {}) if only_user: roots = {only_user: roots.get(only_user)} for user, root in sorted(roots.items()): if not root or not os.path.isdir(root): log(f"SKIP {user}: root fehlt ({root})") continue t0 = time.monotonic() if full: # Reindex: DB zuruecksetzen, dann komplett neu con = connect(user) con.executescript("DROP TABLE IF EXISTS messages_fts; DROP TABLE IF EXISTS messages;") con.executescript(_SCHEMA) con.commit() con.close() log(f"REINDEX {user}: Tabellen neu erstellt") added, updated, removed = update_user(user, root, log=log) dur = time.monotonic() - t0 # Erst-Build abgeschlossen → Suche darf den Index nutzen from mail.mailindex import _ready_path with open(_ready_path(user), "w") as f: f.write(time.strftime("%Y-%m-%d %H:%M:%S")) log(f"OK {user}: added={added} updated={updated} removed={removed} ({dur:.1f}s)") if __name__ == "__main__": main()