|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Download LastTest.log artifacts from the most recent GitHub Actions CI run |
| 4 | +and update the platform-specific md5refs files. |
| 5 | +
|
| 6 | +Requirements: gh CLI authenticated (gh auth login). |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python tests/nonregression/collect_ci_md5refs.py |
| 10 | + python tests/nonregression/collect_ci_md5refs.py --run-id 1234567890 |
| 11 | + python tests/nonregression/collect_ci_md5refs.py --dry-run |
| 12 | +""" |
| 13 | + |
| 14 | +import argparse |
| 15 | +import json |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import tempfile |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +REPO = "GrokImageCompression/grok" |
| 22 | +SCRIPT_DIR = Path(__file__).parent |
| 23 | + |
| 24 | +# Maps artifact name pattern -> platform key for update_md5refs.py |
| 25 | +# Artifact names: test-results-<os>-shared_<ON|OFF> |
| 26 | +ARTIFACT_PLATFORM_MAP = { |
| 27 | + ("macos-latest", "ON"): "Darwin", |
| 28 | + ("macos-latest", "OFF"): "Darwin-static", |
| 29 | + ("windows-latest","ON"): "Windows", |
| 30 | + ("windows-latest","OFF"): "Windows-static", |
| 31 | + ("ubuntu-latest", "ON"): None, # canonical md5refs.txt — skip |
| 32 | + ("ubuntu-latest", "OFF"): "Linux-static", |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def run(cmd, **kwargs): |
| 37 | + return subprocess.run(cmd, check=True, text=True, capture_output=True, **kwargs) |
| 38 | + |
| 39 | + |
| 40 | +def get_latest_run_id(repo): |
| 41 | + result = run(["gh", "run", "list", "--repo", repo, "--workflow", "build.yml", |
| 42 | + "--limit", "1", "--json", "databaseId"]) |
| 43 | + runs = json.loads(result.stdout) |
| 44 | + if not runs: |
| 45 | + sys.exit("No completed runs found for build.yml") |
| 46 | + return str(runs[0]["databaseId"]) |
| 47 | + |
| 48 | + |
| 49 | +def list_artifacts(repo, run_id): |
| 50 | + result = run(["gh", "run", "view", run_id, "--repo", repo, "--json", "jobs"]) |
| 51 | + # list artifacts via api |
| 52 | + result = run(["gh", "api", f"repos/{repo}/actions/runs/{run_id}/artifacts", |
| 53 | + "--paginate"]) |
| 54 | + data = json.loads(result.stdout) |
| 55 | + return data.get("artifacts", []) |
| 56 | + |
| 57 | + |
| 58 | +def download_artifact(repo, artifact_id, dest_dir): |
| 59 | + run(["gh", "api", f"repos/{repo}/actions/artifacts/{artifact_id}/zip", |
| 60 | + "--header", "Accept: application/vnd.github+json", |
| 61 | + "-H", "X-GitHub-Api-Version: 2022-11-28"], |
| 62 | + **{"capture_output": False}) # won't work — need gh run download |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + parser = argparse.ArgumentParser(description=__doc__, |
| 67 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 68 | + parser.add_argument("--run-id", help="Specific GitHub Actions run ID (default: latest)") |
| 69 | + parser.add_argument("--repo", default=REPO) |
| 70 | + parser.add_argument("--dry-run", action="store_true", |
| 71 | + help="Show what would be done without modifying md5refs files") |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + # Check gh is available |
| 75 | + try: |
| 76 | + run(["gh", "auth", "status"]) |
| 77 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 78 | + sys.exit("gh CLI not found or not authenticated. Run: gh auth login") |
| 79 | + |
| 80 | + run_id = args.run_id or get_latest_run_id(args.repo) |
| 81 | + print(f"Using run ID: {run_id}") |
| 82 | + print(f"https://github.com/{args.repo}/actions/runs/{run_id}") |
| 83 | + |
| 84 | + with tempfile.TemporaryDirectory(prefix="grk_md5_") as tmpdir: |
| 85 | + tmp = Path(tmpdir) |
| 86 | + |
| 87 | + # Download all test-results artifacts for this run |
| 88 | + print("\nDownloading artifacts...") |
| 89 | + try: |
| 90 | + run(["gh", "run", "download", run_id, |
| 91 | + "--repo", args.repo, |
| 92 | + "--pattern", "test-results-*", |
| 93 | + "--dir", str(tmp)]) |
| 94 | + except subprocess.CalledProcessError as e: |
| 95 | + sys.exit(f"Failed to download artifacts:\n{e.stderr}") |
| 96 | + |
| 97 | + # Each artifact lands in tmp/<artifact-name>/ |
| 98 | + for artifact_dir in sorted(tmp.iterdir()): |
| 99 | + if not artifact_dir.is_dir(): |
| 100 | + continue |
| 101 | + name = artifact_dir.name |
| 102 | + |
| 103 | + # Parse artifact name; two historical formats: |
| 104 | + # test-results-<os>-shared_<ON|OFF> (new) |
| 105 | + # test-results-<os>-<ON|OFF> (old) |
| 106 | + prefix = "test-results-" |
| 107 | + if not name.startswith(prefix): |
| 108 | + continue |
| 109 | + rest = name[len(prefix):] # e.g. "macos-latest-shared_ON" or "macos-latest-ON" |
| 110 | + |
| 111 | + if "-shared_" in rest: |
| 112 | + os_name, shared_part = rest.rsplit("-shared_", 1) |
| 113 | + elif rest.endswith("-ON") or rest.endswith("-OFF"): |
| 114 | + shared_part = rest.rsplit("-", 1)[1] |
| 115 | + os_name = rest[: -(len(shared_part) + 1)] |
| 116 | + else: |
| 117 | + print(f" Skipping unrecognised artifact: {name}") |
| 118 | + continue |
| 119 | + shared_flag = shared_part # ON or OFF |
| 120 | + |
| 121 | + platform_key = ARTIFACT_PLATFORM_MAP.get((os_name, shared_flag)) |
| 122 | + if platform_key is None: |
| 123 | + print(f" Skipping {name} (canonical Linux refs — update md5refs.txt manually if needed)") |
| 124 | + continue |
| 125 | + |
| 126 | + log_path = artifact_dir / "Testing" / "Temporary" / "LastTest.log" |
| 127 | + if not log_path.exists(): |
| 128 | + print(f" WARNING: no LastTest.log in {name}") |
| 129 | + continue |
| 130 | + |
| 131 | + print(f"\n--- {name} -> platform key: {platform_key} ---") |
| 132 | + cmd = [sys.executable, str(SCRIPT_DIR / "update_md5refs.py"), |
| 133 | + "--platform", platform_key, str(log_path)] |
| 134 | + if args.dry_run: |
| 135 | + print(f" [dry-run] would run: {' '.join(cmd)}") |
| 136 | + else: |
| 137 | + result = subprocess.run(cmd, text=True) |
| 138 | + if result.returncode != 0: |
| 139 | + print(f" WARNING: update_md5refs.py exited {result.returncode}") |
| 140 | + |
| 141 | + if args.dry_run: |
| 142 | + print("\n[dry-run] No files were modified.") |
| 143 | + else: |
| 144 | + print("\nDone. Commit the updated md5refs-*.txt files:") |
| 145 | + for key in sorted(set(v for v in ARTIFACT_PLATFORM_MAP.values() if v)): |
| 146 | + ref = SCRIPT_DIR / f"md5refs-{key}.txt" |
| 147 | + if ref.exists(): |
| 148 | + print(f" git add {ref.relative_to(Path.cwd()) if ref.is_relative_to(Path.cwd()) else ref}") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments