From 55422714072b54dbcd05189d27ed46d07d77d66e Mon Sep 17 00:00:00 2001 From: NanthagopalEswaran Date: Wed, 12 Jun 2024 19:31:45 +0530 Subject: [PATCH 1/4] Used absolute path of virtual environment bin path in local command executors --- .../coding/local_commandline_code_executor.py | 5 ++-- test/coding/test_commandline_code_executor.py | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index 29172bbe922..620b359a4ae 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -294,10 +294,11 @@ def _execute_code_dont_check_setup(self, code_blocks: List[CodeBlock]) -> Comman env = os.environ.copy() if self._virtual_env_context: - path_with_virtualenv = rf"{self._virtual_env_context.bin_path}{os.pathsep}{env['PATH']}" + virtual_env_abs_path = os.path.abspath(self._virtual_env_context.bin_path) + path_with_virtualenv = rf"{virtual_env_abs_path}{os.pathsep}{env['PATH']}" env["PATH"] = path_with_virtualenv if WIN32: - activation_script = os.path.join(self._virtual_env_context.bin_path, "activate.bat") + activation_script = os.path.join(virtual_env_abs_path, "activate.bat") cmd = [activation_script, "&&", *cmd] try: diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index 4daf2e21bcb..923df0ea89e 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -4,6 +4,7 @@ import uuid import venv from pathlib import Path +import shutil import pytest @@ -411,3 +412,28 @@ def test_local_executor_with_custom_python_env(): assert execution.exit_code == 0 assert execution.output.strip() == "True" + + +def test_local_executor_with_custom_python_env_in_local_relative_path(): + try: + relative_folder_path = "temp_folder" + if not os.path.isdir(relative_folder_path): + os.mkdir(relative_folder_path) + + venv_path = os.path.join(relative_folder_path, ".venv") + env_builder = venv.EnvBuilder(with_pip=True) + env_builder.create(venv_path) + env_builder_context = env_builder.ensure_directories(venv_path) + + executor = LocalCommandLineCodeExecutor(work_dir=relative_folder_path, virtual_env_context=env_builder_context) + code_blocks = [ + # https://stackoverflow.com/questions/1871549/how-to-determine-if-python-is-running-inside-a-virtualenv + CodeBlock(code="import sys; print(sys.prefix != sys.base_prefix)", language="python"), + ] + execution = executor.execute_code_blocks(code_blocks) + + assert execution.exit_code == 0 + assert execution.output.strip() == "True" + finally: + if os.path.isdir(relative_folder_path): + shutil.rmtree(relative_folder_path) From 2cb46a8b2f6725f2629e5bad1d9a3e5c91dad155 Mon Sep 17 00:00:00 2001 From: NanthagopalEswaran Date: Wed, 12 Jun 2024 19:56:57 +0530 Subject: [PATCH 2/4] Checked if the expected venv is used or not --- test/coding/test_commandline_code_executor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index 923df0ea89e..f83c4f5c0d1 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -428,12 +428,14 @@ def test_local_executor_with_custom_python_env_in_local_relative_path(): executor = LocalCommandLineCodeExecutor(work_dir=relative_folder_path, virtual_env_context=env_builder_context) code_blocks = [ # https://stackoverflow.com/questions/1871549/how-to-determine-if-python-is-running-inside-a-virtualenv - CodeBlock(code="import sys; print(sys.prefix != sys.base_prefix)", language="python"), + CodeBlock(code="import sys; print(sys.executable)", language="python"), ] execution = executor.execute_code_blocks(code_blocks) assert execution.exit_code == 0 - assert execution.output.strip() == "True" + + bin_path = os.path.abspath(env_builder_context.bin_path) + assert Path(execution.output.strip()).parent.samefile(bin_path) finally: if os.path.isdir(relative_folder_path): shutil.rmtree(relative_folder_path) From 158fd302d1b44d37251d144b2498130a8ed63333 Mon Sep 17 00:00:00 2001 From: NanthagopalEswaran Date: Wed, 12 Jun 2024 20:00:47 +0530 Subject: [PATCH 3/4] Added code comments for documentation --- test/coding/test_commandline_code_executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index f83c4f5c0d1..423a53a762f 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -427,13 +427,13 @@ def test_local_executor_with_custom_python_env_in_local_relative_path(): executor = LocalCommandLineCodeExecutor(work_dir=relative_folder_path, virtual_env_context=env_builder_context) code_blocks = [ - # https://stackoverflow.com/questions/1871549/how-to-determine-if-python-is-running-inside-a-virtualenv CodeBlock(code="import sys; print(sys.executable)", language="python"), ] execution = executor.execute_code_blocks(code_blocks) assert execution.exit_code == 0 + # Check if the expected venv is used bin_path = os.path.abspath(env_builder_context.bin_path) assert Path(execution.output.strip()).parent.samefile(bin_path) finally: From 11e8fa389362b84fa00a1bc244ffbf87b5cb2b8f Mon Sep 17 00:00:00 2001 From: NanthagopalEswaran Date: Sun, 16 Jun 2024 21:39:28 +0530 Subject: [PATCH 4/4] fix: format issue - shutil lib --- test/coding/test_commandline_code_executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index 423a53a762f..99c564b42a4 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -1,10 +1,10 @@ import os +import shutil import sys import tempfile import uuid import venv from pathlib import Path -import shutil import pytest