"""Runtime preflight checks for the audit pipeline. These checks are intentionally lightweight and side-effect free. They give the API and operators a quick answer for "can this process handle an uploaded deck?" before a user waits through the pipeline. """ from __future__ import annotations import os import shutil import tempfile from pathlib import Path from schemas.api import PreflightChecks, PreflightResponse def audit_preflight(jobs_root: Path) -> PreflightResponse: """Return dependency/config readiness for the audit backend.""" jobs_root_writable = _writable_dir(jobs_root) openai_api_key = bool(os.environ.get("OPENAI_API_KEY")) soffice = shutil.which("soffice") is not None pdftoppm = shutil.which("pdftoppm") is not None checks = PreflightChecks( jobs_root_writable=jobs_root_writable, openai_api_key=openai_api_key, soffice=soffice, pdftoppm=pdftoppm, ) ready = jobs_root_writable and openai_api_key warnings: list[str] = [] if not jobs_root_writable: warnings.append( "Audit jobs directory is not writable; uploads will fail." ) if not openai_api_key: warnings.append( "OPENAI_API_KEY is not set; audits will fail at the review stage." ) if not soffice: warnings.append( "soffice (LibreOffice) not found; visual slide rendering will be" " unavailable. Text review can still run." ) if not pdftoppm: warnings.append( "pdftoppm not found; PDF preview generation may be unavailable." " Slide rendering still works via soffice." ) return PreflightResponse( ready=ready, checks=checks, warnings=warnings, jobs_root=str(jobs_root.resolve()), ) def _writable_dir(path: Path) -> bool: try: path.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile(dir=str(path), delete=True): pass return True except OSError: return False