From fad74f5fd8bf1a56992a04c5b324273a2983ce40 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 14:06:22 +0530 Subject: [PATCH 01/13] macos bugfix --- autogen/code_utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 48cc755b3ed4..7de88fa42c90 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -219,7 +219,7 @@ def get_powershell_command(): if result.returncode == 0: return "powershell" - except (FileNotFoundError, NotADirectoryError): + except (FileNotFoundError, NotADirectoryError, PermissionError): # This means that 'powershell' command is not found so now we try looking for 'pwsh' try: result = subprocess.run( @@ -228,16 +228,23 @@ def get_powershell_command(): if result.returncode == 0: return "pwsh" - except (FileNotFoundError, NotADirectoryError): - if WIN32: + except (FileNotFoundError, NotADirectoryError, PermissionError): + if PermissionError: + logging.warning("The application has no permission to run powershell") + + elif WIN32: logging.warning("Neither powershell nor pwsh is installed but it is a Windows OS") + return None -powershell_command = get_powershell_command() + def _cmd(lang): + + powershell_command = get_powershell_command() + if lang.startswith("python") or lang in ["bash", "sh", powershell_command]: return lang if lang in ["shell"]: From fe7a0ff007c455f14f403c782ea50c806af36669 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 14:17:52 +0530 Subject: [PATCH 02/13] logs permissionerror warning only if lang is powershell command --- autogen/code_utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 7de88fa42c90..62d35804ad93 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -230,7 +230,7 @@ def get_powershell_command(): except (FileNotFoundError, NotADirectoryError, PermissionError): if PermissionError: - logging.warning("The application has no permission to run powershell") + return "The application has no permission to run powershell" elif WIN32: logging.warning("Neither powershell nor pwsh is installed but it is a Windows OS") @@ -246,6 +246,9 @@ def _cmd(lang): powershell_command = get_powershell_command() if lang.startswith("python") or lang in ["bash", "sh", powershell_command]: + if lang == "The application has no permission to run powershell": + logging.warning(lang) + return lang if lang in ["shell"]: return "sh" From a6937f1641cd4833e54abd196b5ef89d3f0124d0 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 19:15:46 +0530 Subject: [PATCH 03/13] condensed the if statements down --- autogen/code_utils.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 62d35804ad93..b5e132421f8d 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -219,7 +219,7 @@ def get_powershell_command(): if result.returncode == 0: return "powershell" - except (FileNotFoundError, NotADirectoryError, PermissionError): + except (FileNotFoundError, NotADirectoryError): # This means that 'powershell' command is not found so now we try looking for 'pwsh' try: result = subprocess.run( @@ -228,14 +228,18 @@ def get_powershell_command(): if result.returncode == 0: return "pwsh" - except (FileNotFoundError, NotADirectoryError, PermissionError): - if PermissionError: - return "The application has no permission to run powershell" + except (FileNotFoundError, NotADirectoryError): + if WIN32 and FileNotFoundError: + logging.warning("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") + + elif WIN32 and NotADirectoryError: + logging.warning("PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again.") - elif WIN32: - logging.warning("Neither powershell nor pwsh is installed but it is a Windows OS") - return None + + except PermissionError: + logging.warning("The application has no permission to run powershell") + return "None" @@ -243,16 +247,12 @@ def get_powershell_command(): def _cmd(lang): - powershell_command = get_powershell_command() - - if lang.startswith("python") or lang in ["bash", "sh", powershell_command]: - if lang == "The application has no permission to run powershell": - logging.warning(lang) - + if lang.startswith("python") or lang in ["bash", "sh"]: return lang if lang in ["shell"]: return "sh" if lang in ["ps1", "pwsh", "powershell"]: + powershell_command = get_powershell_command() return powershell_command raise NotImplementedError(f"{lang} not recognized in code execution") From 28c05194f651bc4aaa6d00c0bf48e185e16d6ba4 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 19:16:40 +0530 Subject: [PATCH 04/13] Update code_utils.py --- autogen/code_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index b5e132421f8d..96fac58907ed 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -239,7 +239,7 @@ def get_powershell_command(): except PermissionError: logging.warning("The application has no permission to run powershell") - return "None" + return None From 570e635db557f5ffbc3503ee44a74c1d599aff17 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 19:21:51 +0530 Subject: [PATCH 05/13] fixed formatting --- autogen/code_utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 96fac58907ed..5cc1c8997cef 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -231,22 +231,20 @@ def get_powershell_command(): except (FileNotFoundError, NotADirectoryError): if WIN32 and FileNotFoundError: logging.warning("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") - + elif WIN32 and NotADirectoryError: - logging.warning("PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again.") - + logging.warning( + "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." + ) + return None - + except PermissionError: logging.warning("The application has no permission to run powershell") return None - - - def _cmd(lang): - if lang.startswith("python") or lang in ["bash", "sh"]: return lang if lang in ["shell"]: From 227bf41f510f80733f6ca6d1de2c8b2ad54f20f6 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 21:22:58 +0530 Subject: [PATCH 06/13] handled powershell_command = None case --- autogen/code_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 5cc1c8997cef..dea17676b7b0 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -251,7 +251,7 @@ def _cmd(lang): return "sh" if lang in ["ps1", "pwsh", "powershell"]: powershell_command = get_powershell_command() - return powershell_command + return powershell_command if powershell_command is not None else "sh" raise NotImplementedError(f"{lang} not recognized in code execution") From 253fdc553fba65d9c34f93055f2efb708dc18a67 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Tue, 12 Mar 2024 22:17:39 +0530 Subject: [PATCH 07/13] bugfix --- autogen/code_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index dea17676b7b0..90613715d457 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -228,11 +228,11 @@ def get_powershell_command(): if result.returncode == 0: return "pwsh" - except (FileNotFoundError, NotADirectoryError): - if WIN32 and FileNotFoundError: + except (FileNotFoundError, NotADirectoryError) as e: + if WIN32 and isinstance(e,FileNotFoundError): logging.warning("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") - elif WIN32 and NotADirectoryError: + elif WIN32 and isinstance(e,NotADirectoryError): logging.warning( "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." ) From 4b8589c034a2f3a94b038cbf1ca418e6459b9965 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Wed, 13 Mar 2024 20:49:00 +0530 Subject: [PATCH 08/13] raising exceptions instead of logging warnings --- autogen/code_utils.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 90613715d457..cc93bf0c99c9 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -230,18 +230,21 @@ def get_powershell_command(): except (FileNotFoundError, NotADirectoryError) as e: if WIN32 and isinstance(e,FileNotFoundError): - logging.warning("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") + + raise FileNotFoundError("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") elif WIN32 and isinstance(e,NotADirectoryError): - logging.warning( + raise NotADirectoryError( "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." ) - return None + return "sh" - except PermissionError: - logging.warning("The application has no permission to run powershell") - return None + except PermissionError as e: + if WIN32 and isinstance(e,PermissionError): + raise PermissionError("The application has no permission to run powershell") + else: + logging.warning("The application has no permission to run powershell") def _cmd(lang): @@ -251,7 +254,7 @@ def _cmd(lang): return "sh" if lang in ["ps1", "pwsh", "powershell"]: powershell_command = get_powershell_command() - return powershell_command if powershell_command is not None else "sh" + return powershell_command raise NotImplementedError(f"{lang} not recognized in code execution") From 3df7a8ffe333ce84ccd9592b96db6b7018a5978b Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Wed, 13 Mar 2024 20:53:56 +0530 Subject: [PATCH 09/13] code formatting fixed --- autogen/code_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index cc93bf0c99c9..50a9af758807 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -229,11 +229,10 @@ def get_powershell_command(): return "pwsh" except (FileNotFoundError, NotADirectoryError) as e: - if WIN32 and isinstance(e,FileNotFoundError): - + if WIN32 and isinstance(e, FileNotFoundError): raise FileNotFoundError("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") - elif WIN32 and isinstance(e,NotADirectoryError): + elif WIN32 and isinstance(e, NotADirectoryError): raise NotADirectoryError( "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." ) @@ -241,10 +240,12 @@ def get_powershell_command(): return "sh" except PermissionError as e: - if WIN32 and isinstance(e,PermissionError): + if WIN32 and isinstance(e, PermissionError): raise PermissionError("The application has no permission to run powershell") else: logging.warning("The application has no permission to run powershell") + + return "sh" def _cmd(lang): From c5094ab70d6fd130276836dd58e2671039766dc2 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Fri, 15 Mar 2024 00:13:48 +0530 Subject: [PATCH 10/13] removed return sh statement --- autogen/code_utils.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 50a9af758807..b041248ca3d7 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -229,23 +229,17 @@ def get_powershell_command(): return "pwsh" except (FileNotFoundError, NotADirectoryError) as e: - if WIN32 and isinstance(e, FileNotFoundError): - raise FileNotFoundError("Neither powershell.exe nor pwsh.exe is present but it is a Windows OS") + if isinstance(e, FileNotFoundError): + raise FileNotFoundError("Neither powershell.exe nor pwsh.exe is present in the system. Please install PowerShell and try again.") - elif WIN32 and isinstance(e, NotADirectoryError): + elif isinstance(e, NotADirectoryError): raise NotADirectoryError( "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." ) - return "sh" - except PermissionError as e: - if WIN32 and isinstance(e, PermissionError): + if isinstance(e, PermissionError): raise PermissionError("The application has no permission to run powershell") - else: - logging.warning("The application has no permission to run powershell") - - return "sh" def _cmd(lang): From 6e97b55f61c8323026de0de35d7bcbd1d32f90c4 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Fri, 15 Mar 2024 00:15:42 +0530 Subject: [PATCH 11/13] fixed code formatting --- autogen/code_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index b041248ca3d7..a319ca9022fa 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -230,7 +230,9 @@ def get_powershell_command(): except (FileNotFoundError, NotADirectoryError) as e: if isinstance(e, FileNotFoundError): - raise FileNotFoundError("Neither powershell.exe nor pwsh.exe is present in the system. Please install PowerShell and try again.") + raise FileNotFoundError( + "Neither powershell.exe nor pwsh.exe is present in the system. Please install PowerShell and try again." + ) elif isinstance(e, NotADirectoryError): raise NotADirectoryError( From 020cda797d4b678c5b5b569edd4559c0809a2502 Mon Sep 17 00:00:00 2001 From: Eric Zhu Date: Sat, 16 Mar 2024 00:34:37 -0700 Subject: [PATCH 12/13] update get_powershell_command --- autogen/code_utils.py | 33 +++++++++++++++------------------ test/test_code_utils.py | 22 +++++++--------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index a319ca9022fa..ef0c90dad726 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -218,7 +218,6 @@ def get_powershell_command(): result = subprocess.run(["powershell", "$PSVersionTable.PSVersion.Major"], capture_output=True, text=True) if result.returncode == 0: return "powershell" - except (FileNotFoundError, NotADirectoryError): # This means that 'powershell' command is not found so now we try looking for 'pwsh' try: @@ -227,21 +226,21 @@ def get_powershell_command(): ) if result.returncode == 0: return "pwsh" - - except (FileNotFoundError, NotADirectoryError) as e: - if isinstance(e, FileNotFoundError): - raise FileNotFoundError( - "Neither powershell.exe nor pwsh.exe is present in the system. Please install PowerShell and try again." - ) - - elif isinstance(e, NotADirectoryError): - raise NotADirectoryError( - "PowerShell is either not installed or its path is not given properly in the environment variable PATH. Please check the path and try again." - ) - + except FileExistsError as e: + raise FileNotFoundError( + "Neither powershell.exe nor pwsh.exe is present in the system. " + "Please install PowerShell and try again. " + f"Original error: {e}" + ) + except NotADirectoryError as e: + raise NotADirectoryError( + "PowerShell is either not installed or its path is not given " + "properly in the environment variable PATH. Please check the " + "path and try again. " + f"Original error: {e}" + ) except PermissionError as e: - if isinstance(e, PermissionError): - raise PermissionError("The application has no permission to run powershell") + raise PermissionError(f"No permission to run powershell. Original error: {e}") def _cmd(lang): @@ -461,9 +460,7 @@ def execute_code( image_list = ( ["python:3-slim", "python:3", "python:3-windowsservercore"] if use_docker is True - else [use_docker] - if isinstance(use_docker, str) - else use_docker + else [use_docker] if isinstance(use_docker, str) else use_docker ) for image in image_list: # check if the image exists diff --git a/test/test_code_utils.py b/test/test_code_utils.py index e3909848188d..1d45a9b4e6df 100755 --- a/test/test_code_utils.py +++ b/test/test_code_utils.py @@ -577,24 +577,16 @@ def test_get_powershell_command_pwsh(self, mock_subprocess_run): self.assertEqual(get_powershell_command(), "pwsh") @patch("subprocess.run") - @patch("logging.warning") - def test_get_powershell_command_windows_no_shell(self, mock_logging_warning, mock_subprocess_run): - # Set up the mock to simulate 'powershell' and 'pwsh' not found + def test_get_powershell_command_not_found(self, mock_subprocess_run): mock_subprocess_run.side_effect = [FileNotFoundError, FileNotFoundError] - - with patch("autogen.code_utils.WIN32", True): - self.assertIsNone(get_powershell_command()) - mock_logging_warning.assert_called_once_with( - "Neither powershell nor pwsh is installed but it is a Windows OS" - ) + with self.assertRaises(FileNotFoundError): + get_powershell_command() @patch("subprocess.run") - def test_get_powershell_command_no_windows_no_shell(self, mock_subprocess_run): - # Set up the mock to simulate 'powershell' and 'pwsh' not found - mock_subprocess_run.side_effect = FileNotFoundError - # Mock WIN32 to False - with patch("autogen.code_utils.WIN32", False): - self.assertIsNone(get_powershell_command()) + def test_get_powershell_command_no_permission(self, mock_subprocess_run): + mock_subprocess_run.side_effect = [PermissionError, FileNotFoundError] + with self.assertRaises(PermissionError): + get_powershell_command() if __name__ == "__main__": From 1d222b3d8a6bd0aa883badd347213e33fa820b29 Mon Sep 17 00:00:00 2001 From: Abhay Mathur Date: Sun, 17 Mar 2024 00:33:28 +0530 Subject: [PATCH 13/13] Update code_utils.py fixed code format --- autogen/code_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index ef0c90dad726..230bc1638c68 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -460,7 +460,9 @@ def execute_code( image_list = ( ["python:3-slim", "python:3", "python:3-windowsservercore"] if use_docker is True - else [use_docker] if isinstance(use_docker, str) else use_docker + else [use_docker] + if isinstance(use_docker, str) + else use_docker ) for image in image_list: # check if the image exists