File size: 1,432 Bytes
5fe2042
 
 
 
 
 
fa58e66
 
 
5fe2042
 
fa58e66
5fe2042
 
fa58e66
 
 
 
 
 
 
5fe2042
fa58e66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import subprocess
import shutil
import tempfile
from typing import Tuple


def evaluate_solution(task_id: str, solution: str) -> Tuple[bool, str]:
    initial_dir = os.getcwd()
    task_dir = task_id.replace('/', '_')
    original_task_path = 'tasks/{}'.format(task_dir)
    
    if not os.path.exists(original_task_path):
        raise FileNotFoundError('Task not found: {}'.format(task_id))
    
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_task_path = os.path.join(temp_dir, task_dir)

        # Replicate the directory structure with symbolic links
        shutil.copytree(
            original_task_path,
            temp_task_path,
            symlinks=True,
            dirs_exist_ok=True,
        )
        
        os.chdir(temp_task_path)
        
        # Write solution with UTF-8 encoding
        with open('contracts/Task.sol', 'w', encoding='utf-8') as f:
            f.write(solution)

        try:
            result = subprocess.run(
                ['npx', 'hardhat', 'test'],
                capture_output=True,
                text=True,
                check=True,
                encoding='utf-8',  # Specify UTF-8 encoding for subprocess
            )
            return True, result.stdout
        except subprocess.CalledProcessError as e:
            return False, e.stderr
        finally:
            os.chdir(initial_dir)