# The venue that actually runs the live Channel Access suites.
#
# tests/va/test_apply_fault.py asserts the scenario faults from the far side
# of a real Channel Access wire, and tests/va/test_lume_pva_seam.py asserts the
# model runner's seam on both transports (the suite --pva requires): a real
# pcaspy and p4p server, a real composite, and real aioca and p4p clients.
# They need pcaspy, which publishes no loadable macOS arm64 wheel, so on a
# developer's Mac they skip -- loudly, but they skip, and a skipped live suite
# proves nothing. This image is where they are not allowed to skip.
#
# linux/amd64 is the default in the FROM line rather than left to the caller,
# because it is the venue's defining property, not a preference: pcaspy
# publishes manylinux x86_64 wheels only (no aarch64 wheel exists at any
# interpreter), so an arm64 build of this image could not install the one
# dependency it exists to provide. Defaulting it here makes a useless build
# take a deliberate act rather than an omission. On an arm64 host this runs
# emulated.
#
# python3.11 matches CI's oldest unit-test lane, which is the lane that must
# not silently stop exercising Channel Access.
#
# uv is pinned rather than floating: which workspace projects `uv sync` builds
# is uv's behaviour, not the lockfile's, so an unpinned uv could change what
# this image installs without any change to the repo. 0.9.30 is the last
# release published on bookworm (later ones are trixie-only), and is what the
# unversioned tag still resolves to -- pinning it changed nothing but the name.
ARG BASE_PLATFORM=linux/amd64
FROM --platform=${BASE_PLATFORM} ghcr.io/astral-sh/uv:0.9.30-python3.11-bookworm-slim

# Dependencies come from the repo's own lockfile via the repo's own extras --
# the same `uv sync --extra dev --extra virtual-accelerator` CI runs, on the
# same platform CI runs it. That is deliberate: a hand-maintained pip list here
# would drift from what CI installs, and this venue's whole job is to prove
# what CI will do. --frozen means the lock is used as committed and never
# re-resolved, so the container cannot quietly pick a different pcaspy than the
# one uv.lock pins.
#
# Only the dependency closure is installed (--no-install-workspace, which skips
# the root project AND every workspace member). The osprey source is
# bind-mounted at /work and put on the path at run time, so editing a test or a
# module never invalidates this layer -- it is rebuilt only when a pyproject or
# uv.lock changes.
#
# --no-install-project is not enough since osprey-connectors became a uv
# workspace member (the root pyproject's [tool.uv.workspace]): it skips only
# the root project, and uv still installs the member as an editable. With the
# member's directory absent that fails as "Distribution not found at:
# file:///opt/osprey/packages/osprey-connectors"; with it present, the build
# fails instead, because the member versions from git tags via hatch-vcs and
# the context holds no .git/.
#
# Every member's pyproject.toml (and README.md) is staged anyway, though the
# sync above does not need them: they are a few kilobytes, and keep the
# workspace resolvable should a later uv read member metadata during a frozen
# sync. A member's source is never staged: like osprey's own, it arrives over
# the mount. Installing the projects for real would mean passing a version in,
# as docker/virtual-accelerator/Containerfile does, and rebuilding this layer
# on every source edit.
#
# The image carries the root project's entry points as metadata only: a
# dist-info holding a METADATA file and an entry_points.txt generated from the
# staged pyproject.toml's [project.entry-points.*] tables, so a plug-in group
# (the simulation engines a composite loads by name) resolves exactly as it
# does in an installed project. It is generated from the staged file, never
# spelled here, so this layer still rebuilds only when a pyproject or uv.lock
# changes.
#
# The build context is therefore just those files, staged by run_live_ca.sh --
# NOT the repo root, which also holds .git/, .venv/ and the worktrees and would
# make every build re-tar gigabytes the image never reads.
ENV UV_PROJECT_ENVIRONMENT=/opt/venv \
    UV_LINK_MODE=copy
WORKDIR /opt/osprey
COPY pyproject.toml uv.lock README.md /opt/osprey/
COPY packages/ /opt/osprey/packages/
RUN uv sync --frozen --no-install-workspace --extra dev --extra virtual-accelerator

# Each staged member's src/ on the mount goes on the path through a .pth file,
# generated from the staged directory names rather than spelled out in ENV, so
# a new member under packages/ needs no edit here.
# tests/va/test_live_ca_venue_staging.py pins that every member lives there.
RUN site="$(/opt/venv/bin/python -c 'import sysconfig; print(sysconfig.get_path("purelib"))')" \
 && for member in /opt/osprey/packages/*/; do \
        echo "/work/packages/$(basename "${member}")/src"; \
    done > "${site}/osprey-workspace-members.pth"

# The root project's entry points, as metadata only (see above). Groups and
# names are written sorted; the version is a fixed placeholder.
RUN /opt/venv/bin/python -c 'import pathlib, sysconfig, tomllib; \
project = tomllib.loads(pathlib.Path("/opt/osprey/pyproject.toml").read_text())["project"]; \
groups = project.get("entry-points", {}); \
dist = pathlib.Path(sysconfig.get_path("purelib")) / (project["name"].replace("-", "_") + "-0.dist-info"); \
dist.mkdir(); \
(dist / "METADATA").write_text("Metadata-Version: 2.1\nName: " + project["name"] + "\nVersion: 0\n"); \
(dist / "entry_points.txt").write_text("".join("[" + group + "]\n" + "".join(name + " = " + groups[group][name] + "\n" for name in sorted(groups[group])) + "\n" for group in sorted(groups)))'

# Channel Access stays inside this container: the servers and their clients
# share this network namespace, so nothing is published and no host port is
# claimed. That is why this venue never collides with an operator's
# demo stack on 5064 -- not because it picks a polite port, but because it has
# no reachable port at all.
#
# The suites pick their own ephemeral loopback port at import time (libca
# latches EPICS_CA_* when the C library initialises, earlier than any fixture
# can run), so the ports are deliberately left unset here. Only the loopback
# discipline is pinned, which is what stops a stray broadcast search.
ENV PATH=/opt/venv/bin:$PATH \
    PYTHONPATH=/work/src \
    PYTHONDONTWRITEBYTECODE=1 \
    EPICS_CA_ADDR_LIST=127.0.0.1 \
    EPICS_CA_AUTO_ADDR_LIST=NO

# /work is the repo, mounted read-only. The gate script is read from there too
# rather than copied in, so it stays editable without a rebuild.
#
# --pva in the default command, matching what run_live_ca.sh passes: the extra
# installs all three server roots, so this image can always reach the served
# boot, and --pva is what makes the gate REQUIRE it rather than accept the
# missing-server-module fallback. A bare `docker run` of this image must not be
# a weaker check than the script's.
WORKDIR /work
CMD ["python", "-u", "scripts/va/live_ca/gate.py", "--pva"]
