File size: 1,648 Bytes
8c7f11a
0ef834c
8c7f11a
 
 
 
0ef834c
 
 
 
 
 
 
8c7f11a
0ef834c
 
 
8c7f11a
 
 
 
 
 
 
0ef834c
 
 
 
 
 
 
8c7f11a
 
 
 
 
 
 
0ef834c
 
 
 
 
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
"""Print a materialized search generation's identity.

The default command fully verifies the generation for the Docker build gate.
With --identity-only the launcher reads only the declared identity to check
startup overrides; the ASGI entry point verifies the entire generation before
using it. The declared identity alone is not a verification receipt.
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

from backend.search.corpus.infrastructure.selection import SHA, inspect_generation


def main() -> int:
    args = sys.argv[1:]
    identity_only = args[:1] == ["--identity-only"]
    if identity_only:
        args = args[1:]
    if len(args) != 1:
        raise SystemExit("usage: python -m scripts.search_data_config [--identity-only] VERIFIED_DIRECTORY")
    directory = Path(args[0])
    manifest_path = directory / "generation-manifest.json"
    if manifest_path.is_symlink() or not manifest_path.is_file() or manifest_path.stat().st_size > 1024 * 1024:
        raise ValueError("missing or oversized generation manifest")
    manifest: object = json.loads(manifest_path.read_bytes())
    generation = manifest.get("generation") if isinstance(manifest, dict) else None
    if not isinstance(generation, str):
        raise ValueError("missing generation identity")
    if identity_only:
        if not SHA.fullmatch(generation):
            raise ValueError("invalid search generation identity")
        print(generation)
    else:
        selected = inspect_generation(directory, generation)
        print(selected.generation)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())