Coverage for src \ truenex_memory \ diagnostics \ __init__.py: 92%
25 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-19 10:21 +0200
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-19 10:21 +0200
1"""Local diagnostics for Truenex Memory."""
3from __future__ import annotations
5import platform
6import sys
7from datetime import datetime, timezone
8from pathlib import Path
9from tempfile import NamedTemporaryFile
10from typing import Any
12from truenex_memory import __version__
15def _check_writable(path: Path) -> tuple[bool, str | None]:
16 try:
17 path.mkdir(parents=True, exist_ok=True)
18 with NamedTemporaryFile(prefix=".truenex-memory-", dir=path, delete=True):
19 pass
20 except OSError as exc:
21 return False, str(exc)
22 return True, None
25def run_diagnostics(base_path: str | Path | None = None) -> dict[str, Any]:
26 """Run local diagnostics without contacting external services."""
28 target = Path.cwd() if base_path is None else Path(base_path)
29 python_supported = sys.version_info >= (3, 12)
30 target_exists = target.exists()
31 target_is_dir = target.is_dir() if target_exists else False
32 writable, writable_error = _check_writable(target) if target_is_dir else (False, "path is not a directory")
34 checks: list[dict[str, Any]] = [
35 {
36 "name": "python_version",
37 "ok": python_supported,
38 "detail": sys.version.split()[0],
39 },
40 {
41 "name": "package_import",
42 "ok": True,
43 "detail": f"truenex-memory {__version__}",
44 },
45 {
46 "name": "base_path_exists",
47 "ok": target_exists,
48 "detail": str(target),
49 },
50 {
51 "name": "base_path_writable",
52 "ok": writable,
53 "detail": "writable" if writable else writable_error,
54 },
55 ]
57 return {
58 "status": "ok" if all(check["ok"] for check in checks) else "warn",
59 "generated_at": datetime.now(timezone.utc).isoformat(),
60 "package_version": __version__,
61 "platform": {
62 "python": sys.version.split()[0],
63 "implementation": platform.python_implementation(),
64 "system": platform.system(),
65 "release": platform.release(),
66 },
67 "base_path": str(target),
68 "checks": checks,
69 }
72__all__ = ["run_diagnostics"]