File size: 1,035 Bytes
6078833
 
 
 
 
0167b87
6078833
0167b87
6078833
0167b87
6078833
 
 
ddf8386
0167b87
 
 
6078833
 
 
 
 
 
 
 
 
 
 
0167b87
 
 
6078833
ddf8386
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
from src.utils.tooling import tool
import subprocess
import tempfile

@tool
def execute_code(file_path: str) -> str:
    """
    Executes Python code from a file and returns the final result.
    Args:
        file_path (str): The path to the file containing the Python code to execute.
    Returns:
        str: The result of the code execution.
    """
    try:
        with open(file_path, 'r') as file:
            code = file.read()

        with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
            temp_file.write(code.encode('utf-8'))
            temp_file_path = temp_file.name

        result = subprocess.run(['python', temp_file_path], capture_output=True, text=True)

        if result.returncode != 0:
            raise Exception(f"Error executing code: {result.stderr}")

        return result.stdout

    except FileNotFoundError:
        raise FileNotFoundError(f"The file at {file_path} does not exist.")

    except Exception as e:
        raise Exception(f"An error occurred: {str(e)}")