|
| 1 | +# ruff: noqa: INP001 |
| 2 | +# .ci/ is a top-level script directory invoked by GitHub Actions, not a |
| 3 | +# Python package, so no __init__.py here (mirrors .ci/warm_hf_cache.py). |
| 4 | +"""Decide test matrix scope and emit it to ``GITHUB_OUTPUT``. |
| 5 | +
|
| 6 | +Full matrix runs on push to ``dev`` and on PRs labeled ``full-ci``. |
| 7 | +Otherwise (default PR commits) only ubuntu-latest + Python 3.14 runs. |
| 8 | +On ``workflow_dispatch`` (the manual coverage run) a single |
| 9 | +ubuntu-latest + ``DISPATCH_PYTHON`` combo runs: coverage is the union of |
| 10 | +lines exercised, so it is OS/Python-independent and one combo keeps the |
| 11 | +manual job cheap. |
| 12 | +
|
| 13 | +Inputs come from environment variables: |
| 14 | +
|
| 15 | +* ``EVENT_NAME`` - the GitHub event name (``push`` / ``pull_request`` / |
| 16 | + ``workflow_dispatch``). |
| 17 | +* ``LABELS_JSON`` - ``toJSON(github.event.pull_request.labels.*.name)`` |
| 18 | + from the workflow; ``null`` / missing on non-PR events. |
| 19 | +* ``DISPATCH_PYTHON`` - Python version for the ``workflow_dispatch`` combo; |
| 20 | + falls back to ``DISPATCH_DEFAULT_PYTHON`` when unset/empty. |
| 21 | +* ``GITHUB_OUTPUT`` - file the runner reads to pick up step outputs. |
| 22 | +
|
| 23 | +The script writes ``matrix``, ``warm_os`` and ``full`` to that output |
| 24 | +file and mirrors them to the log for debuggability. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import json |
| 30 | +import logging |
| 31 | +import os |
| 32 | +import sys |
| 33 | +from pathlib import Path |
| 34 | + |
| 35 | +FULL_MATRIX = { |
| 36 | + "os": ["ubuntu-latest"], |
| 37 | + "python-version": ["3.10", "3.11", "3.12", "3.13", "3.14"], |
| 38 | + "include": [{"os": "windows-latest", "python-version": "3.10"}], |
| 39 | +} |
| 40 | + |
| 41 | +MINIMAL_MATRIX = { |
| 42 | + "os": ["ubuntu-latest"], |
| 43 | + "python-version": ["3.14"], |
| 44 | +} |
| 45 | + |
| 46 | +FULL_CI_LABEL = "full-ci" |
| 47 | + |
| 48 | +DISPATCH_EVENT = "workflow_dispatch" |
| 49 | +DISPATCH_DEFAULT_PYTHON = "3.12" |
| 50 | + |
| 51 | + |
| 52 | +def dispatch_matrix(python_version: str) -> dict: |
| 53 | + """Return the single-combo matrix for the manual coverage run.""" |
| 54 | + return {"os": ["ubuntu-latest"], "python-version": [python_version or DISPATCH_DEFAULT_PYTHON]} |
| 55 | + |
| 56 | + |
| 57 | +logger = logging.getLogger("compute_matrix") |
| 58 | + |
| 59 | + |
| 60 | +def collect_os_list(matrix: dict) -> list[str]: |
| 61 | + """Return the unique runner OSes referenced by ``matrix`` (base + includes).""" |
| 62 | + seen: list[str] = [] |
| 63 | + for entry in matrix.get("os", []): |
| 64 | + if entry not in seen: |
| 65 | + seen.append(entry) |
| 66 | + for include in matrix.get("include", []): |
| 67 | + entry = include.get("os") |
| 68 | + if entry and entry not in seen: |
| 69 | + seen.append(entry) |
| 70 | + return seen |
| 71 | + |
| 72 | + |
| 73 | +def is_full(event_name: str, labels: list[str]) -> bool: |
| 74 | + """Return True iff this run should fan out across the full OS/Python matrix.""" |
| 75 | + # Any push that reaches this workflow is a push to `dev` (ci.yaml pins |
| 76 | + # on.push.branches: [dev]), so the branch is implied and not re-checked |
| 77 | + # here. If more push branches are ever added there, revisit this. |
| 78 | + if event_name == "push": |
| 79 | + return True |
| 80 | + return FULL_CI_LABEL in labels |
| 81 | + |
| 82 | + |
| 83 | +def parse_labels(raw: str) -> list[str]: |
| 84 | + """Parse the ``LABELS_JSON`` env var into a list of label names. |
| 85 | +
|
| 86 | + Returns an empty list when the value is missing, ``"null"`` (the |
| 87 | + ``toJSON`` rendering of a missing PR object), malformed, or not a |
| 88 | + JSON array of strings. |
| 89 | + """ |
| 90 | + if not raw: |
| 91 | + return [] |
| 92 | + try: |
| 93 | + decoded = json.loads(raw) |
| 94 | + except json.JSONDecodeError: |
| 95 | + return [] |
| 96 | + if not isinstance(decoded, list): |
| 97 | + return [] |
| 98 | + return [item for item in decoded if isinstance(item, str)] |
| 99 | + |
| 100 | + |
| 101 | +def main() -> int: |
| 102 | + """Compute matrix from env, log a summary, write outputs; return exit code.""" |
| 103 | + logging.basicConfig(level=logging.INFO, format="%(message)s", stream=sys.stderr) |
| 104 | + |
| 105 | + event_name = os.environ.get("EVENT_NAME", "") |
| 106 | + labels = parse_labels(os.environ.get("LABELS_JSON", "")) |
| 107 | + |
| 108 | + if event_name == DISPATCH_EVENT: |
| 109 | + full = False |
| 110 | + matrix = dispatch_matrix(os.environ.get("DISPATCH_PYTHON", "")) |
| 111 | + else: |
| 112 | + full = is_full(event_name, labels) |
| 113 | + matrix = FULL_MATRIX if full else MINIMAL_MATRIX |
| 114 | + warm_os = collect_os_list(matrix) |
| 115 | + |
| 116 | + payload = { |
| 117 | + "matrix": json.dumps(matrix), |
| 118 | + "warm_os": json.dumps(warm_os), |
| 119 | + "full": "true" if full else "false", |
| 120 | + } |
| 121 | + |
| 122 | + logger.info("event_name=%s", event_name) |
| 123 | + logger.info("labels=%s", labels) |
| 124 | + logger.info("full=%s", full) |
| 125 | + logger.info("matrix=%s", payload["matrix"]) |
| 126 | + logger.info("warm_os=%s", payload["warm_os"]) |
| 127 | + |
| 128 | + output_path = os.environ.get("GITHUB_OUTPUT") |
| 129 | + if not output_path: |
| 130 | + logger.error("GITHUB_OUTPUT is not set; cannot emit step outputs") |
| 131 | + return 1 |
| 132 | + lines = "".join(f"{key}={value}\n" for key, value in payload.items()) |
| 133 | + with Path(output_path).open("a", encoding="utf-8") as fh: |
| 134 | + fh.write(lines) |
| 135 | + return 0 |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + sys.exit(main()) |
0 commit comments