executing code 😅
This commit is contained in:
46
functions/run_python_file.py
Normal file
46
functions/run_python_file.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
def run_python_file(working_directory, file_path, args=[]):
|
||||
try:
|
||||
full_path = os.path.join(working_directory, file_path)
|
||||
abs_working_directory = os.path.abspath(working_directory)
|
||||
abs_target_path = os.path.abspath(full_path)
|
||||
|
||||
if not abs_target_path.startswith(abs_working_directory):
|
||||
return (
|
||||
f'Error: Cannot execute "{file_path}" as it is outside the permitted working directory'
|
||||
)
|
||||
|
||||
if not os.path.exists(abs_target_path):
|
||||
return f'Error: File "{file_path}" not found.'
|
||||
|
||||
if not file_path.endswith(".py"):
|
||||
return f'Error: "{file_path}" is not a Python file.'
|
||||
|
||||
completed = subprocess.run(
|
||||
["python3", abs_target_path] + args,
|
||||
cwd=abs_working_directory,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
result_parts = []
|
||||
if completed.stdout.strip():
|
||||
result_parts.append("STDOUT:\n" + completed.stdout.strip())
|
||||
if completed.stderr.strip():
|
||||
result_parts.append("STDERR:\n" + completed.stderr.strip())
|
||||
if completed.returncode != 0:
|
||||
result_parts.append(f"Process exited with code {completed.returncode}")
|
||||
|
||||
if not result_parts:
|
||||
return "No output produced."
|
||||
|
||||
return "\n".join(result_parts)
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return "Error: Process timed out after 30 seconds."
|
||||
except Exception as e:
|
||||
return f"Error: executing Python file: {e}"
|
||||
20
tests.py
20
tests.py
@@ -1,20 +1,24 @@
|
||||
from functions.write_file import write_file
|
||||
from functions.run_python_file import run_python_file
|
||||
|
||||
|
||||
def run_tests():
|
||||
cases = [
|
||||
("calculator", "lorem.txt", "wait, this isn't lorem ipsum"),
|
||||
("calculator", "pkg/morelorem.txt", "lorem ipsum dolor sit amet"),
|
||||
("calculator", "/tmp/temp.txt", "this should not be allowed"),
|
||||
("calculator", "main.py", []),
|
||||
("calculator", "main.py", ["3 + 5"]),
|
||||
("calculator", "tests.py", []),
|
||||
("calculator", "../main.py", []),
|
||||
("calculator", "nonexistent.py", []),
|
||||
]
|
||||
|
||||
for working_dir, file_path, content in cases:
|
||||
print(f'write_file("{working_dir}", "{file_path}", "{content}"):')
|
||||
for working_dir, file_path, args in cases:
|
||||
if args:
|
||||
print(f'run_python_file("{working_dir}", "{file_path}", {args}):')
|
||||
else:
|
||||
print(f'run_python_file("{working_dir}", "{file_path}"):')
|
||||
|
||||
result = write_file(working_dir, file_path, content)
|
||||
result = run_python_file(working_dir, file_path, args)
|
||||
|
||||
print("Result:")
|
||||
|
||||
for line in result.splitlines():
|
||||
print(" " + line)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user