@@ -190,122 +190,6 @@ def build_bsp_attachconfig(bsp, attach_file):
190190
191191 return res
192192
193- # 配置日志
194- logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(levelname)s: %(message)s' )
195- logger = logging .getLogger (__name__ )
196-
197- class QemuManager :
198- def __init__ (self , qemu_cmd , idle_timeout = 5 , checkresult = None ):
199- """
200- 初始化QEMU管理器
201- :param qemu_cmd: QEMU启动命令
202- :param idle_timeout: 日志空闲超时时间(秒)
203- """
204- self .qemu_cmd = qemu_cmd
205- self .idle_timeout = idle_timeout
206- self .qemu_process = None
207- self .log_thread = None
208- self .checkresult = checkresult
209- self .last_log_time = time .time ()
210- self .logs = []
211- self .running = False
212- self .checkresult_found = False # 标记是否找到checkresult
213- def start_qemu (self ):
214- """启动QEMU进程"""
215- logger .info ("Starting QEMU..." )
216- self .qemu_process = subprocess .Popen (
217- self .qemu_cmd ,
218- stdin = subprocess .PIPE ,
219- stdout = subprocess .PIPE ,
220- stderr = subprocess .PIPE ,
221- shell = True ,
222- bufsize = 0 ,
223- text = True
224- )
225- self .running = True
226- logger .info ("QEMU started successfully." )
227-
228- def log_monitor (self ):
229- """监控QEMU输出日志"""
230- logger .info ("Starting log monitor..." )
231- while self .running :
232- line = self .qemu_process .stdout .readline ()
233- if line :
234- line = line .strip ()
235- self .logs .append (line )
236- self .last_log_time = time .time () # 更新最后日志时间
237- logger .info (f"QEMU Output: { line } " ) # 实时打印日志
238- # 检查是否包含checkresult
239- if self .checkresult and self .checkresult in line :
240- logger .info (f"Checkresult '{ self .checkresult } ' found in logs. Success..." )
241- self .checkresult_found = True
242- #self.running = False # 停止监控
243- #break
244- else :
245- time .sleep (0.1 )
246-
247- def send_command (self , command ):
248- """向QEMU发送命令"""
249- if not self .running or not self .qemu_process :
250- logger .error ("QEMU is not running." )
251- return False
252-
253- logger .info (f"Sending command: { command } " )
254- try :
255- self .qemu_process .stdin .write (command + "\n " )
256- self .qemu_process .stdin .flush ()
257- return True
258- except Exception as e :
259- logger .error (f"Failed to send command: { e } " )
260- return False
261-
262- def stop_qemu (self ):
263- """停止QEMU进程"""
264- if self .qemu_process :
265- logger .info ("Stopping QEMU..." )
266- self .running = False
267- self .qemu_process .terminate ()
268- self .qemu_process .wait (timeout = 5 )
269- logger .info ("QEMU stopped." )
270-
271-
272-
273- def run (self ,commands ):
274- """主运行逻辑"""
275- try :
276- # 启动QEMU
277- self .start_qemu ()
278-
279- # 启动日志监控线程
280- self .log_thread = threading .Thread (target = self .log_monitor , daemon = True )
281- self .log_thread .start ()
282-
283- # 等待QEMU启动完成
284- time .sleep (5 )
285-
286- for cmd in commands :
287- if not self .send_command (cmd ):
288- break
289- time .sleep (2 ) # 命令之间间隔2秒
290-
291- # 监控日志输出,超时退出
292- while self .running :
293- idle_time = time .time () - self .last_log_time
294- if idle_time > self .idle_timeout :
295- if not self .checkresult_found :
296- logger .info (f"No logs for { self .idle_timeout } seconds. ::error:: Exiting..." )
297- else :
298- logger .info (f"No logs for { self .idle_timeout } seconds. ::check success:: Exiting..." )
299- break
300- time .sleep (0.1 )
301-
302- except KeyboardInterrupt :
303- logger .info ("Script interrupted by user." )
304- except Exception as e :
305- logger .error (f"An error occurred: { e } " )
306- finally :
307- self .stop_qemu ()
308-
309193def check_output (output , check_string ):
310194 """检查输出中是否包含指定字符串"""
311195 output_str = '' .join (output ) if isinstance (output , list ) else str (output )
0 commit comments