|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to publish task pack JSON files to a remote URL. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import urllib.error |
| 11 | +import urllib.request |
| 12 | +from pathlib import Path |
| 13 | +from typing import List |
| 14 | + |
| 15 | + |
| 16 | +def read_json_files(directory: str) -> List[tuple]: |
| 17 | + """ |
| 18 | + Read all JSON files from the specified directory. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + List of tuples (filename, json_data) |
| 22 | + """ |
| 23 | + json_files = [] |
| 24 | + task_packs_path = Path(directory) |
| 25 | + |
| 26 | + if not task_packs_path.exists(): |
| 27 | + print(f"Error: Directory '{directory}' does not exist", file=sys.stderr) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + for json_file in task_packs_path.glob("*.json"): |
| 31 | + try: |
| 32 | + with open(json_file, "r", encoding="utf-8") as f: |
| 33 | + data = json.load(f) |
| 34 | + json_files.append((json_file.name, data)) |
| 35 | + print(f"Read: {json_file.name}") |
| 36 | + except json.JSONDecodeError as e: |
| 37 | + print(f"Error reading {json_file.name}: {e}", file=sys.stderr) |
| 38 | + except Exception as e: |
| 39 | + print(f"Error processing {json_file.name}: {e}", file=sys.stderr) |
| 40 | + |
| 41 | + return json_files |
| 42 | + |
| 43 | + |
| 44 | +def publish_task_pack( |
| 45 | + url: str, pack_data: dict, visibility: str, origin: str, auth_token: str |
| 46 | +) -> bool: |
| 47 | + """ |
| 48 | + Publish a single task pack to the remote URL. |
| 49 | +
|
| 50 | + Args: |
| 51 | + url: The target URL |
| 52 | + pack_data: The JSON data to publish |
| 53 | + visibility: Visibility setting ('public' or 'hidden') |
| 54 | + origin: Origin of the task packs |
| 55 | + auth_token: Authorization token from environment |
| 56 | +
|
| 57 | + Returns: |
| 58 | + True if successful, False otherwise |
| 59 | + """ |
| 60 | + # Prepare the payload |
| 61 | + payload = {"task_pack": pack_data, "visibility": visibility, "origin": origin} |
| 62 | + |
| 63 | + # Prepare headers |
| 64 | + headers = { |
| 65 | + "Content-Type": "application/json", |
| 66 | + } |
| 67 | + |
| 68 | + if auth_token: |
| 69 | + headers["X-AUTH-KEY"] = auth_token |
| 70 | + |
| 71 | + # Convert payload to JSON bytes |
| 72 | + data = json.dumps(payload).encode("utf-8") |
| 73 | + |
| 74 | + # Create request |
| 75 | + req = urllib.request.Request(url, data=data, headers=headers, method="POST") |
| 76 | + |
| 77 | + try: |
| 78 | + with urllib.request.urlopen(req) as response: |
| 79 | + status = response.status |
| 80 | + response_body = response.read().decode("utf-8") |
| 81 | + if status in (200, 201): |
| 82 | + return True |
| 83 | + else: |
| 84 | + print(f" Warning: Received status {status}", file=sys.stderr) |
| 85 | + print(f" Response: {response_body}", file=sys.stderr) |
| 86 | + return False |
| 87 | + except urllib.error.HTTPError as e: |
| 88 | + error_body = e.read().decode("utf-8") if e.fp else "" |
| 89 | + print(f" HTTP Error {e.code}: {e.reason}", file=sys.stderr) |
| 90 | + if error_body: |
| 91 | + print(f" Response: {error_body}", file=sys.stderr) |
| 92 | + return False |
| 93 | + except urllib.error.URLError as e: |
| 94 | + print(f" URL Error: {e.reason}", file=sys.stderr) |
| 95 | + return False |
| 96 | + except Exception as e: |
| 97 | + print(f" Error: {e}", file=sys.stderr) |
| 98 | + return False |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + parser = argparse.ArgumentParser( |
| 103 | + description="Publish task pack JSON files to a remote URL" |
| 104 | + ) |
| 105 | + parser.add_argument("url", help="Target URL to publish task packs to") |
| 106 | + parser.add_argument( |
| 107 | + "--public", |
| 108 | + action="store_true", |
| 109 | + help="Set visibility to public (default: hidden)", |
| 110 | + ) |
| 111 | + parser.add_argument( |
| 112 | + "--hidden", action="store_true", help="Set visibility to hidden (default)" |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "--origin", |
| 116 | + default="github", |
| 117 | + help="Origin of the task packs (default: github)", |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--dir", |
| 121 | + default="task_packs", |
| 122 | + help="Directory containing JSON files (default: task_packs)", |
| 123 | + ) |
| 124 | + |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + # Determine visibility |
| 128 | + if args.public and args.hidden: |
| 129 | + print("Error: Cannot specify both --public and --hidden", file=sys.stderr) |
| 130 | + sys.exit(1) |
| 131 | + |
| 132 | + visibility = "public" if args.public else "hidden" |
| 133 | + |
| 134 | + # Get auth token from environment |
| 135 | + auth_token = os.environ.get("CODEBATTLE_AUTH_TOKEN", "") |
| 136 | + if not auth_token: |
| 137 | + print( |
| 138 | + "Warning: CODEBATTLE_AUTH_TOKEN environment variable not set", |
| 139 | + file=sys.stderr, |
| 140 | + ) |
| 141 | + |
| 142 | + # Read JSON files |
| 143 | + print(f"Reading JSON files from '{args.dir}/'...") |
| 144 | + json_files = read_json_files(args.dir) |
| 145 | + |
| 146 | + if not json_files: |
| 147 | + print("No JSON files found to publish", file=sys.stderr) |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + print(f"\nPublishing {len(json_files)} task packs to {args.url}...") |
| 151 | + print(f"Visibility: {visibility}") |
| 152 | + print(f"Origin: {args.origin}") |
| 153 | + print() |
| 154 | + |
| 155 | + # Publish each task pack |
| 156 | + success_count = 0 |
| 157 | + fail_count = 0 |
| 158 | + |
| 159 | + for filename, pack_data in json_files: |
| 160 | + print(f"Publishing: {filename}... ", end="", flush=True) |
| 161 | + if publish_task_pack(args.url, pack_data, visibility, args.origin, auth_token): |
| 162 | + print("✓") |
| 163 | + success_count += 1 |
| 164 | + else: |
| 165 | + print("✗") |
| 166 | + fail_count += 1 |
| 167 | + |
| 168 | + # Summary |
| 169 | + print() |
| 170 | + print(f"Summary: {success_count} successful, {fail_count} failed") |
| 171 | + |
| 172 | + if fail_count > 0: |
| 173 | + sys.exit(1) |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + main() |
0 commit comments