55import shutil
66import subprocess
77import sys
8+ import tempfile
89from pathlib import Path
910
1011
1112OPENAPI_SPEC_URL = (
1213 "https://raw.githubusercontent.com/togethercomputer/openapi/main/openapi.yaml"
1314)
14- OUTPUT_DIR = Path (__file__ ).parent .parent / "src" / "together" / "generated"
15+ # We no longer set OUTPUT_DIR to the src folder for generation.
16+ # Instead, we'll copy the generated client to the target directory.
17+ TARGET_DIR = Path (__file__ ).parent .parent / "src" / "together" / "generated"
1518GENERATOR_JAR_URL = "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.11.0/openapi-generator-cli-7.11.0.jar"
1619GENERATOR_JAR = Path (__file__ ).parent / "openapi-generator-cli.jar"
1720
@@ -23,8 +26,7 @@ def run_command(cmd: list[str], check: bool = True) -> subprocess.CompletedProce
2326
2427
2528def download_file (url : str , target : Path ) -> None :
26- """Download a file"""
27-
29+ """Download a file."""
2830 print (f"Downloading { url } to { target } " )
2931 run_command (["wget" , "-O" , str (target ), url ])
3032
@@ -44,10 +46,10 @@ def main() -> None:
4446 args = parse_args ()
4547 spec_file = Path (__file__ ).parent / "openapi.yaml"
4648
47- # Download OpenAPI spec if not skipped
49+ # Download OpenAPI spec if not skipped.
4850 if not args .skip_spec_download :
4951 download_file (OPENAPI_SPEC_URL , spec_file )
50- # Run formatter on the spec for better merge conflict handling
52+ # Format the spec for better merge conflict handling.
5153 run_command (["npx" , "-y" , "prettier" , "--write" , str (spec_file )])
5254 elif not spec_file .exists ():
5355 print (
@@ -56,54 +58,51 @@ def main() -> None:
5658 )
5759 sys .exit (1 )
5860
59- # Download generator if needed
61+ # Download generator if needed.
6062 download_file (GENERATOR_JAR_URL , GENERATOR_JAR )
6163
62- # Delete existing generated code
63- shutil .rmtree (OUTPUT_DIR , ignore_errors = True )
64-
65- # Ensure output directory exists
66- OUTPUT_DIR .mkdir (parents = True , exist_ok = True )
67-
68- # Generate client code
69- cmd = [
70- "java" ,
71- "-jar" ,
72- str (GENERATOR_JAR ),
73- "generate" ,
74- "-i" ,
75- str (spec_file ),
76- "-g" ,
77- "python" ,
78- "-o" ,
79- str (OUTPUT_DIR ),
80- "--package-name=together.generated" ,
81- "--git-repo-id=together-python" ,
82- "--git-user-id=togethercomputer" ,
83- "--additional-properties=packageUrl=https://github.com/togethercomputer/together-python" ,
84- "--additional-properties=library=asyncio" ,
85- "--additional-properties=generateSourceCodeOnly=true" ,
86- ]
87-
88- print ("Generating client code..." )
89- result = run_command (cmd , check = False )
90-
91- if result .returncode != 0 :
92- print ("Error generating client code:" , file = sys .stderr )
93- print (result .stderr , file = sys .stderr )
94- sys .exit (1 )
95-
96- # Move files from nested directory to target directory
97- nested_dir = OUTPUT_DIR / "together" / "generated"
98- if nested_dir .exists ():
99- print ("Moving files from nested directory..." )
100- # Move all contents to parent directory
101- for item in nested_dir .iterdir ():
102- shutil .move (str (item ), str (OUTPUT_DIR / item .name ))
103- # Clean up empty directories
104- shutil .rmtree (OUTPUT_DIR / "together" , ignore_errors = True )
105-
106- print ("Successfully generated client code" )
64+ # Create a temporary directory for generation.
65+ with tempfile .TemporaryDirectory () as tmp_dir :
66+ tmp_path = Path (tmp_dir )
67+ # Build the generation command.
68+ cmd = [
69+ "java" ,
70+ "-jar" ,
71+ str (GENERATOR_JAR ),
72+ "generate" ,
73+ "-i" ,
74+ str (spec_file ),
75+ "-g" ,
76+ "python" ,
77+ "-o" ,
78+ str (tmp_path ),
79+ "--package-name=together.generated" ,
80+ "--git-repo-id=together-python" ,
81+ "--git-user-id=togethercomputer" ,
82+ "--additional-properties=packageUrl=https://github.com/togethercomputer/together-python" ,
83+ "--additional-properties=library=asyncio" ,
84+ "--additional-properties=generateSourceCodeOnly=true" ,
85+ ]
86+
87+ print ("Generating client code into temporary directory..." )
88+ result = run_command (cmd , check = False )
89+ if result .returncode != 0 :
90+ print ("Error generating client code:" , file = sys .stderr )
91+ print (result .stderr , file = sys .stderr )
92+ sys .exit (1 )
93+
94+ # The generator will create a directory structure like: tmp_dir/together/generated
95+ generated_dir = tmp_path / "together" / "generated"
96+ if not generated_dir .exists ():
97+ print ("Error: Expected generated directory not found" , file = sys .stderr )
98+ sys .exit (1 )
99+
100+ # Remove any existing generated client code.
101+ shutil .rmtree (TARGET_DIR , ignore_errors = True )
102+ TARGET_DIR .parent .mkdir (parents = True , exist_ok = True )
103+ # Copy the generated code from the temporary directory to the target directory.
104+ shutil .copytree (generated_dir , TARGET_DIR )
105+ print ("Successfully generated and copied client code to" , TARGET_DIR )
107106
108107
109108if __name__ == "__main__" :
0 commit comments