From fb424dfa193f067ced520f5dd8fda518bed901f3 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Mon, 8 Sep 2025 16:25:09 +0300 Subject: [PATCH] =?UTF-8?q?executing=20code=20=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/run_python_file.py | 46 ++++++++++++++++++++++++++++++++++++ tests.py | 20 +++++++++------- 2 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 functions/run_python_file.py diff --git a/functions/run_python_file.py b/functions/run_python_file.py new file mode 100644 index 0000000..f85316e --- /dev/null +++ b/functions/run_python_file.py @@ -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}" \ No newline at end of file diff --git a/tests.py b/tests.py index ac00bbe..d051ba1 100644 --- a/tests.py +++ b/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)