-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
109 lines (87 loc) · 2.99 KB
/
lambda_function.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
import sys
import json
import boto3
import io
import os
import subprocess
import concurrent.futures
import uuid
def execute_test_case(code, test_case):
temp_script_filename = f"/tmp/temp_script_{uuid.uuid4().hex}.py"
args = {k: v for k, v in test_case.items() if k != "expected_output"}
expected_output = test_case.get("expected_output", None)
complete_code = f"""
import argparse
import json
{code}
def run_test_case():
try:
result = add(**{args})
return {{"test_case": {args}, "your_output": result, "expected_output": {expected_output}, "status": "PASSED" if result == {expected_output} else "FAILED", "error": None}}
except Exception as e:
return {{"test_case": {args}, "your_output": None, "expected_output": {expected_output}, "status": "FAILED", "error": str(e)}}
if __name__ == "__main__":
output = run_test_case()
print(json.dumps(output))
"""
try:
with open(temp_script_filename, "w") as temp_script_file:
temp_script_file.write(complete_code)
result = subprocess.run(
["python3", temp_script_filename], capture_output=True, text=True
)
if result.returncode == 0:
return json.loads(result.stdout)
else:
return {
"test_case": args,
"your_output": None,
"expected_output": expected_output,
"status": "FAILED",
"error": result.stderr,
}
except Exception as e:
return {
"test_case": args,
"your_output": None,
"expected_output": expected_output,
"status": "FAILED",
"error": str(e),
}
finally:
if os.path.exists(temp_script_filename):
os.remove(temp_script_filename)
def handler(event, context):
language = event.get("language", "")
code = event.get("code", "")
TEST_CASE_DIR = os.path.join("test_cases", "add_test_cases.json")
with open(TEST_CASE_DIR, "r") as test_file:
test_cases = json.load(test_file)
if language != "python" or code == "":
response = {
"status": 400,
"message": "Valid language (python), code, and test cases are required",
"output": None,
}
else:
results = []
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(execute_test_case, code, test_case)
for test_case in test_cases
]
for future in concurrent.futures.as_completed(futures):
results.append(future.result())
response = {
"status": 200,
"output": results,
"message": "Code executed successfully",
}
return {
"statusCode": response["status"],
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
},
"body": json.dumps(response),
}