-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecute.py
More file actions
31 lines (24 loc) · 733 Bytes
/
execute.py
File metadata and controls
31 lines (24 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import platform
import subprocess
import signal
import time
import sys
def execute(cmd):
is_linux = platform.system() == 'Linux'
infile = open(sys.argv[2], 'r').read()
t_beginning = time.time()
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, preexec_fn=os.setsid if is_linux else None)
outdata = p.communicate(infile)
t_end = time.time()
exec_time = t_end - t_beginning
print outdata[0]
if outdata[1] == '':
return '%.3f' % exec_time
else:
return 'Runtime error '
if __name__ == '__main__':
result = execute(sys.argv[1])
outfile = open(sys.argv[3],'w')
outfile.write(result)
outfile.close()