-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
68 lines (51 loc) · 1.97 KB
/
cli.py
File metadata and controls
68 lines (51 loc) · 1.97 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
"""Command line interface for tracee."""
import argparse
import importlib.util
import shutil
import subprocess
import sys
from pathlib import Path
_ROOT = Path(__file__).resolve().parent
_UI_DIR = _ROOT / "playground-ui"
_DIST_DIR = _UI_DIR / "dist"
def _build_frontend():
"""build the frontend if dist/ is missing and npm is available."""
if _DIST_DIR.exists():
return
if not _UI_DIR.exists():
return
npm = shutil.which("npm")
if npm is None:
print(
"warning: playground-ui/dist/ not found and npm is not installed. "
"the server will start without the UI. "
"install Node.js (v18+) and re-run to get the web interface.",
file=sys.stderr,
)
return
print("playground-ui/dist/ not found — building frontend...")
subprocess.run([npm, "install"], cwd=_UI_DIR, check=True)
subprocess.run([npm, "run", "build"], cwd=_UI_DIR, check=True)
print("frontend build complete.")
def main():
parser = argparse.ArgumentParser(
prog="tracee",
description="tracee - MAS tracing and visualization toolkit",
)
subcommands = parser.add_subparsers(dest="command")
serve = subcommands.add_parser("serve", help="start the tracee server and UI")
serve.add_argument("--port", type=int, default=8000)
serve.add_argument("--host", default="0.0.0.0")
serve.add_argument("--skip-build", action="store_true", help="skip automatic frontend build")
args = parser.parse_args()
if args.command == "serve":
if importlib.util.find_spec("fastapi") is None or importlib.util.find_spec("uvicorn") is None:
parser.error("`tracee serve` requires server extras. Install with `pip install 'tracee[server]'`.")
if not args.skip_build:
_build_frontend()
import uvicorn
uvicorn.run("server.app:app", host=args.host, port=args.port)
return
parser.print_help()
if __name__ == "__main__":
main()