__init__.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Hermes CLI - Unified command-line interface for Hermes Agent."""
  2. import sys
  3. __release_date__ = "2026.9.24"
  4. # Declared for type checkers and the old-updater surface audit; served lazily by __getattr__.
  5. __version__: str
  6. def __getattr__(name: str) -> str:
  7. """Old-updater compat: shipped updaters import ``__version__`` after the checkout swap.
  8. tests/compat/old_updater_surface.json freezes that import. In-tree code resolves
  9. identity through hermes_cli.version_info.get_version_info(); this reads only the
  10. install stamp -- never git -- and keeps the pre-stamp placeholder when a checkout
  11. has no stamp.
  12. Lazy because ``pm`` is not importable when this package loads: a venv
  13. editable-installed from a pre-PM tree maps only the top-level packages it knew
  14. at install time, and the repo root reaches ``sys.path`` only once
  15. ``hermes_bootstrap`` runs -- after this ``__init__``, from ``hermes_cli.main``.
  16. """
  17. if name != "__version__":
  18. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  19. from hermes_cli.steward import read_install_stamp
  20. try:
  21. from pm.paths import repo_root
  22. except ModuleNotFoundError as exc:
  23. if exc.name != "pm" and not (exc.name or "").startswith("pm."):
  24. raise
  25. # The old editable finder may not know the new pm package yet.
  26. import json
  27. from pathlib import Path
  28. try:
  29. stamp = json.loads((Path(__file__).resolve().parents[1] / "install-stamp.json").read_text(encoding="utf-8-sig"))
  30. return str(stamp.get("baseVersion") or "0.0.0")
  31. except (OSError, ValueError, AttributeError):
  32. return "0.0.0"
  33. return str(read_install_stamp(repo_root()).get("baseVersion") or "0.0.0")
  34. def _ensure_utf8() -> bool:
  35. """Force UTF-8 stdout/stderr to prevent UnicodeEncodeError crashes; True when a stream was repaired.
  36. The CLI prints box-drawing characters and the ☤ glyph in the setup wizard, doctor, and status
  37. banners; under a non-UTF-8 codec that raises before the command can even start (e.g.
  38. `hermes setup` on a fresh Pi).
  39. """
  40. repaired = False
  41. for stream_name in ("stdout", "stderr"):
  42. stream = getattr(sys, stream_name, None)
  43. if stream is None:
  44. continue
  45. try:
  46. if (getattr(stream, "encoding", "") or "").lower().replace("-", "") == "utf8":
  47. continue
  48. # Preferred: reconfigure in place, preserving object identity so code already holding
  49. # a reference to the old sys.stdout benefits from the repair too.
  50. reconfigure = getattr(stream, "reconfigure", None)
  51. if callable(reconfigure):
  52. reconfigure(encoding="utf-8", errors="replace")
  53. else:
  54. # No reconfigure(): reopen the fd as UTF-8 (closefd=False keeps the original fd open).
  55. new_stream = open(stream.fileno(), "w", encoding="utf-8", errors="replace", # windows-footgun: ok (stdout re-open for write, not a read)
  56. buffering=1, closefd=False)
  57. setattr(sys, stream_name, new_stream)
  58. repaired = True
  59. except (AttributeError, OSError, ValueError):
  60. pass
  61. return repaired
  62. # Import repairs only this process's streams. Gateway, compute host, and test code import this
  63. # package as a library; rewriting their os.environ would leak into every child they spawn, so the
  64. # child-process UTF-8 hint is applied by the CLI entry point (hermes_cli.main.main) instead.
  65. _stdio_repaired = _ensure_utf8()