add write_file and run_python_file with tests

This commit is contained in:
2026-03-28 11:42:52 +01:00
parent 164d89c295
commit fc885a4cb9
7 changed files with 82 additions and 75 deletions
+18
View File
@@ -0,0 +1,18 @@
import os
def write_file(working_directory, file_path, content):
abs_path = os.path.abspath(working_directory)
new_file_path = os.path.normpath(os.path.join(abs_path, file_path))
valid_target_file = os.path.commonpath([abs_path, new_file_path]) == abs_path
if not valid_target_file:
return f'Error: Cannot write to "{file_path}" as it is outside the permitted working directory'
if os.path.isdir(new_file_path):
return f'Error: Cannot write to "{file_path}" as it is a directory'
os.makedirs(os.path.dirname(new_file_path), exist_ok=True)
try:
with open(new_file_path, "w") as f:
f.write(content)
except Exception as e:
return f"Error: {e}"
return f'Successfully wrote to "{file_path}" ({len(content)} characters written)'