forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_plugin_imports.py
More file actions
75 lines (63 loc) · 2.4 KB
/
Copy pathcheck_plugin_imports.py
File metadata and controls
75 lines (63 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""Import each Python plugin app and build OpenAPI schemas when dependencies are present."""
import importlib
import os
import sys
from pathlib import Path
from typing import List, Tuple
ROOT = Path(__file__).resolve().parents[2]
PLUGINS = ROOT / "plugins"
SDK_SRC = PLUGINS / "omi-plugin-sdk" / "src"
def main() -> int:
blockers: List[Tuple[str, str]] = []
checked = 0
plugin_roots = [path.resolve() for path in PLUGINS.glob("omi-*-app")]
for main_py in sorted(PLUGINS.glob("omi-*-app/main.py")):
app_dir = main_py.parent
module_name = "main"
checked += 1
sys.path[:0] = [str(app_dir), str(SDK_SRC)]
old_cwd = Path.cwd()
os.chdir(app_dir)
try:
_purge_plugin_modules(plugin_roots)
module = importlib.import_module(module_name)
app = getattr(module, "app", None)
if app is None:
blockers.append((app_dir.name, "main.py imported but has no app attribute"))
continue
app.openapi()
except ModuleNotFoundError as exc:
blockers.append((app_dir.name, f"missing dependency: {exc.name}"))
except Exception as exc: # noqa: BLE001 - smoke script must report import blockers without hiding later apps.
blockers.append((app_dir.name, f"{type(exc).__name__}: {exc}"))
finally:
os.chdir(old_cwd)
for path in (str(app_dir), str(SDK_SRC)):
try:
sys.path.remove(path)
except ValueError:
pass
print(f"checked={checked}")
if blockers:
print("blockers:")
for app, reason in blockers:
print(f"- {app}: {reason}")
return 1
print("all plugin apps imported and generated OpenAPI")
return 0
def _purge_plugin_modules(plugin_roots: List[Path]) -> None:
for name, module in list(sys.modules.items()):
module_file = getattr(module, "__file__", None)
if not module_file:
continue
try:
resolved = Path(module_file).resolve()
except OSError:
continue
if any(_is_relative_to(resolved, root) for root in plugin_roots):
sys.modules.pop(name, None)
def _is_relative_to(path: Path, root: Path) -> bool:
return path == root or root in path.parents
if __name__ == "__main__":
raise SystemExit(main())