hermes_import.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """One-time, read-only import of an existing Hermes home into Yaoyao's embedded home.
  2. Usage: python hermes_import.py SOURCE STAGING
  3. SOURCE is never modified. SQLite databases are copied with the online backup API, so a
  4. Hermes process that is still running is read as a consistent snapshot. Re-downloadable
  5. runtimes, caches, logs, process files and Hermes' own installation are skipped. The
  6. yaoyao-bot-bridge plugin is skipped: the embedded kernel has no plugin bridge.
  7. Prints a JSON manifest on stdout.
  8. """
  9. import hashlib
  10. import json
  11. import os
  12. import shutil
  13. import sqlite3
  14. import sys
  15. import time
  16. from pathlib import Path
  17. SKIP_DIRS = {"hermes-agent", "venv", ".venv", "node_modules", "models", "runtimes", "node", "logs",
  18. "cache", "__pycache__", ".git", "sandboxes", "tmp", "pm"}
  19. SKIP_PLUGINS = {"yaoyao-bot-bridge"}
  20. SKIP_SUFFIXES = (".pid", ".lock", ".sock", "-wal", "-shm", "-journal")
  21. MAX_FILE = 512 * 1024 * 1024
  22. def is_sqlite(path: Path) -> bool:
  23. try:
  24. with path.open("rb") as handle:
  25. return handle.read(16) == b"SQLite format 3\x00"
  26. except OSError:
  27. return False
  28. def backup(source: Path, target: Path) -> None:
  29. src = sqlite3.connect(f"file:{source}?mode=ro", uri=True, timeout=30)
  30. try:
  31. dst = sqlite3.connect(target)
  32. try:
  33. src.backup(dst)
  34. finally:
  35. dst.close()
  36. finally:
  37. src.close()
  38. def digest(path: Path) -> str:
  39. h = hashlib.sha256()
  40. with path.open("rb") as handle:
  41. for chunk in iter(lambda: handle.read(1024 * 1024), b""):
  42. h.update(chunk)
  43. return h.hexdigest()
  44. def main(source: Path, staging: Path) -> dict:
  45. source = source.expanduser().resolve(strict=True)
  46. staging.mkdir(parents=True, exist_ok=False, mode=0o700)
  47. files, skipped = [], 0
  48. for root, dirs, names in os.walk(source, followlinks=False):
  49. current = Path(root)
  50. relative = current.relative_to(source)
  51. dirs[:] = [d for d in dirs if d not in SKIP_DIRS
  52. and not (relative.name == "plugins" and d in SKIP_PLUGINS)
  53. and not (current / d).is_symlink()]
  54. (staging / relative).mkdir(parents=True, exist_ok=True, mode=0o700)
  55. for name in names:
  56. path = current / name
  57. if name.endswith(SKIP_SUFFIXES) or path.is_symlink() or not path.is_file():
  58. skipped += 1
  59. continue
  60. if path.stat().st_size > MAX_FILE:
  61. skipped += 1
  62. continue
  63. target = staging / relative / name
  64. if is_sqlite(path):
  65. backup(path, target)
  66. else:
  67. shutil.copy2(path, target)
  68. os.chmod(target, 0o600)
  69. files.append({"path": str(relative / name), "sha256": digest(target)})
  70. version = None
  71. stamp = source / "hermes-agent" / "pyproject.toml"
  72. if stamp.exists():
  73. for line in stamp.read_text(errors="replace").splitlines():
  74. if line.strip().startswith("version"):
  75. version = line.split("=", 1)[1].strip().strip('"')
  76. break
  77. manifest = {"version": 1, "source": str(source), "importedAt": int(time.time() * 1000),
  78. "hermesVersion": version, "files": files, "skipped": skipped}
  79. (staging / ".import.json").write_text(json.dumps(manifest, ensure_ascii=False))
  80. os.chmod(staging / ".import.json", 0o600)
  81. return manifest
  82. if __name__ == "__main__":
  83. result = main(Path(sys.argv[1]), Path(sys.argv[2]))
  84. print(json.dumps({"files": len(result["files"]), "skipped": result["skipped"], "hermesVersion": result["hermesVersion"]}))