#!/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)