bootstrap.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. """Share exactly one runtime between Hermes' independent plugin loaders."""
  2. import _imp
  3. import importlib.util
  4. import sys
  5. from pathlib import Path
  6. def runtime():
  7. # Hermes loads tool packages and dashboard APIs under unrelated module names.
  8. # Relative imports alone would give them independent authorization tables.
  9. name = "_yaoyao_bot_bridge_runtime_v1"
  10. _imp.acquire_lock()
  11. try:
  12. module = sys.modules.get(name)
  13. if module is None:
  14. spec = importlib.util.spec_from_file_location(
  15. name, Path(__file__).with_name("bridge_runtime.py")
  16. )
  17. if spec is None or spec.loader is None:
  18. raise ImportError("夭夭工具桥运行模块无法加载")
  19. module = importlib.util.module_from_spec(spec)
  20. sys.modules[name] = module
  21. try:
  22. spec.loader.exec_module(module)
  23. except BaseException:
  24. sys.modules.pop(name, None)
  25. raise
  26. return module
  27. finally:
  28. _imp.release_lock()