|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import requests |
| 6 | +import git |
| 7 | +import re |
| 8 | +import argparse |
| 9 | + |
| 10 | +def open_file(file_path): |
| 11 | + with open(file_path, 'r') as file: |
| 12 | + data = file.read() |
| 13 | + return data |
| 14 | + |
| 15 | +def write_file(file_path, data): |
| 16 | + with open(file_path, 'w') as file: |
| 17 | + file.write(data) |
| 18 | + |
| 19 | +def create_compiler_explorer_shortlink(file_path, COMPILER_ID, COMPILER_FLAGS): |
| 20 | + source_code = open_file(file_path) |
| 21 | + # Step 1: Prepare the JSON payload |
| 22 | + payload = { |
| 23 | + "sessions": [ |
| 24 | + { |
| 25 | + "id": 1, |
| 26 | + "language": "cuda", |
| 27 | + "source": source_code, |
| 28 | + "compilers": [ |
| 29 | + { |
| 30 | + "id": COMPILER_ID, |
| 31 | + "options": COMPILER_FLAGS, |
| 32 | + "libs": [ |
| 33 | + { |
| 34 | + "id": "cccl", |
| 35 | + "version": "trunk" |
| 36 | + }, |
| 37 | + { |
| 38 | + "id": "cuco", |
| 39 | + "version": "dev" |
| 40 | + } |
| 41 | + ] |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | + |
| 48 | + # Step 2: Send a POST request to the /api/shortener endpoint |
| 49 | + url = 'https://godbolt.org/api/shortener' |
| 50 | + headers = {'Content-Type': 'application/json'} |
| 51 | + |
| 52 | + response = requests.post(url, headers=headers, data=json.dumps(payload)) |
| 53 | + |
| 54 | + # Step 3: Parse the response |
| 55 | + if response.status_code == 200: |
| 56 | + data = response.json() |
| 57 | + short_url = data.get('url') |
| 58 | + if short_url: |
| 59 | + return short_url |
| 60 | + else: |
| 61 | + print("Error: Short URL not found in response.") |
| 62 | + return None |
| 63 | + else: |
| 64 | + print(f"Error: Request failed with status code {response.status_code}") |
| 65 | + print(f"Response: {response.text}") |
| 66 | + return None |
| 67 | + |
| 68 | +def update_readme_with_shortlinks(readme_path, shortlink_table): |
| 69 | + # Open and read the README.md file |
| 70 | + readme_content = open_file(readme_path) |
| 71 | + lines = readme_content.splitlines() |
| 72 | + |
| 73 | + updated_lines = [] |
| 74 | + # Iterate through each line of the README |
| 75 | + for line in lines: |
| 76 | + updated_line = line |
| 77 | + # Check each file in the shortlink_table |
| 78 | + for file_name, shortlink in shortlink_table.items(): |
| 79 | + # Check if the file name exists in the line |
| 80 | + if file_name in line: |
| 81 | + # Look for the specific godbolt link format and replace the URL |
| 82 | + updated_line = re.sub( |
| 83 | + r"\(see \[live example in godbolt\]\(https?://[^\)]+\)\)", |
| 84 | + f"(see [live example in godbolt]({shortlink}))", |
| 85 | + line |
| 86 | + ) |
| 87 | + updated_lines.append(updated_line) |
| 88 | + |
| 89 | + # Join the lines and write the updated content back to README.md |
| 90 | + updated_content = "\n".join(updated_lines) |
| 91 | + write_file(readme_path, updated_content) |
| 92 | + |
| 93 | +COMPILER_ID = 'nvcc125u1' # Desired compiler ID |
| 94 | +COMPILER_FLAGS = '-std=c++17 -arch=sm_70 --expt-extended-lambda' # NVCC compiler flags |
| 95 | +EXAMPLES_DIR = "examples" # Path to the examples directory relative to the repo root |
| 96 | + |
| 97 | +def main(): |
| 98 | + # Parse command-line arguments |
| 99 | + parser = argparse.ArgumentParser(description="Generate shortlinks for example CUDA files.") |
| 100 | + parser.add_argument('--force', action='store_true', help="Generate new links for all example files, even if unchanged.") |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + # Initialize the Git repository |
| 104 | + repo = git.Repo(search_parent_directories=True) |
| 105 | + repo_root = repo.git.rev_parse("--show-toplevel") |
| 106 | + |
| 107 | + # Ensure we are in the ci directory |
| 108 | + ci_dir = os.path.join(repo_root, 'ci') |
| 109 | + os.chdir(ci_dir) |
| 110 | + |
| 111 | + # Check if the remote repository already exists |
| 112 | + try: |
| 113 | + remote = repo.remote("gold_cuco") |
| 114 | + except git.exc.NoSuchRemoteError: |
| 115 | + # Create the remote repository if it doesn't exist |
| 116 | + remote = repo.create_remote("gold_cuco", "https://github.com/NVIDIA/cuCollections.git") |
| 117 | + print("Remote 'gold_cuco' created.") |
| 118 | + |
| 119 | + # Fetch the remote repository |
| 120 | + print("Fetching latest changes from the remote 'gold_cuco'...") |
| 121 | + remote.fetch() |
| 122 | + |
| 123 | + # Get the current branch |
| 124 | + current_branch = repo.active_branch |
| 125 | + |
| 126 | + # Get the 'dev' branch from the remote |
| 127 | + dev_branch = remote.refs.dev |
| 128 | + |
| 129 | + # Initialize a hash table (dictionary) to store file shortlinks |
| 130 | + shortlink_table = {} |
| 131 | + |
| 132 | + # Determine which files to process based on the --force flag |
| 133 | + if args.force: |
| 134 | + # Get all .cu and .cuh files in the examples directory |
| 135 | + example_files = [] |
| 136 | + for root, _, files in os.walk(os.path.join(repo_root, EXAMPLES_DIR)): |
| 137 | + for file_name in files: |
| 138 | + if file_name.endswith('.cu') or file_name.endswith('.cuh'): |
| 139 | + example_files.append(os.path.relpath(os.path.join(root, file_name), repo_root)) |
| 140 | + else: |
| 141 | + # Get the list of changed files between the current branch and the 'dev' branch |
| 142 | + changed_files = current_branch.commit.diff(dev_branch.commit) |
| 143 | + example_files = [diff.b_path for diff in changed_files if diff.b_path.startswith(EXAMPLES_DIR) and (diff.b_path.endswith('.cu') or diff.b_path.endswith('.cuh'))] |
| 144 | + |
| 145 | + # Iterate through the example files and create short links |
| 146 | + for file_path in example_files: |
| 147 | + full_file_path = os.path.join(repo_root, file_path) |
| 148 | + shortlink = create_compiler_explorer_shortlink(full_file_path, COMPILER_ID, COMPILER_FLAGS) |
| 149 | + if shortlink: |
| 150 | + shortlink_table[file_path] = shortlink # Store the file and its shortlink in the hash table |
| 151 | + print(f"File: {file_path}: Shortlink: {shortlink}") |
| 152 | + |
| 153 | + # Update README.md with the shortlinks |
| 154 | + readme_path = os.path.join(repo_root, "README.md") |
| 155 | + update_readme_with_shortlinks(readme_path, shortlink_table) |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
| 159 | + |
0 commit comments