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 did_timeout = False | |
ricow1
2013/05/27 09:05:17
Write the usage pattern in a comment here.
kustermann
2013/05/27 11:11:37
Done.
| |
10 | |
11 def run_command(name, executable, arguments, timeout_in_seconds): | |
12 print "Running %s: '%s'" % (name, [executable] + arguments) | |
13 global did_timeout | |
ricow1
2013/05/27 09:05:17
why don't you send this back in the tuple like the
kustermann
2013/05/27 11:11:37
I think we need to make it global. Otherwise 'time
| |
14 did_timeout = False | |
15 start = time.time() | |
16 | |
17 process = subprocess.Popen([executable] + arguments, | |
18 stdout=subprocess.PIPE, | |
19 stderr=subprocess.PIPE) | |
20 def timeout_handler(): | |
21 global did_timeout | |
ricow1
2013/05/27 09:05:17
this is already global
kustermann
2013/05/27 11:11:37
I think global has to be set on every new scope. B
| |
22 did_timeout = True | |
23 process.kill() | |
24 timer = threading.Timer(timeout_in_seconds, timeout_handler) | |
25 timer.start() | |
26 | |
27 stdout, stderr = process.communicate() | |
28 exit_code = process.wait() | |
29 timer.cancel() | |
30 | |
31 end = time.time() | |
32 | |
33 return (exit_code, stdout, stderr, end - start) | |
34 | |
35 def main(args): | |
36 recording_file = args[0] | |
37 result_file = args[1] | |
38 | |
39 with open(recording_file) as fd: | |
40 test_cases = json.load(fd) | |
41 | |
42 for test_case in test_cases: | |
43 name = test_case['name'] | |
44 command = test_case['command'] | |
45 executable = command['executable'] | |
46 arguments = command['arguments'] | |
47 timeout_limit = command['timeout_limit'] | |
48 | |
49 exit_code, stdout, stderr, duration = ( | |
50 run_command(name, executable, arguments, timeout_limit)) | |
51 | |
52 test_case['command_output'] = { | |
53 'exit_code' : exit_code, | |
54 'stdout' : stdout, | |
55 'stderr' : stderr, | |
56 'duration' : duration, | |
57 'did_timeout' : did_timeout, | |
58 } | |
59 with open(result_file, 'w') as fd: | |
60 json.dump(test_cases, fd) | |
61 | |
62 if __name__ == '__main__': | |
63 if len(sys.argv) != 3: | |
64 print >> sys.stderr, ("Usage: %s <input-file.json> <output-file.json>" | |
65 % sys.argv[0]) | |
66 sys.exit(1) | |
67 sys.exit(main(sys.argv[1:])) | |
68 | |
OLD | NEW |