|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import urllib.request |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | + |
| 10 | +def parse_nightly_version(nightly_version): |
| 11 | + """ |
| 12 | + Parse NIGHTLY_VERSION (e.g., 'dev20251004') to date string (e.g., '2025-10-04'). |
| 13 | +
|
| 14 | + Args: |
| 15 | + nightly_version: String in format 'devYYYYMMDD' |
| 16 | +
|
| 17 | + Returns: |
| 18 | + Date string in format 'YYYY-MM-DD' |
| 19 | + """ |
| 20 | + match = re.match(r"dev(\d{4})(\d{2})(\d{2})", nightly_version) |
| 21 | + if not match: |
| 22 | + raise ValueError(f"Invalid NIGHTLY_VERSION format: {nightly_version}") |
| 23 | + |
| 24 | + year, month, day = match.groups() |
| 25 | + return f"{year}-{month}-{day}" |
| 26 | + |
| 27 | + |
| 28 | +def get_torch_nightly_version(): |
| 29 | + """ |
| 30 | + Read NIGHTLY_VERSION from torch_pin.py. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + NIGHTLY_VERSION string |
| 34 | + """ |
| 35 | + with open("torch_pin.py", "r") as f: |
| 36 | + content = f.read() |
| 37 | + |
| 38 | + match = re.search(r'NIGHTLY_VERSION\s*=\s*["\']([^"\']+)["\']', content) |
| 39 | + if not match: |
| 40 | + raise ValueError("Could not find NIGHTLY_VERSION in torch_pin.py") |
| 41 | + |
| 42 | + return match.group(1) |
| 43 | + |
| 44 | + |
| 45 | +def get_commit_hash_for_nightly(date_str): |
| 46 | + """ |
| 47 | + Fetch commit hash from PyTorch nightly branch for a given date. |
| 48 | +
|
| 49 | + Args: |
| 50 | + date_str: Date string in format 'YYYY-MM-DD' |
| 51 | +
|
| 52 | + Returns: |
| 53 | + Commit hash string |
| 54 | + """ |
| 55 | + api_url = "https://api.github.com/repos/pytorch/pytorch/commits" |
| 56 | + params = f"?sha=nightly&per_page=100" |
| 57 | + url = api_url + params |
| 58 | + |
| 59 | + req = urllib.request.Request(url) |
| 60 | + req.add_header("Accept", "application/vnd.github.v3+json") |
| 61 | + req.add_header("User-Agent", "ExecuTorch-Bot") |
| 62 | + |
| 63 | + try: |
| 64 | + with urllib.request.urlopen(req) as response: |
| 65 | + commits = json.loads(response.read().decode()) |
| 66 | + except Exception as e: |
| 67 | + print(f"Error fetching commits: {e}", file=sys.stderr) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + # Look for commit with title matching "{date_str} nightly release" |
| 71 | + target_title = f"{date_str} nightly release" |
| 72 | + |
| 73 | + for commit in commits: |
| 74 | + commit_msg = commit.get("commit", {}).get("message", "") |
| 75 | + # Check if the first line of commit message matches |
| 76 | + first_line = commit_msg.split("\n")[0].strip() |
| 77 | + if first_line == target_title or first_line.startswith(f"{date_str} nightly"): |
| 78 | + return commit["sha"] |
| 79 | + |
| 80 | + raise ValueError( |
| 81 | + f"Could not find commit with title matching '{target_title}' in nightly branch" |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def update_pytorch_pin(commit_hash): |
| 86 | + """ |
| 87 | + Update .ci/docker/ci_commit_pins/pytorch.txt with the new commit hash. |
| 88 | +
|
| 89 | + Args: |
| 90 | + commit_hash: Commit hash to write |
| 91 | + """ |
| 92 | + pin_file = ".ci/docker/ci_commit_pins/pytorch.txt" |
| 93 | + with open(pin_file, "w") as f: |
| 94 | + f.write(f"{commit_hash}\n") |
| 95 | + print(f"Updated {pin_file} with commit hash: {commit_hash}") |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + try: |
| 100 | + # Read NIGHTLY_VERSION from torch_pin.py |
| 101 | + nightly_version = get_torch_nightly_version() |
| 102 | + print(f"Found NIGHTLY_VERSION: {nightly_version}") |
| 103 | + |
| 104 | + # Parse to date string |
| 105 | + date_str = parse_nightly_version(nightly_version) |
| 106 | + print(f"Parsed date: {date_str}") |
| 107 | + |
| 108 | + # Fetch commit hash from PyTorch nightly branch |
| 109 | + commit_hash = get_commit_hash_for_nightly(date_str) |
| 110 | + print(f"Found commit hash: {commit_hash}") |
| 111 | + |
| 112 | + # Update the pin file |
| 113 | + update_pytorch_pin(commit_hash) |
| 114 | + |
| 115 | + print("Successfully updated PyTorch commit pin!") |
| 116 | + |
| 117 | + except Exception as e: |
| 118 | + print(f"Error: {e}", file=sys.stderr) |
| 119 | + sys.exit(1) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments