import time
import threading
import subprocess
class Command( object ) :
def __init__ ( self , cmd , inp_path, out_path) :
self .cmd = cmd .split ( ' ' )
self .process = None
self .inp_path = inp_path
self .out_path = out_path
def run( self , timeout) :
def target( ) :
f = open ( self .inp_path , 'r' )
out = open ( self .out_path , 'w' )
self .process = subprocess .Popen ( self .cmd ,
bufsize = 4096 ,
shell = False ,
stdin = f,
stdout = out)
self .process .communicate ( )
out.flush ( )
return_code = 0
thread = threading .Thread ( target = target)
thread .start ( )
thread .join ( timeout)
if thread .isAlive ( ) :
return_code = 123456
try :
self .process .terminate ( )
thread .join ( )
except AttributeError :
pass
else :
return_code = self .process .returncode
return return_code
c = Command( './a.out' , '/tmp/in.txt' , '/tmp/out.txt' )
return_code = c.run ( timeout = 2 ) #timelimit.
if return_code == 123456 :
print 'TLE' #Time Limit Exceeded
elif return_code == 0 :
print 'Program ran successfully' #Either WA/AC
else :
print 'RTE' #Run Time Error
aW1wb3J0IHRpbWUKaW1wb3J0IHRocmVhZGluZwppbXBvcnQgc3VicHJvY2VzcwoKY2xhc3MgQ29tbWFuZChvYmplY3QpOgoJZGVmIF9faW5pdF9fKHNlbGYsIGNtZCwgaW5wX3BhdGgsIG91dF9wYXRoKToKCQlzZWxmLmNtZCA9IGNtZC5zcGxpdCgnICcpCgkJc2VsZi5wcm9jZXNzID0gTm9uZQoJCXNlbGYuaW5wX3BhdGggPSBpbnBfcGF0aAoJCXNlbGYub3V0X3BhdGggPSBvdXRfcGF0aAoKCWRlZiBydW4oc2VsZiwgdGltZW91dCk6CgkJZGVmIHRhcmdldCgpOgoJCQlmID0gb3BlbihzZWxmLmlucF9wYXRoLCAncicpCgkJCQoJCQlvdXQgPSBvcGVuKHNlbGYub3V0X3BhdGgsJ3cnKQoJCQkKCQkJc2VsZi5wcm9jZXNzID0gc3VicHJvY2Vzcy5Qb3BlbihzZWxmLmNtZCwKCQkJCQkJICAgYnVmc2l6ZSA9IDQwOTYsCgkJCQkJCSAgIHNoZWxsID0gRmFsc2UsIAoJCQkJCQkgICBzdGRpbiA9IGYsIAoJCQkJCQkgICBzdGRvdXQgPSBvdXQpCgkJCXNlbGYucHJvY2Vzcy5jb21tdW5pY2F0ZSgpCgkJCW91dC5mbHVzaCgpCgkJcmV0dXJuX2NvZGUgPSAwCgkJdGhyZWFkID0gdGhyZWFkaW5nLlRocmVhZCh0YXJnZXQgPSB0YXJnZXQpCgkJdGhyZWFkLnN0YXJ0KCkKCQl0aHJlYWQuam9pbih0aW1lb3V0KQoJCWlmIHRocmVhZC5pc0FsaXZlKCk6CgkJCXJldHVybl9jb2RlID0gMTIzNDU2CgkJCXRyeToKCQkJCXNlbGYucHJvY2Vzcy50ZXJtaW5hdGUoKQoJIAkJCXRocmVhZC5qb2luKCkKCSAJCWV4Y2VwdCBBdHRyaWJ1dGVFcnJvcjoKCQkgCQlwYXNzCgkJZWxzZToKCQkgCXJldHVybl9jb2RlID0gc2VsZi5wcm9jZXNzLnJldHVybmNvZGUKCQlyZXR1cm4gcmV0dXJuX2NvZGUKCmMgPSBDb21tYW5kKCcuL2Eub3V0JywgJy90bXAvaW4udHh0JywgJy90bXAvb3V0LnR4dCcpCnJldHVybl9jb2RlID0gYy5ydW4odGltZW91dCA9IDIpICN0aW1lbGltaXQuCgppZiByZXR1cm5fY29kZSA9PSAxMjM0NTY6CglwcmludCAnVExFJyAjVGltZSBMaW1pdCBFeGNlZWRlZAplbGlmIHJldHVybl9jb2RlID09IDA6CglwcmludCAnUHJvZ3JhbSByYW4gc3VjY2Vzc2Z1bGx5JyAjRWl0aGVyIFdBL0FDCmVsc2U6CglwcmludCAnUlRFJyAjUnVuIFRpbWUgRXJyb3I=
stdout
stderr
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "prog.py", line 14, in target
IOError: [Errno 2] No such file or directory: '/tmp/in.txt'
Traceback (most recent call last):
File "prog.py", line 43, in <module>
File "prog.py", line 39, in run
AttributeError: 'NoneType' object has no attribute 'returncode'