|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +# Copyright (c) 2026 ATOM Project Contributors |
| 4 | +"""What a suite's PR run executes, derived from the repository itself, so a |
| 5 | +suite's path list can be checked against it (test_ci_gate_suites.py): |
| 6 | +
|
| 7 | +- the code it installs: the packages pyproject.toml declares; |
| 8 | +- how it installs them: the packaging files; |
| 9 | +- the run's definition: its workflow file, the local actions, workflows, |
| 10 | + scripts, data files and Dockerfiles the steps that run on a PR use, and |
| 11 | + what those in turn use (a job or step that only runs on main does not run |
| 12 | + for a PR). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import re |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import tomllib |
| 21 | +import yaml |
| 22 | + |
| 23 | +REPO = Path(__file__).resolve().parent.parent |
| 24 | +PACKAGING_FILES = ("setup.py", "pyproject.toml", "MANIFEST.in") |
| 25 | +# Where a run's definition lives. A reference is a path below one of them: |
| 26 | +# stop at whitespace, quotes and expressions; not inside a longer path or name. |
| 27 | +_ROOTS = ( |
| 28 | + ".github/scripts", |
| 29 | + ".github/actions", |
| 30 | + ".github/benchmark", |
| 31 | + ".github/workflows", |
| 32 | + "docker", |
| 33 | +) |
| 34 | +_REFERENCE = re.compile( |
| 35 | + rf"(?<![\w.-])(?:{'|'.join(map(re.escape, _ROOTS))})/[A-Za-z0-9_./-]*" |
| 36 | +) |
| 37 | +_ACTION_FILES = ("action.yml", "action.yaml") |
| 38 | +# Files the run executes, whose own references are part of the run. Any |
| 39 | +# other file a run names (a JSON, another workflow read for a pin) is its |
| 40 | +# data: relevant itself, but what it names is not run. |
| 41 | +_EXECUTED_SUFFIXES = (".sh", ".py", "dockerfile", "Dockerfile") |
| 42 | +_MAIN_ONLY = "refs/heads/main" |
| 43 | + |
| 44 | + |
| 45 | +def installed_package_dirs() -> list[str]: |
| 46 | + """Top-level directories of the packages pyproject.toml installs.""" |
| 47 | + config = tomllib.loads((REPO / "pyproject.toml").read_text(encoding="utf-8")) |
| 48 | + includes = config["tool"]["setuptools"]["packages"]["find"]["include"] |
| 49 | + return sorted({pattern.split(".", 1)[0] for pattern in includes}) |
| 50 | + |
| 51 | + |
| 52 | +def _runs_on_pr(condition) -> bool: |
| 53 | + return _MAIN_ONLY not in str(condition or "") |
| 54 | + |
| 55 | + |
| 56 | +def _existing(reference: str) -> str | None: |
| 57 | + """The repository path a reference names ('/'-terminated for a directory), |
| 58 | + or None when it names nothing that exists below a root.""" |
| 59 | + name = reference.removesuffix("/.").rstrip("/") |
| 60 | + if name in _ROOTS: |
| 61 | + return None |
| 62 | + if (REPO / name).is_file(): |
| 63 | + return name |
| 64 | + if (REPO / name).is_dir(): |
| 65 | + return f"{name}/" |
| 66 | + return None |
| 67 | + |
| 68 | + |
| 69 | +def _named_in(text: str) -> set[str]: |
| 70 | + """The paths a script, Dockerfile or step names outside its comment lines.""" |
| 71 | + code = "\n".join( |
| 72 | + line for line in text.splitlines() if not line.lstrip().startswith("#") |
| 73 | + ) |
| 74 | + return {path for path in map(_existing, _REFERENCE.findall(code)) if path} |
| 75 | + |
| 76 | + |
| 77 | +def _local(uses: str) -> str: |
| 78 | + """A local action or reusable workflow's path; '' for a published one.""" |
| 79 | + return uses.removeprefix("./") if uses.startswith("./") else "" |
| 80 | + |
| 81 | + |
| 82 | +def _executed_files(path: str) -> list[Path]: |
| 83 | + """The files of a referenced path the run executes: a script or |
| 84 | + Dockerfile itself, an action directory's action file.""" |
| 85 | + target = REPO / path |
| 86 | + if not path.endswith("/"): |
| 87 | + return [target] if path.endswith(_EXECUTED_SUFFIXES) else [] |
| 88 | + return [target / name for name in _ACTION_FILES if (target / name).is_file()] |
| 89 | + |
| 90 | + |
| 91 | +def _with_what_they_use(paths: set[str]) -> set[str]: |
| 92 | + found = set(paths) |
| 93 | + pending = list(paths) |
| 94 | + while pending: |
| 95 | + for file in _executed_files(pending.pop()): |
| 96 | + for path in _named_in(file.read_text(encoding="utf-8", errors="ignore")): |
| 97 | + if path not in found: |
| 98 | + found.add(path) |
| 99 | + pending.append(path) |
| 100 | + return found |
| 101 | + |
| 102 | + |
| 103 | +def workflow_references(workflow_file: str) -> list[str]: |
| 104 | + """Repository paths the workflow's PR-run steps use, and the paths those |
| 105 | + use in turn; a directory ends with '/'. A step's env is not read: a gate's |
| 106 | + own path lists live there.""" |
| 107 | + workflow = yaml.safe_load( |
| 108 | + (REPO / ".github" / "workflows" / workflow_file).read_text(encoding="utf-8") |
| 109 | + ) |
| 110 | + used: set[str] = set() |
| 111 | + for job in workflow["jobs"].values(): |
| 112 | + if not _runs_on_pr(job.get("if")): |
| 113 | + continue |
| 114 | + used |= _named_in(_local(job.get("uses", ""))) |
| 115 | + for step in job.get("steps") or []: |
| 116 | + if _runs_on_pr(step.get("if")): |
| 117 | + used |= _named_in(_local(step.get("uses", ""))) |
| 118 | + used |= _named_in(str(step.get("run", ""))) |
| 119 | + used |= _named_in(str(step.get("with") or "")) |
| 120 | + return sorted(_with_what_they_use(used) | {f".github/workflows/{workflow_file}"}) |
| 121 | + |
| 122 | + |
| 123 | +def example_path(reference: str) -> str: |
| 124 | + """A file the reference stands for: itself, or one inside a directory.""" |
| 125 | + return reference + "x" if reference.endswith("/") else reference |
0 commit comments