Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
7.3 KB
Raw Blame
"""Exercise a release binary as a running local application.

The release workflow uses this after PyInstaller finishes. It verifies the
server, bundled frontend, bundled ffmpeg and the binary's gallery-dl mode from
the artifact itself.
"""

from __future__ import annotations

import argparse
import json
import os
import re
import socket
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.request
from pathlib import Path
from typing import Any


def _free_port() -> int:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(("127.0.0.1", 0))
        return int(sock.getsockname()[1])


def _request(url: str) -> tuple[int, str, str]:
    with urllib.request.urlopen(url, timeout=2) as response:
        return (
            response.status,
            response.headers.get_content_type(),
            response.read().decode("utf-8", "replace"),
        )


def check_application(base_url: str) -> None:
    status, content_type, body = _request(f"{base_url}/api/health")
    health: dict[str, Any] = json.loads(body)
    if status != 200 or health.get("status") != "ok":
        raise RuntimeError(f"health check failed: {status} {health!r}")

    status, content_type, body = _request(f"{base_url}/")
    if status != 200 or content_type != "text/html" or '<div id="app"></div>' not in body:
        raise RuntimeError("the binary did not serve the built frontend")

    status, content_type, body = _request(f"{base_url}/api/settings/ffmpeg")
    ffmpeg: dict[str, Any] = json.loads(body)
    if status != 200 or not ffmpeg.get("available") or ffmpeg.get("source") != "bundled":
        raise RuntimeError(f"bundled ffmpeg was not detected: {ffmpeg!r}")
    status, content_type, body = _request(f"{base_url}/api/settings/ffmpeg/notice")
    if status != 200 or "GNU" not in body or "SHA-256" not in body:
        raise RuntimeError("the bundled ffmpeg shipped without its licence notice")

    # What the frozen build drops without a word when the spec forgets it: the zone
    # database (local times silently read as UTC on Windows), the PDF fonts, the
    # capture extension the Settings install button and the zip route hand out, and
    # the scrapers' metadata (a stale workspace copy would shadow the shipped one), and
    # the decoders Horizon reads buildings with, checked offline.
    status, _content_type, body = _request(f"{base_url}/api/settings/bundled")
    smoke: dict[str, Any] = json.loads(body)
    missing = [name for name, ok in smoke.items() if not ok]
    if status != 200 or missing:
        raise RuntimeError(f"the binary is missing bundled parts: {missing or smoke!r}")


def wait_until_ready(base_url: str, process: subprocess.Popen[bytes], timeout: float = 60) -> None:
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        code = process.poll()
        if code is not None:
            raise RuntimeError(f"binary exited before it became ready (exit {code})")
        try:
            check_application(base_url)
            return
        except (OSError, urllib.error.URLError):
            time.sleep(0.25)
    raise RuntimeError(f"binary did not become ready within {timeout:g} seconds")


def stop(process: subprocess.Popen[bytes]) -> None:
    """Stop the binary, children included.

    PyInstaller builds this artifact in onefile mode, so the `.exe` is a
    bootloader that unpacks itself and runs the application as a *child*. On
    POSIX the bootloader forwards the signal down; on Windows there is nothing
    to forward it — `terminate()` is `TerminateProcess` on the bootloader alone,
    and the application keeps running with the workspace lock file still open.
    Windows cannot delete an open file, so the temporary workspace below refuses
    to clean up and the release fails after the binary already passed.
    """
    if process.poll() is not None:
        return
    if sys.platform == "win32":
        # taskkill walks the tree; /F because the orphan has no console to ask.
        subprocess.run(
            ["taskkill", "/F", "/T", "/PID", str(process.pid)],
            capture_output=True,
            check=False,
        )
    else:
        process.terminate()
    try:
        process.wait(timeout=10)
    except subprocess.TimeoutExpired:
        process.kill()
        process.wait(timeout=10)


#: What the binary is started with to act as gallery-dl (engine/gallerydl.py FROZEN_FLAG).
GALLERY_DL_FLAG = "--run-gallery-dl"


def check_gallery_dl(binary: Path, env: dict[str, str]) -> None:
    """The binary is gallery-dl too, started again with its flag.

    gallery-dl runs as a program of its own, and in the binary that program is the
    binary itself. Its version proves the flag reaches gallery-dl's entry point, and
    an address no extractor knows (exit 64, offline) proves its extractors were bundled.
    """
    asked = [str(binary), GALLERY_DL_FLAG, "--config-ignore"]
    version = subprocess.run(
        [*asked, "--version"], env=env, capture_output=True, text=True, timeout=120, check=False
    )
    if version.returncode != 0 or not re.fullmatch(r"\d+\.\d+\.\d+\S*", version.stdout.strip()):
        raise RuntimeError(f"the binary did not run as gallery-dl: {version.stderr.strip()!r}")
    unknown = subprocess.run(
        [*asked, "-j", "https://example.invalid/"],
        env=env, capture_output=True, text=True, timeout=120, check=False,
    )
    if unknown.returncode != 64:
        raise RuntimeError(f"gallery-dl's extractors are missing: {unknown.stderr.strip()!r}")


#: What the binary is started with to run a spawned pool (packaging/entry.py PROCESSES_FLAG).
PROCESSES_FLAG = "--smoke-processes"


def check_processes(binary: Path, env: dict[str, str]) -> None:
    """The binary's spawned children do the work they are handed.

    A search scores its points in processes that start the binary again. Without
    `freeze_support()` first, each child would start a second app instead, so the pool
    never answers: the two factorials it sums (5! + 6! = 840) prove it does.
    """
    try:
        done = subprocess.run(
            [str(binary), PROCESSES_FLAG], env=env, capture_output=True, text=True, timeout=180, check=False
        )
    except subprocess.TimeoutExpired as exc:
        raise RuntimeError("the binary's processes never answered") from exc
    if done.returncode != 0 or done.stdout.strip() != "840":
        raise RuntimeError(f"the binary's processes did not answer: {done.stderr.strip()[-400:]!r}")


def smoke(binary: Path) -> None:
    binary = binary.resolve()
    if not binary.is_file():
        raise FileNotFoundError(binary)

    port = _free_port()
    base_url = f"http://127.0.0.1:{port}"
    with tempfile.TemporaryDirectory(prefix="azimut-binary-smoke-") as workspace:
        env = os.environ.copy()
        env["AZIMUT_HOME"] = workspace
        check_gallery_dl(binary, env)
        check_processes(binary, env)
        process = subprocess.Popen(
            [str(binary), "--no-browser", "--port", str(port)],
            env=env,
        )
        try:
            wait_until_ready(base_url, process)
        finally:
            stop(process)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("binary", type=Path)
    args = parser.parse_args()
    smoke(args.binary)


if __name__ == "__main__":
    main()