executes 'cmd' as a child process and returns the child's output, the duration of execution, and the process exit status. Aborts if child process does not generate output for 'timeLimit' seconds.
(cmd, timeLimit=20)
| 24 | import sys |
| 25 | |
| 26 | def getProcessResults(cmd, timeLimit=20): |
| 27 | ''' |
| 28 | executes 'cmd' as a child process and returns the child's output, |
| 29 | the duration of execution, and the process exit status. Aborts if |
| 30 | child process does not generate output for 'timeLimit' seconds. |
| 31 | ''' |
| 32 | output = "" |
| 33 | startTime = time.time() |
| 34 | child = pexpect.spawn(cmd, timeout=10) |
| 35 | child.logfile = sys.stdout |
| 36 | |
| 37 | while 1: |
| 38 | try: |
| 39 | # read_nonblocking will add to 'outout' one byte at a time |
| 40 | # newlines can show up as '\r\n' so we kill any '\r's which |
| 41 | # will mess up the formatting for the viewer |
| 42 | output += child.read_nonblocking(timeout=timeLimit).replace("\r","") |
| 43 | except pexpect.EOF as e: |
| 44 | print(str(e)) |
| 45 | # process terminated normally |
| 46 | break |
| 47 | except pexpect.TIMEOUT as e: |
| 48 | print(str(e)) |
| 49 | output += "\nProcess aborted by FlashTest after %s seconds.\n" % timeLimit |
| 50 | print(child.isalive()) |
| 51 | child.kill(9) |
| 52 | break |
| 53 | |
| 54 | endTime = time.time() |
| 55 | child.close(force=True) |
| 56 | |
| 57 | duration = endTime - startTime |
| 58 | exitStatus = child.exitstatus |
| 59 | |
| 60 | return (output, duration, exitStatus) |
| 61 | |
| 62 | cmd = "./ticker.py" |
| 63 |
no test coverage detected
searching dependent graphs…