118 def _waitStdoutProc(self): |
118 def _waitStdoutProc(self): |
119 while True: |
119 while True: |
120 a = self.proc.stdout.readline() |
120 a = self.proc.stdout.readline() |
121 if len(a) == 0 or a is None: |
121 if len(a) == 0 or a is None: |
122 break |
122 break |
123 sys.stdout.write(a) |
|
124 self.ReportOutput(a) |
123 self.ReportOutput(a) |
125 self.event.set() |
124 self.event.set() |
126 if self.pattern is not None and a.find(self.pattern) >= 0: |
125 if self.pattern is not None and a.find(self.pattern) >= 0: |
127 sys.stdout.write("found pattern in '" + a +"'") |
126 sys.stdout.write("found pattern in '" + a +"'") |
128 self.success_event.set() |
127 self.success_event.set() |
170 |
169 |
171 def waitPatternInStdout(self, pattern, timeout, count=1): |
170 def waitPatternInStdout(self, pattern, timeout, count=1): |
172 found = 0 |
171 found = 0 |
173 self.pattern = pattern |
172 self.pattern = pattern |
174 end_time = timesec() + timeout |
173 end_time = timesec() + timeout |
175 self.event.clear() |
|
176 while True: |
174 while True: |
177 remain = end_time - timesec() |
175 remain = end_time - timesec() |
178 if remain <= 0 : |
176 if remain <= 0 : |
179 res = False |
177 res = False |
180 break |
178 break |
317 elapsed = "%.3fs: "%(timesec() - self.starttime) |
315 elapsed = "%.3fs: "%(timesec() - self.starttime) |
318 self.report.write("<p>" + elapsed + text + "</p>") |
316 self.report.write("<p>" + elapsed + text + "</p>") |
319 |
317 |
320 def ReportOutput(self, text): |
318 def ReportOutput(self, text): |
321 elapsed = "%.3fs: "%(timesec() - self.starttime) |
319 elapsed = "%.3fs: "%(timesec() - self.starttime) |
|
320 sys.stdout.write(elapsed + text) |
322 self.report.write("<pre>" + elapsed + text + "</pre>") |
321 self.report.write("<pre>" + elapsed + text + "</pre>") |
323 |
322 |
324 |
323 |
325 class AuxiliaryProcess: |
324 class AuxiliaryProcess(stdoutIdleObserver): |
326 def __init__(self, beremiz_app, command): |
325 def __init__(self, beremiz_app, command): |
327 self.app = beremiz_app |
326 self.app = beremiz_app |
328 self.app.ReportText("Launching process " + repr(command)) |
327 self.app.ReportText("Launching process " + repr(command)) |
329 self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=0) |
328 self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=0) |
330 self.app.ReportText("Launched process " + repr(command) + " PID: " + str(self.proc.pid)) |
329 self.app.ReportText("Launched process " + repr(command) + " PID: " + str(self.proc.pid)) |
331 if self.proc is not None: |
330 stdoutIdleObserver.__init__(self) |
332 self.thread = Thread(target = self._waitStdoutProc).start() |
|
333 |
|
334 def _waitStdoutProc(self): |
|
335 while True: |
|
336 a = self.proc.stdout.readline() |
|
337 if len(a) == 0 or a is None: |
|
338 break |
|
339 a = "aux: "+a |
|
340 sys.stdout.write(a) |
|
341 self.ReportOutput(a) |
|
342 self.ReportOutput("AuxStdoutFinish") |
|
343 |
331 |
344 def close(self): |
332 def close(self): |
345 if self.proc is not None: |
333 if self.proc is not None: |
346 proc = self.proc |
334 proc = self.proc |
347 self.proc = None |
335 self.proc = None |
352 except OSError: |
340 except OSError: |
353 pass |
341 pass |
354 proc.wait() |
342 proc.wait() |
355 # self.thread.join() |
343 # self.thread.join() |
356 |
344 |
|
345 def ReportOutput(self, text): |
|
346 self.app.ReportOutput("Aux: "+text) |
|
347 |
|
348 def ReportScreenShot(self, msg): |
|
349 self.app.ReportOutput("Aux: "+msg) |
|
350 |
357 def __del__(self): |
351 def __del__(self): |
358 self.close() |
352 self.close() |
359 |
353 |
360 def run_test(func, *args, **kwargs): |
354 def run_test(func, *args, **kwargs): |
361 app = BeremizApp(*args, **kwargs) |
355 app = BeremizApp(*args, **kwargs) |
365 # sadly, sys.excepthook is broken in sikuli/jython |
359 # sadly, sys.excepthook is broken in sikuli/jython |
366 # purpose of this run_test function is to work around it. |
360 # purpose of this run_test function is to work around it. |
367 # and catch exception cleanly anyhow |
361 # and catch exception cleanly anyhow |
368 e_type, e_value, e_traceback = sys.exc_info() |
362 e_type, e_value, e_traceback = sys.exc_info() |
369 err_msg = "\n".join(traceback.format_exception(e_type, e_value, e_traceback)) |
363 err_msg = "\n".join(traceback.format_exception(e_type, e_value, e_traceback)) |
370 sys.stdout.write(err_msg) |
|
371 app.ReportOutput(err_msg) |
364 app.ReportOutput(err_msg) |
372 success = False |
365 success = False |
373 |
366 |
374 app.close() |
367 app.close() |
375 |
368 |