-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.py
69 lines (62 loc) · 2.45 KB
/
verify.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os, sys
import subprocess as sp
def python_verify(data, user_file):
answer = open(user_file).read()
# write to temporary files
with open('answer.py', 'w') as temp:
temp.write(answer)
try:
test = open('task/test.py').read()
with open('test.py', 'w') as temp:
temp.write(test)
except IOError: # user has not requested task yet
print("A task folder could not be found. Request a task using python client.py request <task_id>")
sys.exit(1) # terminate program TODO: find a better way of doing this
proc = sp.Popen(['python', 'test.py'], stdin=sp.PIPE, stdout=sp.PIPE)
test_result, _ = proc.communicate(input=data)
# dispose of temporary files
os.remove('answer.py')
os.remove('test.py')
return test_result
def js_verify(data, user_file):
answer = open(user_file).read()
# write to temporary files
with open('answer.js', 'w') as temp:
temp.write(answer)
try:
test = open('task/test.js').read()
except IOError: # user has not requested task yet
print("A task folder could not be found. Request a task\nusing python client.py request <task_id>")
sys.exit(1) # terminate program TODO: find a better way of doing this
with open('test.js', 'w') as temp:
temp.write(test)
proc = sp.Popen(['node', 'test.js'], stdin=sp.PIPE, stdout=sp.PIPE)
test_result, _ = proc.communicate(input=data)
# dispose of temporary files
os.remove('answer.js')
os.remove('test.js')
return test_result
def cpp_verify(data, user_file):
answer = open(user_file).read()
# write to temp file
with open('answer.cpp', 'w') as temp:
temp.write(answer)
try:
test = open('task/test.h').read()
with open('test.h', 'w') as temp:
temp.write(test)
except IOError: # user has not requested task yet
print("A task folder could not be found. Request a task\nusing python client.py request <task_id>")
sys.exit(1) # terminate program
proc = sp.Popen(['g++', 'answer.cpp', 'test.h', '-o', 'test'], stderr=sp.DEVNULL, stdout=sp.DEVNULL)
proc.wait()
if(proc.returncode):
print("Answer failed to compile.")
sys.exit(1)
proc = sp.Popen(['./test'], stdin=sp.PIPE, stdout=sp.PIPE)
test_result, _ = proc.communicate(input=data)
# dispose of temporary files
os.remove('answer.cpp')
os.remove('test.h')
os.remove('test')
return test_result