|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Migrate SWEAP images from jefzda/sweap-images to ghcr.io/sg-evals/sweap-images. |
| 3 | +
|
| 4 | +Two modes: |
| 5 | + --push Pull from jefzda, retag, push to GHCR, clean up (requires docker login to ghcr.io) |
| 6 | + --update Update Dockerfile FROM lines to use ghcr.io (no Docker required) |
| 7 | + --dry-run Show what would be done without making changes |
| 8 | +
|
| 9 | +Usage: |
| 10 | + # First, push images (requires GHCR write access): |
| 11 | + docker login ghcr.io -u USERNAME -p TOKEN |
| 12 | + python3 scripts/migrate_sweap_to_ghcr.py --push |
| 13 | +
|
| 14 | + # Then, update Dockerfiles: |
| 15 | + python3 scripts/migrate_sweap_to_ghcr.py --update |
| 16 | +
|
| 17 | + # Or do both: |
| 18 | + python3 scripts/migrate_sweap_to_ghcr.py --push --update |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import glob |
| 23 | +import os |
| 24 | +import re |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | + |
| 28 | +SRC_REGISTRY = "jefzda/sweap-images" |
| 29 | +DST_REGISTRY = "ghcr.io/sg-evals/sweap-images" |
| 30 | + |
| 31 | +def find_sweap_references(): |
| 32 | + """Find all Dockerfiles referencing jefzda/sweap-images and extract tags.""" |
| 33 | + tag_to_files = {} |
| 34 | + for f in sorted(glob.glob("benchmarks/csb_*/*/environment/Dockerfile*")): |
| 35 | + with open(f) as fh: |
| 36 | + for line in fh: |
| 37 | + m = re.match(r"FROM\s+(jefzda/sweap-images:(\S+))", line) |
| 38 | + if m: |
| 39 | + full_ref = m.group(1) |
| 40 | + tag = m.group(2) |
| 41 | + tag_to_files.setdefault(tag, []).append(f) |
| 42 | + return tag_to_files |
| 43 | + |
| 44 | + |
| 45 | +def push_images(tag_to_files, dry_run=False): |
| 46 | + """Pull from jefzda, retag to GHCR, push, clean up. One at a time (disk-safe).""" |
| 47 | + tags = sorted(tag_to_files.keys()) |
| 48 | + print(f"Migrating {len(tags)} images to {DST_REGISTRY}...\n") |
| 49 | + |
| 50 | + failed = [] |
| 51 | + for i, tag in enumerate(tags, 1): |
| 52 | + src = f"{SRC_REGISTRY}:{tag}" |
| 53 | + dst = f"{DST_REGISTRY}:{tag}" |
| 54 | + print(f"[{i}/{len(tags)}] {tag[:60]}...") |
| 55 | + |
| 56 | + if dry_run: |
| 57 | + print(f" DRY RUN: would pull {src}, tag as {dst}, push, clean\n") |
| 58 | + continue |
| 59 | + |
| 60 | + try: |
| 61 | + subprocess.run(["docker", "pull", src], check=True, capture_output=True, text=True) |
| 62 | + subprocess.run(["docker", "tag", src, dst], check=True, capture_output=True, text=True) |
| 63 | + subprocess.run(["docker", "push", dst], check=True, capture_output=True, text=True) |
| 64 | + # Clean up both to save disk |
| 65 | + subprocess.run(["docker", "rmi", src, dst], capture_output=True, text=True) |
| 66 | + print(f" OK\n") |
| 67 | + except subprocess.CalledProcessError as e: |
| 68 | + print(f" FAILED: {e.stderr.strip()}\n") |
| 69 | + failed.append(tag) |
| 70 | + |
| 71 | + if failed: |
| 72 | + print(f"\n{len(failed)} images failed to migrate:") |
| 73 | + for t in failed: |
| 74 | + print(f" {t}") |
| 75 | + return False |
| 76 | + return True |
| 77 | + |
| 78 | + |
| 79 | +def update_dockerfiles(tag_to_files, dry_run=False): |
| 80 | + """Replace jefzda/sweap-images with ghcr.io/sg-evals/sweap-images in all Dockerfiles.""" |
| 81 | + total_files = 0 |
| 82 | + for tag, files in sorted(tag_to_files.items()): |
| 83 | + for f in files: |
| 84 | + with open(f) as fh: |
| 85 | + content = fh.read() |
| 86 | + new_content = content.replace(SRC_REGISTRY, DST_REGISTRY) |
| 87 | + if new_content != content: |
| 88 | + if dry_run: |
| 89 | + print(f" DRY RUN: would update {f}") |
| 90 | + else: |
| 91 | + with open(f, "w") as fh: |
| 92 | + fh.write(new_content) |
| 93 | + total_files += 1 |
| 94 | + |
| 95 | + action = "Would update" if dry_run else "Updated" |
| 96 | + print(f"\n{action} {total_files} Dockerfiles ({SRC_REGISTRY} → {DST_REGISTRY})") |
| 97 | + return total_files |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 102 | + parser.add_argument("--push", action="store_true", help="Pull/retag/push images to GHCR") |
| 103 | + parser.add_argument("--update", action="store_true", help="Update Dockerfile FROM lines") |
| 104 | + parser.add_argument("--dry-run", action="store_true", help="Show what would be done") |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + if not args.push and not args.update: |
| 108 | + parser.print_help() |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 112 | + tag_to_files = find_sweap_references() |
| 113 | + |
| 114 | + if not tag_to_files: |
| 115 | + print("No jefzda/sweap-images references found. Migration may already be complete.") |
| 116 | + sys.exit(0) |
| 117 | + |
| 118 | + print(f"Found {len(tag_to_files)} unique tags across {sum(len(v) for v in tag_to_files.values())} Dockerfiles\n") |
| 119 | + |
| 120 | + if args.push: |
| 121 | + ok = push_images(tag_to_files, dry_run=args.dry_run) |
| 122 | + if not ok and not args.dry_run: |
| 123 | + print("\nSome pushes failed. Fix and rerun with --push before --update.") |
| 124 | + sys.exit(1) |
| 125 | + |
| 126 | + if args.update: |
| 127 | + update_dockerfiles(tag_to_files, dry_run=args.dry_run) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments