"""Where the session files are, and how to refresh them. The repo root already has `download_data.py`, which mirrors the whole data repo into `downloaded_data/`. This module does not re-implement that; it points at the mirror and decides which of its files are participant sessions. """ from __future__ import annotations import json import re import subprocess import sys from pathlib import Path REPO = Path(__file__).resolve().parents[2] MIRROR = REPO / "downloaded_data" #: Superseded files live under pilot/history/, and every id the team used for #: its own runs is prefixed. Both are excluded from the participant pool: the #: history directory because those sessions were replaced, the prefixes because #: they are us. TEST_/SMOKE_ sessions have reached the live data repo before. _NOT_A_PARTICIPANT = re.compile(r"^(SMOKE|TEST|DEV|DEMO)", re.I) def _is_dev_session(msgpack_path: Path) -> bool: """True when the session's own metadata says it was a debug run. The prefix test above reads the FOLDER name, which on a SONA link is a one-way hash of the survey code: TEST_ra3 lands in pilot/ab060ed0ac83ea86/ and looks exactly like a participant. Every dev link sets dev_mode, and the final push writes it into the metadata, so that is the thing to read. Only the metadata can answer this, so a debug session whose last push failed is still invisible here. That was every session until 2026-10-17. """ meta = msgpack_path.with_suffix(".json") if not meta.exists(): return False try: with open(meta, encoding="utf-8") as f: return bool(json.load(f).get("user_storage", {}).get("dev_mode")) except (OSError, ValueError): return False def pull(quiet: bool = False) -> Path: """Refresh the mirror from GitLab. Returns the mirror path.""" r = subprocess.run([sys.executable, str(REPO / "download_data.py")], cwd=REPO, capture_output=True, text=True) if r.returncode != 0: raise RuntimeError(f"download_data.py failed:\n{r.stdout[-2000:]}\n{r.stderr[-2000:]}") if not quiet: print(r.stdout.strip()[-1000:]) return MIRROR def session_files(mirror: Path | None = None, include_history: bool = False, include_dev: bool = False) -> list[Path]: """Every participant session in the mirror, sorted. One path per session, not per pid: a pid can be several people, because the pilot links were handed out as "pick one of these at random". Dropped: the history directory, folders named for one of the team's own prefixes, and any session whose metadata says dev_mode. The last of those is the one that catches a debug run on a SONA link, whose folder is a hash and whose name says nothing. `include_dev` brings them back, for checking a test run end to end. """ root = Path(mirror or MIRROR) if not root.exists(): return [] out = [] for p in sorted(root.rglob("*.msgpack")): parts = p.relative_to(root).parts if not include_history and "history" in parts: continue if _NOT_A_PARTICIPANT.match(p.parent.name): continue if not include_dev and _is_dev_session(p): continue out.append(p) return out