11#!/usr/bin/env python3
22from __future__ import annotations
33
4+ import shutil
45import subprocess
56import sys
67from pathlib import Path
78
8- import requests
99
10-
11- OPENAPI_SPEC_URL = (
12- "https://raw.githubusercontent.com/togethercomputer/openapi/refs/heads/main/openapi.yaml"
13- )
10+ OPENAPI_SPEC_URL = "https://raw.githubusercontent.com/togethercomputer/openapi/main/openapi.yaml"
1411OUTPUT_DIR = Path (__file__ ).parent .parent / "src" / "together" / "generated"
15- GENERATOR_JAR_URL = "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.3 .0/openapi-generator-cli-7.3 .0.jar"
12+ GENERATOR_JAR_URL = "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.11 .0/openapi-generator-cli-7.11 .0.jar"
1613GENERATOR_JAR = Path (__file__ ).parent / "openapi-generator-cli.jar"
1714
1815
16+ def run_command (cmd : list [str ], check : bool = True ) -> subprocess .CompletedProcess :
17+ """Run a command and optionally check its return code."""
18+ print (f"Running: { ' ' .join (cmd )} " )
19+ return subprocess .run (cmd , check = check , capture_output = True , text = True )
20+
21+
1922def download_file (url : str , target : Path ) -> None :
20- """Download a file if it doesn't exist."""
21- if target .exists ():
22- return
23+ """Download a file"""
2324
2425 print (f"Downloading { url } to { target } " )
25- response = requests .get (url , stream = True )
26- response .raise_for_status ()
27-
28- target .parent .mkdir (parents = True , exist_ok = True )
29- with open (target , "wb" ) as f :
30- for chunk in response .iter_content (chunk_size = 8192 ):
31- f .write (chunk )
26+ run_command (["wget" , "-O" , str (target ), url ])
3227
3328
3429def main () -> None :
@@ -39,6 +34,9 @@ def main() -> None:
3934 # Download generator if needed
4035 download_file (GENERATOR_JAR_URL , GENERATOR_JAR )
4136
37+ # Delete existing generated code
38+ shutil .rmtree (OUTPUT_DIR , ignore_errors = True )
39+
4240 # Ensure output directory exists
4341 OUTPUT_DIR .mkdir (parents = True , exist_ok = True )
4442
@@ -54,19 +52,32 @@ def main() -> None:
5452 "python" ,
5553 "-o" ,
5654 str (OUTPUT_DIR ),
57- "--additional-properties=packageName =together.generated" ,
55+ "--package-name =together.generated" ,
5856 "--git-repo-id=together-python" ,
5957 "--git-user-id=togethercomputer" ,
58+ "--additional-properties=packageUrl=https://github.com/togethercomputer/together-python" ,
59+ "--additional-properties=library=asyncio" ,
60+ "--additional-properties=generateSourceCodeOnly=true" ,
6061 ]
6162
6263 print ("Generating client code..." )
63- result = subprocess . run (cmd , capture_output = True , text = True )
64+ result = run_command (cmd , check = False )
6465
6566 if result .returncode != 0 :
6667 print ("Error generating client code:" , file = sys .stderr )
6768 print (result .stderr , file = sys .stderr )
6869 sys .exit (1 )
6970
71+ # Move files from nested directory to target directory
72+ nested_dir = OUTPUT_DIR / "together" / "generated"
73+ if nested_dir .exists ():
74+ print ("Moving files from nested directory..." )
75+ # Move all contents to parent directory
76+ for item in nested_dir .iterdir ():
77+ shutil .move (str (item ), str (OUTPUT_DIR / item .name ))
78+ # Clean up empty directories
79+ shutil .rmtree (OUTPUT_DIR / "together" , ignore_errors = True )
80+
7081 print ("Successfully generated client code" )
7182
7283
0 commit comments