|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import requests |
| 6 | +import subprocess |
| 7 | + |
| 8 | +def open_file(file_path): |
| 9 | + with open(file_path, 'r') as file: |
| 10 | + data = file.read() |
| 11 | + return data |
| 12 | + |
| 13 | +def create_compiler_explorer_shortlink(file_path, compiler_id, compiler_flags): |
| 14 | + source_code = open_file(file_path) |
| 15 | + # Step 1: Prepare the JSON payload |
| 16 | + payload = { |
| 17 | + "sessions": [ |
| 18 | + { |
| 19 | + "id": 1, |
| 20 | + "language": "cuda", |
| 21 | + "source": source_code, |
| 22 | + "compilers": [ |
| 23 | + { |
| 24 | + "id": compiler_id, |
| 25 | + "options": compiler_flags, |
| 26 | + "libs": [ |
| 27 | + { |
| 28 | + "id": "cccl", |
| 29 | + "version": "trunk" |
| 30 | + }, |
| 31 | + { |
| 32 | + "id": "cuco", |
| 33 | + "version": "dev" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + |
| 42 | + # Step 2: Send a POST request to the /api/shortener endpoint |
| 43 | + url = 'https://godbolt.org/api/shortener' |
| 44 | + headers = {'Content-Type': 'application/json'} |
| 45 | + |
| 46 | + response = requests.post(url, headers=headers, data=json.dumps(payload)) |
| 47 | + |
| 48 | + # Step 3: Parse the response |
| 49 | + if response.status_code == 200: |
| 50 | + data = response.json() |
| 51 | + short_url = data.get('url') |
| 52 | + if short_url: |
| 53 | + return short_url |
| 54 | + else: |
| 55 | + print("Error: Short URL not found in response.") |
| 56 | + return None |
| 57 | + else: |
| 58 | + print(f"Error: Request failed with status code {response.status_code}") |
| 59 | + print(f"Response: {response.text}") |
| 60 | + return None |
| 61 | + |
| 62 | +compiler_id = 'nvcc125u1' # Replace with the desired compiler ID |
| 63 | +compiler_flags = '-std=c++17 -arch=sm_70 --expt-extended-lambda' # Replace with your compiler flags |
| 64 | +EXAMPLES_DIR = "examples" |
| 65 | + |
| 66 | +def get_changed_cuda_files(): |
| 67 | + # Run the git command to get the changed files |
| 68 | + result = subprocess.run( |
| 69 | + ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], |
| 70 | + capture_output=True, |
| 71 | + text=True |
| 72 | + ) |
| 73 | + |
| 74 | + # Check if the command was successful |
| 75 | + if result.returncode != 0: |
| 76 | + print("Error executing git command:", result.stderr) |
| 77 | + return [] |
| 78 | + |
| 79 | + # Get the output and filter for .cu files in EXAMPLES_DIR |
| 80 | + return result.stdout.splitlines() |
| 81 | + |
| 82 | +def main(): |
| 83 | + changed_files = get_changed_cuda_files() |
| 84 | + |
| 85 | + for file in changed_files: |
| 86 | + shortlink = create_compiler_explorer_shortlink(file, compiler_id, compiler_flags) |
| 87 | + print(f"File: {file}: Shortlink: {shortlink}") |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments