Improve error handling, return hardhat output
Browse files- api/engine.py +38 -12
- api/resources/root.py +5 -20
api/engine.py
CHANGED
@@ -4,20 +4,46 @@
|
|
4 |
|
5 |
import os
|
6 |
import subprocess
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
-
def evaluate_solution(task_id: str, solution: str):
|
10 |
initial_dir = os.getcwd()
|
11 |
task_dir = task_id.replace('/', '_')
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
os.chdir(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
import os
|
6 |
import subprocess
|
7 |
+
import shutil
|
8 |
+
import tempfile
|
9 |
+
from typing import Tuple
|
10 |
|
11 |
|
12 |
+
def evaluate_solution(task_id: str, solution: str) -> Tuple[bool, str]:
|
13 |
initial_dir = os.getcwd()
|
14 |
task_dir = task_id.replace('/', '_')
|
15 |
+
original_task_path = 'tasks/{}'.format(task_dir)
|
16 |
+
|
17 |
+
if not os.path.exists(original_task_path):
|
18 |
+
raise FileNotFoundError('Task not found: {}'.format(task_id))
|
19 |
+
|
20 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
21 |
+
temp_task_path = os.path.join(temp_dir, task_dir)
|
22 |
|
23 |
+
# Replicate the directory structure with symbolic links
|
24 |
+
shutil.copytree(
|
25 |
+
original_task_path,
|
26 |
+
temp_task_path,
|
27 |
+
symlinks=True,
|
28 |
+
dirs_exist_ok=True,
|
29 |
+
)
|
30 |
+
|
31 |
+
os.chdir(temp_task_path)
|
32 |
+
|
33 |
+
# Write solution with UTF-8 encoding
|
34 |
+
with open('contracts/Task.sol', 'w', encoding='utf-8') as f:
|
35 |
+
f.write(solution)
|
36 |
+
|
37 |
+
try:
|
38 |
+
result = subprocess.run(
|
39 |
+
['npx', 'hardhat', 'test'],
|
40 |
+
capture_output=True,
|
41 |
+
text=True,
|
42 |
+
check=True,
|
43 |
+
encoding='utf-8', # Specify UTF-8 encoding for subprocess
|
44 |
+
)
|
45 |
+
return True, result.stdout
|
46 |
+
except subprocess.CalledProcessError as e:
|
47 |
+
return False, e.stderr
|
48 |
+
finally:
|
49 |
+
os.chdir(initial_dir)
|
api/resources/root.py
CHANGED
@@ -3,17 +3,10 @@
|
|
3 |
|
4 |
import falcon
|
5 |
from engine import evaluate_solution
|
6 |
-
from threading import Lock
|
7 |
-
from typing import Dict
|
8 |
import logging
|
9 |
|
10 |
|
11 |
class RootResource:
|
12 |
-
def __init__(self):
|
13 |
-
self.main_lock = Lock()
|
14 |
-
self.task_locks: Dict[str, Lock] = {}
|
15 |
-
self.max_tasks = 5
|
16 |
-
|
17 |
def on_get(self, request, response):
|
18 |
response.text = 'Human Eval for Solidity Server v1.2410.0'
|
19 |
|
@@ -31,19 +24,11 @@ class RootResource:
|
|
31 |
solution = payload['solution']
|
32 |
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
if task_id not in self.task_locks:
|
41 |
-
self.task_locks[task_id] = Lock()
|
42 |
-
|
43 |
-
with self.task_locks[task_id]:
|
44 |
-
passed = evaluate_solution(task_id, solution)
|
45 |
-
|
46 |
-
response.media = {'passed': passed}
|
47 |
except FileNotFoundError as e:
|
48 |
logging.error('Task not found: {}'.format(str(e)))
|
49 |
response.status = falcon.HTTP_404
|
|
|
3 |
|
4 |
import falcon
|
5 |
from engine import evaluate_solution
|
|
|
|
|
6 |
import logging
|
7 |
|
8 |
|
9 |
class RootResource:
|
|
|
|
|
|
|
|
|
|
|
10 |
def on_get(self, request, response):
|
11 |
response.text = 'Human Eval for Solidity Server v1.2410.0'
|
12 |
|
|
|
24 |
solution = payload['solution']
|
25 |
|
26 |
try:
|
27 |
+
passed, output = evaluate_solution(task_id, solution)
|
28 |
+
response.media = {
|
29 |
+
'passed': passed,
|
30 |
+
'output': output,
|
31 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
except FileNotFoundError as e:
|
33 |
logging.error('Task not found: {}'.format(str(e)))
|
34 |
response.status = falcon.HTTP_404
|