Files
ISSSTELEON/tools/capture_pages.py
T

78 lines
3.0 KiB
Python

"""Capture what the reference app's pages show, for the WebApp to match: each tab of a domain's
Indicadores page as rendered by the old web image over its own API (loaded with the synthetic
hospital, as tools/capture_reference.py does), recorded as its headings, cards and plots in
order (`pages.json`), the loading indicators seen while it fills, and screenshots.
uv run python tools/capture_pages.py cacu
uv run python tools/capture_pages.py tea --hospital ~/isssteleon-data
Only the Docker CLI and a browser touch the old app; none of its code is imported or copied.
"""
import argparse
import json
import os
import socket
import subprocess
import time
import urllib.request
from pathlib import Path
from capture_reference import docker, image_id, running
HERE = Path(__file__).parent
WEB = { # the web image of each reference instance (CaCu's and CaMa's are one build)
"cacu": "isssteleon-cacu197/webapp:local",
"cama": "isssteleon/webapp:cama-dev-3743ea6",
"tea": "sitio-c/webapp:local",
}
def _port() -> int:
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
def _up(url: str, seconds: int = 120) -> None:
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
try:
urllib.request.urlopen(url, timeout=5)
return
except OSError:
time.sleep(1)
raise TimeoutError(url)
def capture(domain: str, hospital: Path, out: Path) -> None:
web = f"capture-{domain}-web"
with running(domain, hospital, curves=False) as ref:
port = _port()
docker("rm", "-f", web, check=False)
link, publish = f"{ref.name}:webapi", f"127.0.0.1:{port}:80"
docker("run", "-d", "--name", web, "--network", "bridge", "--link", link,
"-p", publish, WEB[domain]) # fmt: skip
try:
_up(f"http://127.0.0.1:{port}/")
out.mkdir(parents=True, exist_ok=True)
env = {**os.environ, "BASE": f"http://127.0.0.1:{port}", "DOMAIN": domain,
"OUT": str(out.resolve())} # fmt: skip
subprocess.run(["node", str(HERE / "capture_pages.mjs")], cwd=HERE.parent / "WebApp",
env=env, check=True) # fmt: skip
manifest = {"domain": domain, "web": {"tag": WEB[domain], "id": image_id(WEB[domain])},
"api": {"tag": ref.image, "id": image_id(ref.image)},
"load": ref.load.strip().splitlines()} # fmt: skip
(out / "manifest.json").write_text(json.dumps(manifest, indent=1) + "\n")
finally:
docker("rm", "-f", web, check=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("domain", choices=WEB)
parser.add_argument("--hospital", type=Path, default=Path("tests/fixtures/hospital"))
parser.add_argument("--out", type=Path, default=Path("tests/fixtures/reference"))
args = parser.parse_args()
capture(args.domain, args.hospital, args.out / args.domain / "ui")