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

1"""Local diagnostics for Truenex Memory.""" 

2 

3from __future__ import annotations 

4 

5import platform 

6import sys 

7from datetime import datetime, timezone 

8from pathlib import Path 

9from tempfile import NamedTemporaryFile 

10from typing import Any 

11 

12from truenex_memory import __version__ 

13 

14 

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 

23 

24 

25def run_diagnostics(base_path: str | Path | None = None) -> dict[str, Any]: 

26 """Run local diagnostics without contacting external services.""" 

27 

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") 

33 

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 ] 

56 

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 } 

70 

71 

72__all__ = ["run_diagnostics"]