-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
221 lines (169 loc) · 6.54 KB
/
runner.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Team Test Runner
Usage:
runner.py <id> [--debug]
"""
import sys
import io
import tempfile
import json
import requests
import subprocess
import random # for mockup
# Colored terminal text
from colorama import init, Style, Fore
init()
# uuid, project id, email
def runProgram(testid: int, command: str, **kwargs) -> any:
"""
Run the provided command, noting its test id.
:param testid: The test id.
:param command: The command to run.
:param stdin: optional stdin to pass to the command.
:return: The output of the command.
"""
p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
if ("stdin" in kwargs):
#print(kwargs.get("stdin"))
out, err = p.communicate(input=kwargs.get("stdin").encode())
else:
out, err = p.communicate()
data = [testid, p.returncode, out.decode(), err]
return data;
def setup_run(project_id: int, session_id: str) -> any:
"""
Sets up the run
Returns json object of project, or None on error.
"""
email = "[email protected]"
try:
r = requests.post('https://hatathon-backend.herokuapp.com/run', json={'uuid':session_id, 'projectid':project_id, 'email':email})
if (r.status_code != 200):
print(f"Server responded to SETUP RUN request for {session_id} with error code.")
return None
else:
return r.status_code
except:
print("Error making request for setup run.")
return None
def get_project(session_id: str) -> any:
"""
Get the project by session id.
Returns json object of project, or None on error.
"""
try:
r = requests.post('https://hatathon-backend.herokuapp.com/get', json={'type':'results', 'uuid':session_id})
if (r.status_code != 200):
print(f"Server responded to request for {session_id} with error code.")
return None
else:
return r.json()
except:
print("Error making request for project details.")
return None
def get_input(test) -> str:
"""Get the input for the test."""
#https://hatathon-backend.herokuapp.com/static/{input}
if ('input' not in test):
print("Error: No input file specified. Test must have an \"input\" field.")
if (test['input'] is None):
print("No input for test.")
return ""
else:
url = f"https://hatathon-backend.herokuapp.com{requests.utils.quote(test['input'])}"
r = requests.get(url)
if (r.status_code != 200):
print(f"Server responded to request for {session_id} with error code.")
return None
return r.content.decode()
def get_output(test) -> str:
"""Get the output for the test."""
#https://hatathon-backend.herokuapp.com/static/{output}
if ('output' not in test):
print("Error: No file with expected output specified. Test must have an \"output\" field.")
if (test['output'] is None):
print("No output for test.")
return None
else:
url = f"https://hatathon-backend.herokuapp.com{requests.utils.quote(test['output'])}"
r = requests.get(url)
if (r.status_code != 200):
print(f"Server responded to request for {session_id} expected output with error code {r.status_code}.")
return None
return r.content.decode()
STATUS = {
"SUCCESS": 0,
"FAIL": 1,
"RUNNING": 2,
"WAITING": 3,
}
def return_results(session_id: str, test_id: int, status: int):
"""
Send results for a given test to the server.
"""
try:
r = requests.post('https://hatathon-backend.herokuapp.com/edit', json={'type':'res', 'uuid':session_id, 'testid':test_id, 'status':status})
if (r.status_code != 200):
print(f"Server responded to request for {session_id} with error code {r.status_code}.")
except:
print("Error making request for project details.")
"""
Main function
"""
if __name__ == '__main__':
print(Style.BRIGHT+"Crowd Code Test Runner"+Style.RESET_ALL)
if (len(sys.argv) != 3 or '--help' in sys.argv or '-h' in sys.argv):
print("Usage: runner.py <project_id> <session_id>")
sys.exit(1)
project_id = sys.argv[1]
session_id = sys.argv[2]
setup_run(project_id, session_id)
project = get_project(session_id)
if (project is None):
print(f"Fatal error getting project {session_id}, exiting.")
exit(1)
print(f"Loaded project {session_id}.")
#print(json.dumps(project, indent=4, sort_keys=True))
#print(f"Loaded project {project['name']}")
#testcases = get_testcases(project)
# iterate all tests with test_iterator(project)
print(f"Running {len(project)} tests.")
print('\n'+Style.BRIGHT+"===== TEST CASES ====="+Style.RESET_ALL)
for test in iter(project):
if (test['disabled']):
continue
return_results(session_id, test['pid'], STATUS['RUNNING'])
print(f"{'['+str(test['pid'])+']':<6}{test['command']}\t{test['author']}")
testinput = get_input(test)
testoutput = get_output(test)
#print(testoutput)
#print(f"Command: {test['command']}")
#print("-----------------------------------------------------")
#print(Style.BRIGHT+"INPUT"+Style.RESET_ALL+'\n'+get_input(test))
#print("-----------------------------------------------------")
#print(Style.BRIGHT+"EXPECTED OUTPUT"+Style.RESET_ALL+'\n'+get_output(test))
#print("-----------------------------------------------------")
run = {}
if (testinput is not None):
if (test['input'] is not None):
run = runProgram(test['pid'], test['command'], stdin=test['input'])
else:
run = runProgram(test['pid'], test['command'])
returncode = run[1]
# send results to server
if returncode == 0:
if testoutput is not None:
if run[2] == testoutput:
return_results(session_id, test['pid'], STATUS['SUCCESS'])
print("success")
else:
#print(testoutput)
#print(run[2])
return_results(session_id, test['pid'], STATUS['FAIL'])
print("fail 2")
else:
return_results(session_id, test['pid'], STATUS['SUCCESS'])
print("success")
else:
return_results(session_id, test['pid'], STATUS['FAIL'])
print("fail")