-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_mercury.py
More file actions
87 lines (70 loc) · 2.35 KB
/
Copy pathstart_mercury.py
File metadata and controls
87 lines (70 loc) · 2.35 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
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""Start Mercury with env loaded from .env.mercury."""
from __future__ import annotations
import argparse
import os
import runpy
import sys
from pathlib import Path
def _parse_env_value(raw: str) -> str:
s = raw.strip()
if len(s) >= 2 and ((s[0] == "'" and s[-1] == "'") or (s[0] == '"' and s[-1] == '"')):
return s[1:-1]
return s
def load_env_file(path: Path) -> None:
if not path.is_file():
raise FileNotFoundError(f"Missing env file: {path}")
for line in path.read_text(encoding="utf-8").splitlines():
s = line.strip()
if not s or s.startswith("#"):
continue
if s.startswith("export "):
s = s[len("export ") :].strip()
if "=" not in s:
continue
key, value = s.split("=", 1)
key = key.strip()
if not key:
continue
os.environ[key] = _parse_env_value(value)
def _normalize_entry(entry: str) -> str:
e = (entry or "").strip()
if e in ("run", "run.py"):
return "run.py"
if e in ("run-lan", "run-lan.py"):
return "run-lan.py"
return e
def main() -> int:
parser = argparse.ArgumentParser(description="Start Mercury with env from .env.mercury")
parser.add_argument(
"entry",
nargs="?",
default="run.py",
help="Entry file: run.py or run-lan.py (aliases: run, run-lan)",
)
args = parser.parse_args()
root = Path(__file__).resolve().parent
entry_name = _normalize_entry(args.entry)
entry_path = root / entry_name
env_path = root / ".env.mercury"
try:
load_env_file(env_path)
except FileNotFoundError as exc:
print(f"[mercury] {exc}")
return 1
if not os.environ.get("MERCURY_USERS"):
print("[mercury] MERCURY_USERS is empty. Set it in .env.mercury")
return 1
if not os.environ.get("MERCURY_SECRET_KEY"):
print("[mercury] MERCURY_SECRET_KEY is empty. Set it in .env.mercury")
return 1
if not entry_path.is_file():
print(f"[mercury] Entry file not found: {entry_path}")
return 1
print("[mercury] Starting with env from .env.mercury")
print(f"[mercury] Entry: {entry_name}")
sys.path.insert(0, str(root))
runpy.run_path(str(entry_path), run_name="__main__")
return 0
if __name__ == "__main__":
raise SystemExit(main())