Files
hyperstitionstudio-core/src/hyperstitionstud_io/ci/doctor.py
T

111 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""
hyperstitionstud_io.ci.doctor — Location-Independent Environment, Font, Schema & SHA-256 Diagnostic (`hs-doctor`).
"""
from __future__ import annotations
import importlib.metadata
import json
from pathlib import Path
from typing import Any
from hyperstitionstud_io.config import load_workspace_config
from hyperstitionstud_io.glossary import sha256_file
from hyperstitionstud_io.publishing.fonts import resolve_font_path
EXPECTED_SHA256_BY_SUFFIX = {
"taxonomy/glossary-registry.json": "7564ef40fb3e0e18d1d8547522db431b6a369c40139713b573df6e58b972d43d",
"encyclopedia/encyclopedia-manifest.json": "0d7ce833bb0aa0eea069bf764b2e3b0b36ac2631821c42f3d6bcb897335f8a7d",
}
REQUIRED_FONT_NAMES = [
"DejaVuSans.ttf",
"DejaVuSans-Bold.ttf",
"DejaVuSansMono.ttf",
"DejaVuSansMono-Bold.ttf",
]
def run_diagnostics(workspace_root: str | Path | None = None) -> dict[str, Any]:
cfg = load_workspace_config(workspace_root=workspace_root)
pkgs = {}
for p in ("Pillow", "reportlab", "pypdf", "pdfplumber"):
try:
pkgs[p] = importlib.metadata.version(p)
except importlib.metadata.PackageNotFoundError:
pkgs[p] = None
fonts_resolved = {
fname: str(resolve_font_path(fname)) if resolve_font_path(fname) else "builtin-fallback"
for fname in REQUIRED_FONT_NAMES
}
required_dirs = ("core_dir", "archive_dir", "zoology_dir", "knowledge_graph_dir")
dirs_ok = {k: cfg.paths[k].is_dir() for k in required_dirs if k in cfg.paths}
isolation_checks = {}
for p in cfg.read_only_files:
p_str = p.as_posix()
actual = sha256_file(p) if p.is_file() else None
expected = next((sha for suf, sha in EXPECTED_SHA256_BY_SUFFIX.items() if p_str.endswith(suf)), None)
isolation_checks[p_str] = {
"exists": p.is_file(),
"sha256": actual,
"matches_baseline": (actual == expected) if expected else (actual is not None),
}
fb_path = Path(__file__).resolve().parent.parent / "phonetics" / "cmudict_fallback.json"
fb_count = len(json.loads(fb_path.read_text(encoding="utf-8"))) if fb_path.is_file() else 0
lr_path = cfg.core_dir / "deploy" / "logrotate.d" / "hyperstition-sqlite-meta"
lr_txt = lr_path.read_text(encoding="utf-8") if lr_path.is_file() else ""
lr_ok = (
lr_path.is_file()
and "/var/lib/hyperstitionstud.io/api-telemetry.db" in lr_txt
and "/tmp/" not in lr_txt
and "size 100M" in lr_txt
and "nocompress" in lr_txt
and "rotate 0" not in lr_txt
and "--truncate-quiescent" in lr_txt
and "--compress-archive" in lr_txt
and "prerotate" in lr_txt
)
all_ok = (
pkgs.get("Pillow") is not None
and all(dirs_ok.values())
and all(v["matches_baseline"] for v in isolation_checks.values())
and fb_count >= 190
and lr_ok
)
return {
"namespace_urn": cfg.namespace_urn,
"schema_version": cfg.schema_version,
"workspace_root": str(cfg.workspace_root),
"workspace_config_file": str(cfg.source_file) if cfg.source_file else None,
"all_checks_passed": all_ok,
"python_packages": pkgs,
"truetype_fonts": fonts_resolved,
"workspace_directories": dirs_ok,
"offline_cmudict_fallback_entries": fb_count,
"sqlite_logrotate_config": {
"path": str(lr_path),
"exists": lr_path.is_file(),
"default_size_limit": "100M",
"quiescent_truncation_configured": lr_ok,
},
"citation_path_mounts": cfg.citation_path_mounts,
"isolation_locks": isolation_checks,
}
def main() -> int:
res = run_diagnostics()
print(json.dumps(res, indent=2))
return 0 if res["all_checks_passed"] else 1
if __name__ == "__main__":
raise SystemExit(main())