-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
40 lines (37 loc) · 1.34 KB
/
Copy pathstart.py
File metadata and controls
40 lines (37 loc) · 1.34 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
#!/usr/bin/env python3
"""Start a detached, loopback-only board; preserve existing practice state."""
import json
from pathlib import Path
import socket
import subprocess
import sys
import time
import urllib.request
import webbrowser
ROOT = Path(__file__).resolve().parent
URL = "http://127.0.0.1:8769"
def healthy():
try:
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
with opener.open(URL + "/api/health", timeout=1) as response:
return json.load(response).get("app") == "go-coach"
except Exception:
return False
if not healthy():
with socket.socket() as sock:
if sock.connect_ex(("127.0.0.1",8769)) == 0:
sys.exit("8769 端口已被其他程序占用,请检查后再启动。")
(ROOT / ".local").mkdir(exist_ok=True)
with open(ROOT / ".local/server.log", "a") as log:
process = subprocess.Popen([sys.executable,str(ROOT / "server.py")],cwd=ROOT,
stdin=subprocess.DEVNULL,stdout=log,stderr=log,start_new_session=True)
(ROOT / ".local/server.pid").write_text(str(process.pid))
for _ in range(40):
if healthy():
break
time.sleep(0.15)
else:
sys.exit("棋盘未启动,请查看 .local/server.log。")
print(URL)
if "--no-browser" not in sys.argv:
webbrowser.open(URL)