OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import sys |
| 4 import json |
| 5 import subprocess |
| 6 import time |
| 7 import threading |
| 8 |
| 9 def run_command(name, executable, arguments, timeout_in_seconds): |
| 10 print "Running %s: '%s'" % (name, [executable] + arguments) |
| 11 |
| 12 # The timeout_handler will set this to True if the command times out. |
| 13 timeout_value = {'did_timeout' : False} |
| 14 |
| 15 start = time.time() |
| 16 |
| 17 process = subprocess.Popen([executable] + arguments, |
| 18 stdout=subprocess.PIPE, |
| 19 stderr=subprocess.PIPE) |
| 20 def timeout_handler(): |
| 21 timeout_value['did_timeout'] = True |
| 22 process.kill() |
| 23 timer = threading.Timer(timeout_in_seconds, timeout_handler) |
| 24 timer.start() |
| 25 |
| 26 stdout, stderr = process.communicate() |
| 27 exit_code = process.wait() |
| 28 timer.cancel() |
| 29 |
| 30 end = time.time() |
| 31 |
| 32 return (exit_code, stdout, stderr, end - start, timeout_value['did_timeout']) |
| 33 |
| 34 def main(args): |
| 35 recording_file = args[0] |
| 36 result_file = args[1] |
| 37 |
| 38 with open(recording_file) as fd: |
| 39 test_cases = json.load(fd) |
| 40 |
| 41 for test_case in test_cases: |
| 42 name = test_case['name'] |
| 43 command = test_case['command'] |
| 44 executable = command['executable'] |
| 45 arguments = command['arguments'] |
| 46 timeout_limit = command['timeout_limit'] |
| 47 |
| 48 exit_code, stdout, stderr, duration, did_timeout = ( |
| 49 run_command(name, executable, arguments, timeout_limit)) |
| 50 |
| 51 test_case['command_output'] = { |
| 52 'exit_code' : exit_code, |
| 53 'stdout' : stdout, |
| 54 'stderr' : stderr, |
| 55 'duration' : duration, |
| 56 'did_timeout' : did_timeout, |
| 57 } |
| 58 with open(result_file, 'w') as fd: |
| 59 json.dump(test_cases, fd) |
| 60 |
| 61 if __name__ == '__main__': |
| 62 if len(sys.argv) != 3: |
| 63 print >> sys.stderr, ("Usage: %s <input-file.json> <output-file.json>" |
| 64 % sys.argv[0]) |
| 65 sys.exit(1) |
| 66 sys.exit(main(sys.argv[1:])) |
| 67 |
OLD | NEW |