-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
226 lines (196 loc) · 6.68 KB
/
executor.py
File metadata and controls
226 lines (196 loc) · 6.68 KB
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
222
223
224
225
226
import subprocess
import json
import tempfile
import os
import time
def run_python_code(code, test_input, timeout=5):
"""
Run Python code in isolated Docker container using stdin
"""
try:
# Create a wrapper script that includes the code
full_script = f"""
{code}
import json
args = {repr(test_input)}
result = solution(*args)
print(json.dumps(result))
"""
# Run docker with code passed via stdin
cmd = [
'docker', 'run', '--rm', '-i',
'--network', 'none',
'--memory', '128m',
'--cpus', '0.5',
'python:3.11-slim',
'python', '-c', full_script
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout + 2
)
if result.returncode == 0:
output = result.stdout.strip()
try:
import json
return json.loads(output)
except:
return eval(output)
else:
error_msg = result.stderr.strip()
if 'timeout' in error_msg.lower():
return {'error': 'Time limit exceeded'}
error_lines = [line for line in error_msg.split('\n') if line.strip() and 'Error' in line]
return {'error': error_lines[0][:200] if error_lines else error_msg[:200]}
except subprocess.TimeoutExpired:
return {'error': 'Execution timeout'}
except Exception as e:
return {'error': f'Runtime error: {str(e)}'}
def run_sql_query(query, setup_sql, timeout=5):
"""
Run SQL query in SQLite without file mounting
"""
try:
# Combine setup and query
full_sql = f"{setup_sql}\n{query};"
# Run sqlite directly with SQL passed as command
cmd = [
'docker', 'run', '--rm', '-i',
'--network', 'none',
'--memory', '128m',
'alpine:latest',
'sh', '-c',
f'apk add --no-cache sqlite > /dev/null 2>&1 && echo {repr(full_sql)} | sqlite3 -batch -separator "|" :memory:'
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout + 3
)
if result.returncode == 0:
return result.stdout.strip()
else:
return {'error': result.stderr.strip()[:200]}
except subprocess.TimeoutExpired:
return {'error': 'Query timeout'}
except Exception as e:
return {'error': f'SQL error: {str(e)}'}
def run_java_code(code, test_input, timeout=5):
"""
Compile and run Java code without file mounting
"""
try:
# Create main wrapper
test_harness = f"""
{code}
public class Main {{
public static void main(String[] args) {{
String input = "{test_input[0] if test_input else ""}";
boolean result = Solution.isPalindrome(input);
System.out.println(result);
}}
}}
"""
# Escape quotes for shell
escaped_code = test_harness.replace('"', '\\"').replace('$', '\\$')
# Run compilation and execution in one go
cmd = [
'docker', 'run', '--rm', '-i',
'--network', 'none',
'--memory', '256m',
'openjdk:11-slim',
'sh', '-c',
f'echo "{escaped_code}" > Main.java && javac Main.java && timeout {timeout} java Main'
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout + 3
)
if result.returncode == 0:
output = result.stdout.strip().lower()
return output == 'true'
else:
error = result.stderr.strip()
if 'error' in error.lower():
lines = error.split('\n')
for line in lines:
if 'error' in line.lower():
return {'error': line[:150]}
return {'error': error[:200]}
except subprocess.TimeoutExpired:
return {'error': 'Execution timeout'}
except Exception as e:
return {'error': f'Java error: {str(e)}'}
def evaluate_submission(assignment, code):
"""
Run all test cases for a submission
Returns results with pass/fail for each test
"""
language = assignment['language']
public_tests = assignment.get('public_tests', [])
hidden_tests = assignment.get('hidden_tests', [])
all_tests = public_tests + hidden_tests
results = []
for idx, test in enumerate(all_tests):
is_public = idx < len(public_tests)
# run the code
if language == 'python':
actual_output = run_python_code(code, test['input'])
elif language == 'sql':
setup = test.get('setup_sql', assignment.get('setup_sql', ''))
actual_output = run_sql_query(code, setup)
elif language == 'java':
actual_output = run_java_code(code, test['input'])
else:
actual_output = {'error': 'Unsupported language'}
# check if it passed
expected = test['expected_output']
if isinstance(actual_output, dict) and 'error' in actual_output:
passed = False
else:
passed = (actual_output == expected)
# build result object
test_result = {
'test_num': idx + 1,
'passed': passed,
'is_public': is_public
}
# only include details for public tests
if is_public:
test_result['description'] = test.get('description', f'Test {idx + 1}')
test_result['expected'] = expected
test_result['actual'] = actual_output
results.append(test_result)
# calculate stats
total = len(results)
passed = sum(1 for r in results if r['passed'])
score = round((passed / total * 100), 1) if total > 0 else 0
return {
'total_tests': total,
'passed_tests': passed,
'public_tests': len(public_tests),
'hidden_tests': len(hidden_tests),
'test_results': results,
'score': score,
'status': 'passed' if passed == total else 'failed'
}
# quick test if run directly
if __name__ == '__main__':
test_code = '''
def solution(nums, target):
seen = {}
for i, num in enumerate(nums):
diff = target - num
if diff in seen:
return [seen[diff], i]
seen[num] = i
return []
'''
test_input = [[2, 7, 11, 15], 9]
result = run_python_code(test_code, test_input)
print(f"Test result: {result}")