99import requests
1010import subprocess
1111import sys
12+ import time
1213
1314import dotenv
1415from amaranth import *
@@ -74,6 +75,9 @@ def build_cli_parser(self, parser):
7475 submit_subparser .add_argument (
7576 "--dry-run" , help = argparse .SUPPRESS ,
7677 default = False , action = "store_true" )
78+ submit_subparser .add_argument (
79+ "--wait" , help = argparse .SUPPRESS ,
80+ default = False , action = "store_true" )
7781
7882 def run_cli (self , args ):
7983 if args .action == "submit" and not args .dry_run :
@@ -90,7 +94,7 @@ def run_cli(self, args):
9094
9195 rtlil_path = self .prepare () # always prepare before submission
9296 if args .action == "submit" :
93- self .submit (rtlil_path , dry_run = args .dry_run )
97+ self .submit (rtlil_path , dry_run = args .dry_run , wait = args . wait )
9498
9599 def prepare (self ):
96100 """Elaborate the design and convert it to RTLIL.
@@ -99,7 +103,7 @@ def prepare(self):
99103 """
100104 return self .platform .build (SiliconTop (self .config ), name = self .config_model .chipflow .project_name )
101105
102- def submit (self , rtlil_path , * , dry_run = False ):
106+ def submit (self , rtlil_path , * , dry_run = False , wait = False ):
103107 """Submit the design to the ChipFlow cloud builder.
104108 """
105109 git_head = subprocess .check_output (
@@ -174,11 +178,13 @@ def submit(self, rtlil_path, *, dry_run=False):
174178 return
175179
176180 logger .info (f"Submitting { submission_name } for project { self .project_name } " )
177- endpoint = os .environ .get ("CHIPFLOW_API_ENDPOINT " , "https://build.chipflow.org/api/builds " )
178- host = urlparse ( endpoint ). netloc
181+ chipflow_api_origin = os .environ .get ("CHIPFLOW_API_ORIGIN " , "https://build.chipflow.org" )
182+ build_submit_url = f" { chipflow_api_origin } /build/submit"
179183
180184 resp = requests .post (
181- os .environ .get ("CHIPFLOW_API_ENDPOINT" , "https://build.chipflow.org/api/builds" ),
185+ build_submit_url ,
186+ # TODO: This needs to be reworked to accept only one key, auth accepts user and pass
187+ # TODO: but we want to submit a single key
182188 auth = (os .environ ["CHIPFLOW_API_KEY_ID" ], os .environ ["CHIPFLOW_API_KEY_SECRET" ]),
183189 data = data ,
184190 files = {
@@ -197,7 +203,36 @@ def submit(self, rtlil_path, *, dry_run=False):
197203 # Handle response based on status code
198204 if resp .status_code == 200 :
199205 logger .info (f"Submitted design: { resp_data } " )
200- print (f"https://{ host } /build/{ resp_data ['build_id' ]} " )
206+ build_url = f"{ chipflow_api_origin } /build/{ resp_data ['build_id' ]} "
207+ build_status_url = f"{ chipflow_api_origin } /build/{ resp_data ['build_id' ]} /status"
208+
209+ print (f"Design submitted successfully! Build URL: { build_url } " )
210+
211+ # Poll the status API until the build is completed or failed
212+ if wait :
213+ while True :
214+ status_resp = requests .get (
215+ build_status_url ,
216+ auth = (os .environ ["CHIPFLOW_API_KEY_ID" ], os .environ ["CHIPFLOW_API_KEY_SECRET" ])
217+ )
218+ if status_resp .status_code != 200 :
219+ logger .error (f"Failed to fetch build status: { status_resp .text } " )
220+ raise ChipFlowError ("Error while checking build status." )
221+
222+ status_data = status_resp .json ()
223+ build_status = status_data .get ("status" )
224+ logger .info (f"Build status: { build_status } " )
225+
226+ if build_status == "completed" :
227+ print ("Build completed successfully!" )
228+ exit (0 )
229+ elif build_status == "failed" :
230+ print ("Build failed." )
231+ exit (1 )
232+
233+ # Wait before polling again
234+ time .sleep (10 )
235+
201236 else :
202237 # Log detailed information about the failed request
203238 logger .error (f"Request failed with status code { resp .status_code } " )
0 commit comments