88import requests
99import subprocess
1010import time
11+ import sys
1112
1213import dotenv
1314from amaranth import *
@@ -179,12 +180,15 @@ def submit(self, rtlil_path, *, dry_run=False, wait=False):
179180 logger .info (f"Submitted design: { resp_data } " )
180181 build_url = f"{ chipflow_api_origin } /build/{ resp_data ['build_id' ]} "
181182 build_status_url = f"{ chipflow_api_origin } /build/{ resp_data ['build_id' ]} /status"
183+ log_stream_url = f"{ chipflow_api_origin } /build/{ resp_data ['build_id' ]} /logs?follow=true"
182184
183185 print (f"Design submitted successfully! Build URL: { build_url } " )
184186
185187 # Poll the status API until the build is completed or failed
188+ stream_event_counter = 0
186189 if wait :
187190 while True :
191+ logger .info ("Polling build status..." )
188192 status_resp = requests .get (
189193 build_status_url ,
190194 auth = (os .environ ["CHIPFLOW_API_KEY_ID" ], os .environ ["CHIPFLOW_API_KEY_SECRET" ])
@@ -203,10 +207,32 @@ def submit(self, rtlil_path, *, dry_run=False, wait=False):
203207 elif build_status == "failed" :
204208 print ("Build failed." )
205209 exit (1 )
206-
207- # Wait before polling again
208- time .sleep (10 )
209-
210+ elif build_status == "running" :
211+ print ("Build running." )
212+ # Wait before polling again
213+ # time.sleep(10)
214+ # Attempt to stream logs rather than time.sleep
215+ try :
216+ if stream_event_counter > 1 :
217+ logger .warning ("Log streaming may have been interrupted. Some logs may be missing." )
218+ logger .warning (f"Check { build_url } " )
219+ stream_event_counter += 1
220+ with requests .get (
221+ log_stream_url ,
222+ auth = (os .environ ["CHIPFLOW_API_KEY_ID" ], os .environ ["CHIPFLOW_API_KEY_SECRET" ]),
223+ stream = True
224+ ) as log_resp :
225+ if log_resp .status_code == 200 :
226+ for line in log_resp .iter_lines ():
227+ if line :
228+ print (line .decode ("utf-8" )) # Print logs in real-time
229+ sys .stdout .flush ()
230+ else :
231+ logger .warning (f"Failed to stream logs: { log_resp .text } " )
232+ except requests .RequestException as e :
233+ logger .error (f"Error while streaming logs: { e } " )
234+ pass
235+ time .sleep (10 ) # Wait before polling again
210236 else :
211237 # Log detailed information about the failed request
212238 logger .error (f"Request failed with status code { resp .status_code } " )
0 commit comments