File size: 10,848 Bytes
c6c9042
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
"""
preflight.py β€” Prerequisite checker for the PDF Accessibility Pipeline
=======================================================================

Called at the top of pipeline.py before any pipeline logic runs.
Checks system tools, Python packages, and required environment variables.
For missing config (API key, model path) it prompts interactively and
saves values to .env so the pipeline can continue without a restart.
"""

import importlib.util
import os
import platform
import shutil
import sys

# ---------------------------------------------------------------------------
# Pip package name β†’ Python import name (where they differ)
# ---------------------------------------------------------------------------
_PIP_TO_IMPORT: dict = {
    "opencv-python": "cv2",
    "Pillow": "PIL",
    "python-dotenv": "dotenv",
}

# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------

_ROOT = os.path.dirname(os.path.abspath(__file__))


def _detect_os() -> str:
    s = platform.system()
    if s == "Darwin":
        return "mac"
    if s == "Linux":
        return "linux"
    return "other"


def _load_dot_env(root: str) -> None:
    env_path = os.path.join(root, ".env")
    if os.path.isfile(env_path):
        try:
            from dotenv import load_dotenv
            load_dotenv(env_path, override=True)
        except ImportError:
            pass  # dotenv not installed yet; will be caught in package check


def _reload_env(root: str) -> None:
    env_path = os.path.join(root, ".env")
    from dotenv import load_dotenv
    load_dotenv(env_path, override=True)


def _check_system_tool(tool: str) -> bool:
    return shutil.which(tool) is not None


def _check_python_packages(req_path: str) -> list:
    """Return list of pip package names that are not importable."""
    if not os.path.isfile(req_path):
        return []
    missing = []
    with open(req_path, encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            # Strip version specifiers
            pkg = line.split("==")[0].split(">=")[0].split("<=")[0].split("!=")[0].strip()
            import_name = _PIP_TO_IMPORT.get(
                pkg,
                pkg.lower().replace("-", "_"),
            )
            if importlib.util.find_spec(import_name) is None:
                missing.append(pkg)
    return missing


def _write_env_vars(root: str, values: dict) -> None:
    """Upsert key=value pairs into .env (create if absent)."""
    env_path = os.path.join(root, ".env")
    existing_lines = []
    if os.path.isfile(env_path):
        with open(env_path, encoding="utf-8") as f:
            existing_lines = f.readlines()

    for key, val in values.items():
        key_found = False
        for i, line in enumerate(existing_lines):
            stripped = line.split("=")[0].strip() if "=" in line else ""
            if stripped == key:
                existing_lines[i] = f"{key}={val}\n"
                key_found = True
                break
        if not key_found:
            existing_lines.append(f"{key}={val}\n")

    try:
        with open(env_path, "w", encoding="utf-8") as f:
            f.writelines(existing_lines)
    except PermissionError:
        print(f"\n[preflight] ERROR: Cannot write to {env_path} β€” permission denied.")
        print("            Manually add the following lines to your .env file:")
        for key, val in values.items():
            print(f"            {key}={val}")
        sys.exit(1)


def _prompt_once(prompt: str) -> str:
    """Print prompt and read a line from stdin. Strip whitespace."""
    try:
        return input(prompt).strip()
    except (EOFError, KeyboardInterrupt):
        print()
        sys.exit(1)


# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------

def run_preflight_checks(
    *,
    skip: bool = False,
    need_python_part: bool = True,
    need_java_part: bool = True,
) -> None:
    """
    Run all prerequisite checks before the pipeline starts.

    Args:
        skip:             If True, skip all checks (--no-preflight flag).
        need_python_part: If True, check tesseract, poppler, Python packages,
                          OPENAI_API_KEY, and YOLO_MODEL_PATH.
        need_java_part:   If True, check java and mvn.
    """
    if skip:
        print("[preflight] Skipped (--no-preflight).")
        return

    print("[preflight] Checking prerequisites...\n")

    os_type = _detect_os()
    _load_dot_env(_ROOT)

    # ------------------------------------------------------------------
    # Phase 1 β€” System tools
    # ------------------------------------------------------------------

    # tool_name β†’ (mac install command, linux install command)
    tools_to_check: list[tuple] = []
    if need_python_part:
        tools_to_check += [
            ("tesseract",
             "brew install tesseract",
             "sudo apt install tesseract-ocr"),
            ("pdftoppm",
             "brew install poppler",
             "sudo apt install poppler-utils"),
        ]
    if need_java_part:
        tools_to_check += [
            ("java",
             "brew install openjdk@11  # or download from https://adoptium.net/",
             "sudo apt install default-jdk"),
            ("mvn",
             "brew install maven",
             "sudo apt install maven"),
        ]

    missing_tools: list[tuple] = []
    for tool, mac_cmd, linux_cmd in tools_to_check:
        ok = _check_system_tool(tool)
        status = "OK " if ok else "MISSING"
        label = tool if tool != "pdftoppm" else "pdftoppm (poppler)"
        print(f"  [{status}]  {label}")
        if not ok:
            missing_tools.append((tool, mac_cmd, linux_cmd))

    # ------------------------------------------------------------------
    # Phase 2 β€” Python packages
    # ------------------------------------------------------------------

    missing_pkgs: list = []
    if need_python_part:
        req_path = os.path.join(_ROOT, "requirements.txt")
        missing_pkgs = _check_python_packages(req_path)
        status = "OK " if not missing_pkgs else "MISSING"
        print(f"  [{status}]  Python packages")

    # ------------------------------------------------------------------
    # Early exit if any system tools or packages are missing
    # ------------------------------------------------------------------

    if missing_tools or missing_pkgs:
        print()

        if missing_tools:
            print("[preflight] Missing required system tools:\n")
            for tool, mac_cmd, linux_cmd in missing_tools:
                label = tool if tool != "pdftoppm" else "pdftoppm (poppler)"
                print(f"  {label}:")
                if os_type == "mac":
                    print(f"    macOS:  {mac_cmd}")
                elif os_type == "linux":
                    print(f"    Linux:  {linux_cmd}")
                else:
                    print(f"    macOS:  {mac_cmd}")
                    print(f"    Linux:  {linux_cmd}")
                print()

        if missing_pkgs:
            print("[preflight] Missing Python packages:\n")
            print(f"  {', '.join(missing_pkgs)}\n")
            print("  Run:  pip install -r requirements.txt\n")

        print("[preflight] Fix the issues above, then re-run pipeline.py.")
        sys.exit(1)

    # ------------------------------------------------------------------
    # Phase 3 β€” Environment variables
    # ------------------------------------------------------------------

    missing_env: list = []
    if need_python_part:
        api_key = os.environ.get("OPENAI_API_KEY", "").strip()
        yolo_path = os.environ.get("YOLO_MODEL_PATH", "").strip()

        if not api_key:
            print("  [MISSING] OPENAI_API_KEY  (will prompt)")
            missing_env.append("OPENAI_API_KEY")
        else:
            print("  [OK ]  OPENAI_API_KEY")

        if not yolo_path or not os.path.isfile(yolo_path):
            reason = "path not set" if not yolo_path else f"file not found: {yolo_path}"
            print(f"  [MISSING] YOLO_MODEL_PATH  ({reason}, will prompt)")
            missing_env.append("YOLO_MODEL_PATH")
        else:
            print("  [OK ]  YOLO_MODEL_PATH")

    if not missing_env:
        print(f"\n[preflight] All prerequisites satisfied. Starting pipeline...\n")
        return

    print()
    is_interactive = sys.stdin.isatty()

    # Non-interactive (CI/CD): print export hints and exit
    if not is_interactive:
        print("[preflight] Running in non-interactive mode (no TTY).")
        print("            Set the following environment variables and re-run:\n")
        if "OPENAI_API_KEY" in missing_env:
            print('            export OPENAI_API_KEY="sk-..."')
        if "YOLO_MODEL_PATH" in missing_env:
            print('            export YOLO_MODEL_PATH="/absolute/path/to/yolov11x_best.pt"')
        print()
        sys.exit(1)

    # Interactive: prompt for each missing var
    print("[preflight] Some required configuration is missing. Let's set it up.\n")
    collected: dict = {}

    if "OPENAI_API_KEY" in missing_env:
        print("  OPENAI_API_KEY")
        print("  Get your key from: https://platform.openai.com/api-keys\n")
        val = _prompt_once("  Enter your OpenAI API key (sk-...): ")
        if not val:
            val = _prompt_once("  (cannot be empty) Enter your OpenAI API key: ")
        if not val:
            print("\n[preflight] OPENAI_API_KEY is required. Exiting.")
            sys.exit(1)
        collected["OPENAI_API_KEY"] = val
        os.environ["OPENAI_API_KEY"] = val
        print()

    if "YOLO_MODEL_PATH" in missing_env:
        print("  YOLO_MODEL_PATH")
        print("  Download the model weights from:")
        print("    https://github.com/moured/YOLOv11-Document-Layout-Analysis/releases")
        print("    File: yolov11x_best.pt\n")
        path = _prompt_once("  Enter the absolute path to the downloaded .pt file: ")
        if not os.path.isfile(path):
            path = _prompt_once(f"  File not found at '{path}'. Enter path again: ")
        if not os.path.isfile(path):
            print(f"\n[preflight] YOLO model not found at '{path}'. Exiting.")
            sys.exit(1)
        collected["YOLO_MODEL_PATH"] = path
        os.environ["YOLO_MODEL_PATH"] = path
        print()

    # Save to .env and reload
    _write_env_vars(_ROOT, collected)
    _reload_env(_ROOT)
    print("[preflight] Saved to .env.\n")
    print("[preflight] All prerequisites satisfied. Starting pipeline...\n")