Files
aigc-home/e2e/run-animate2-loop-document.py
T

124 lines
4.7 KiB
Python

"Foreground local browser check; stop every owned service before returning."
from __future__ import annotations
import hashlib
import json
import os
import signal
import subprocess
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / (".local-evidence/animate2-loop-document/browser")
def main():
# Require successful rebuilt and packaged current resources before any service starts.
evidence = ROOT / "docs/hel-892/animate2-loop-document-evidence"
for record in json.loads((evidence / "package-check.json").read_bytes()):
product = ROOT / "calliope-backend/src/calliope/comfyui" / record["file"]
assert hashlib.sha256(product.read_bytes()).hexdigest() == record["sha256"]
assert record["wheel"] and record["sdist"] and record["definitions"] == 145
protection = json.loads((evidence / "import-protection.json").read_bytes())
assert protection["upgrade"] == {"updated": 1, "skipped": 154, "user_excluded": 1}
data = ROOT / ".local-evidence/animate2-loop-document-server"
assert not data.exists(), "Prior owned data must be reversibly quarantined before another run"
OUT.mkdir(parents=True, exist_ok=True)
(OUT / "fresh-path-check.json").write_text(
json.dumps(dict(data_path=str(data.relative_to(ROOT)), existed_before_start=False)) + "\n"
)
# Refuse to reuse another operator's service or port.
import socket
for port in (8247, 5173):
with socket.socket() as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", port))
processes = []
logs = []
try:
commands = [
([sys.executable, str(ROOT / "e2e/animate2-loop-document-server.py")], ROOT),
(
["node", "node_modules/vite/bin/vite.js", "--host", "127.0.0.1"],
ROOT / "calliope-web",
),
]
for i, (command, cwd) in enumerate(commands):
log = (OUT / f"service-{i}.log").open("w")
logs.append(log)
processes.append(
subprocess.Popen(command, cwd=cwd, stdout=log, stderr=log, start_new_session=True)
)
for url in (
"http://127.0.0.1:8247/api/workflows",
"http://127.0.0.1:5173/connections",
):
parsed = urlsplit(url)
if (
parsed.scheme != "http"
or parsed.hostname not in {"127.0.0.1", "localhost"}
or parsed.port not in {8247, 5173}
or parsed.username
or parsed.password
):
raise ValueError("Refused before readiness request: " + url)
deadline = time.monotonic() + 30
while True:
if any(p.poll() is not None for p in processes):
raise RuntimeError("Local service exited; inspect service logs")
try:
with urllib.request.urlopen(url, timeout=1) as response:
if response.status == 200:
break
except (urllib.error.URLError, TimeoutError):
pass
if time.monotonic() > deadline:
raise TimeoutError(url)
time.sleep(0.2)
names = ["animate2-loop-document"]
for name in names:
subprocess.run(["node", f"e2e/{name}.mjs"], cwd=ROOT, check=True)
finally:
for p in processes:
if p.poll() is None:
os.killpg(p.pid, signal.SIGTERM)
try:
p.wait(timeout=10)
except subprocess.TimeoutExpired:
os.killpg(p.pid, signal.SIGKILL)
p.wait(timeout=5)
for log in logs:
log.close()
released = []
for port in (8247, 5173):
with socket.socket() as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", port))
released.append(port)
with socket.socket() as probe:
probe.settimeout(1)
assert probe.connect_ex(("127.0.0.1", port)) != 0
(OUT / "service-stop.json").write_text(
json.dumps(
{
"ports_released": released,
"independent_connection_refused": released,
"child_exit_codes": [p.returncode for p in processes],
}
)
+ "\n"
)
print(("All owned browser services stopped; ports released."))
if __name__ == "__main__":
main()