Skip to content
File

Blob: archive/sfu-bringup/monitor.py

python74 lines
1#!/usr/bin/env python3
2"""Read diagnostic output over native USB without toggling reset lines."""
3import json
4import os
5from pathlib import Path
6import sys
7import re
8import shlex
9import time
10import termios
11import serial
12 
13root = Path(__file__).resolve().parents[2]
14sys.path.insert(0, str(root / "scripts"))
15from serial_device import device_port
16os.umask(0o077)
17secrets = []
18for line in (root / ".credential.env").read_text().splitlines():
19 match = re.match(r"^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$", line)
20 if match:
21 value = " ".join(shlex.split(match.group(2), comments=True))
22 if value:
23 secrets.append(value)
24port = serial.Serial(port=None, baudrate=115200, timeout=0.2, exclusive=True)
25port.dtr = True
26port.rts = True
27find_port = device_port
28for attempt in range(40):
29 try:
30 port.port = find_port()
31 port.open()
32 attrs = termios.tcgetattr(port.fileno())
33 attrs[2] &= ~termios.HUPCL
34 termios.tcsetattr(port.fileno(), termios.TCSANOW, attrs)
35 break
36 except (serial.SerialException, RuntimeError):
37 time.sleep(0.25)
38else:
39 raise SystemExit("USB serial port did not reappear")
40pending = bytearray()
41deadline = time.monotonic() + 80
42out = root / "artifacts/archive/sfu-bringup"
43out.mkdir(parents=True, exist_ok=True, mode=0o700)
44ready = False
45with (out / "hardware-serial.log").open("a") as log:
46 while time.monotonic() < deadline:
47 try:
48 pending.extend(port.read(4096))
49 except serial.SerialException:
50 print("USB disconnected during the diagnostic run", flush=True)
51 break
52 while b"\n" in pending:
53 raw, _, remainder = pending.partition(b"\n")
54 pending = bytearray(remainder)
55 line = raw.decode(errors="replace").strip()
56 for value in secrets:
57 line = line.replace(value, "[redacted]")
58 log.write(line + "\n")
59 log.flush()
60 print(line, flush=True)
61 if line.startswith("PROBE "):
62 try:
63 event = json.loads(line[6:])
64 except ValueError:
65 continue
66 if event.get("event") == "hardware_ready":
67 (out / "hardware-result.json").write_text(json.dumps(event, indent=2) + "\n")
68 ready = True
69 if ready:
70 break
71port.close()
72if not ready:
73 raise SystemExit("No diagnostic summary received within 80 seconds")