File size: 2,070 Bytes
fdfe81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""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