From 564c0bb5512865905e84aba4877ed0d88bbc4600 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 11:53:14 -0700 Subject: [PATCH 1/8] upgrade functionality --- guardrails/cli/hub/install.py | 6 ++++++ guardrails/cli/hub/utils.py | 1 + guardrails/hub/install.py | 3 ++- guardrails/hub/validator_package_service.py | 5 +++++ tests/unit_tests/cli/hub/test_install.py | 16 ++++++++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/guardrails/cli/hub/install.py b/guardrails/cli/hub/install.py index 3ad9a07d1..a9d261bf6 100644 --- a/guardrails/cli/hub/install.py +++ b/guardrails/cli/hub/install.py @@ -25,6 +25,11 @@ def install( "--quiet", help="Run the command in quiet mode to reduce output verbosity.", ), + upgrade: bool = typer.Option( + False, + "--upgrade", + help="Upgrade the package to the latest version." + ), ): try: trace_if_enabled("hub/install") @@ -41,6 +46,7 @@ def confirm(): package_uri, install_local_models=local_models, quiet=quiet, + upgrade = upgrade, install_local_models_confirm=confirm, ) except Exception as e: diff --git a/guardrails/cli/hub/utils.py b/guardrails/cli/hub/utils.py index 8d7220801..a73f23845 100644 --- a/guardrails/cli/hub/utils.py +++ b/guardrails/cli/hub/utils.py @@ -31,6 +31,7 @@ def pip_process( if package: command.append(package) + print('pip command', command) env = dict(os.environ) if no_color: env["NO_COLOR"] = "true" diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index 6ce9e16cf..502ae32eb 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -35,6 +35,7 @@ def install( package_uri: str, install_local_models=None, quiet: bool = True, + upgrade: bool = False, install_local_models_confirm: Callable = default_local_models_confirm, ) -> ValidatorModuleType: """Install a validator package from a hub URI. @@ -84,7 +85,7 @@ def install( dl_deps_msg = "Downloading dependencies" with loader(dl_deps_msg, spinner="bouncingBar"): ValidatorPackageService.install_hub_module( - module_manifest, site_packages, quiet=quiet, logger=cli_logger + module_manifest, site_packages, quiet=quiet, upgrade=upgrade, logger=cli_logger ) use_remote_endpoint = False diff --git a/guardrails/hub/validator_package_service.py b/guardrails/hub/validator_package_service.py index 526b9cb5b..a1f9c2079 100644 --- a/guardrails/hub/validator_package_service.py +++ b/guardrails/hub/validator_package_service.py @@ -260,6 +260,7 @@ def install_hub_module( module_manifest: Manifest, site_packages: str, quiet: bool = False, + upgrade: bool=False, logger=guardrails_logger, ): install_url = ValidatorPackageService.get_install_url(module_manifest) @@ -268,6 +269,10 @@ def install_hub_module( ) pip_flags = [f"--target={install_directory}", "--no-deps"] + + if upgrade: + pip_flags.append("--upgrade") + if quiet: pip_flags.append("-q") diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 21eb5abe9..1f81d7672 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -204,7 +204,23 @@ def test_other_exception(self, mocker): ) sys_exit_spy.assert_called_once_with(1) + + def test_install_with_upgrade_flag(self, mocker): + mock_install = mocker.patch("guardrails.hub.install.install") + runner = CliRunner() + result = runner.invoke( + hub_command, ["install", "--upgrade", "hub://guardrails/test-validator"] + ) + + mock_install.assert_called_once_with( + "hub://guardrails/test-validator", + install_local_models=None, + quiet=False, + install_local_models_confirm=ANY, + upgrade=True, + ) + assert result.exit_code == 0 def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") From 713aa84622e140a036b5dbe282bf9e3613cc53f7 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 11:56:07 -0700 Subject: [PATCH 2/8] remove print statement --- guardrails/cli/hub/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/guardrails/cli/hub/utils.py b/guardrails/cli/hub/utils.py index a73f23845..8d7220801 100644 --- a/guardrails/cli/hub/utils.py +++ b/guardrails/cli/hub/utils.py @@ -31,7 +31,6 @@ def pip_process( if package: command.append(package) - print('pip command', command) env = dict(os.environ) if no_color: env["NO_COLOR"] = "true" From 59fbb5d0ac774fb823e346b1b0e5b79836bc95d7 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 12:00:04 -0700 Subject: [PATCH 3/8] lint --- guardrails/cli/hub/install.py | 6 ++---- guardrails/hub/install.py | 6 +++++- guardrails/hub/validator_package_service.py | 6 +++--- tests/unit_tests/cli/hub/test_install.py | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/guardrails/cli/hub/install.py b/guardrails/cli/hub/install.py index a9d261bf6..b105517a2 100644 --- a/guardrails/cli/hub/install.py +++ b/guardrails/cli/hub/install.py @@ -26,9 +26,7 @@ def install( help="Run the command in quiet mode to reduce output verbosity.", ), upgrade: bool = typer.Option( - False, - "--upgrade", - help="Upgrade the package to the latest version." + False, "--upgrade", help="Upgrade the package to the latest version." ), ): try: @@ -46,7 +44,7 @@ def confirm(): package_uri, install_local_models=local_models, quiet=quiet, - upgrade = upgrade, + upgrade=upgrade, install_local_models_confirm=confirm, ) except Exception as e: diff --git a/guardrails/hub/install.py b/guardrails/hub/install.py index 502ae32eb..005a5306d 100644 --- a/guardrails/hub/install.py +++ b/guardrails/hub/install.py @@ -85,7 +85,11 @@ def install( dl_deps_msg = "Downloading dependencies" with loader(dl_deps_msg, spinner="bouncingBar"): ValidatorPackageService.install_hub_module( - module_manifest, site_packages, quiet=quiet, upgrade=upgrade, logger=cli_logger + module_manifest, + site_packages, + quiet=quiet, + upgrade=upgrade, + logger=cli_logger, ) use_remote_endpoint = False diff --git a/guardrails/hub/validator_package_service.py b/guardrails/hub/validator_package_service.py index a1f9c2079..e90825d27 100644 --- a/guardrails/hub/validator_package_service.py +++ b/guardrails/hub/validator_package_service.py @@ -260,7 +260,7 @@ def install_hub_module( module_manifest: Manifest, site_packages: str, quiet: bool = False, - upgrade: bool=False, + upgrade: bool = False, logger=guardrails_logger, ): install_url = ValidatorPackageService.get_install_url(module_manifest) @@ -270,9 +270,9 @@ def install_hub_module( pip_flags = [f"--target={install_directory}", "--no-deps"] - if upgrade: + if upgrade: pip_flags.append("--upgrade") - + if quiet: pip_flags.append("-q") diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 1f81d7672..28bc85fa1 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -204,7 +204,7 @@ def test_other_exception(self, mocker): ) sys_exit_spy.assert_called_once_with(1) - + def test_install_with_upgrade_flag(self, mocker): mock_install = mocker.patch("guardrails.hub.install.install") runner = CliRunner() @@ -222,6 +222,7 @@ def test_install_with_upgrade_flag(self, mocker): assert result.exit_code == 0 + def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") mock_pip_process.return_value = {"Location": "/site-pacakges"} From 997da67f34007c0a8169c80d79e1f89307608a33 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 12:10:21 -0700 Subject: [PATCH 4/8] test --- tests/unit_tests/cli/hub/test_install.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 28bc85fa1..2ae435ce7 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -206,23 +206,27 @@ def test_other_exception(self, mocker): sys_exit_spy.assert_called_once_with(1) def test_install_with_upgrade_flag(self, mocker): - mock_install = mocker.patch("guardrails.hub.install.install") + mock_install_hub_module = mocker.patch( + "guardrails.hub.validator_package_service.ValidatorPackageService.install_hub_module" + ) + runner = CliRunner() result = runner.invoke( hub_command, ["install", "--upgrade", "hub://guardrails/test-validator"] ) - mock_install.assert_called_once_with( - "hub://guardrails/test-validator", - install_local_models=None, + mock_install_hub_module.assert_called_once_with( + ANY, + ANY, quiet=False, - install_local_models_confirm=ANY, upgrade=True, + logger=ANY, ) assert result.exit_code == 0 + def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") mock_pip_process.return_value = {"Location": "/site-pacakges"} From 2a32c85a9ac07bf24662b5df080aa1fbee73945c Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 12:44:02 -0700 Subject: [PATCH 5/8] lint + test --- tests/unit_tests/cli/hub/test_install.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 2ae435ce7..21eb5abe9 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -205,27 +205,6 @@ def test_other_exception(self, mocker): sys_exit_spy.assert_called_once_with(1) - def test_install_with_upgrade_flag(self, mocker): - mock_install_hub_module = mocker.patch( - "guardrails.hub.validator_package_service.ValidatorPackageService.install_hub_module" - ) - - runner = CliRunner() - result = runner.invoke( - hub_command, ["install", "--upgrade", "hub://guardrails/test-validator"] - ) - - mock_install_hub_module.assert_called_once_with( - ANY, - ANY, - quiet=False, - upgrade=True, - logger=ANY, - ) - - assert result.exit_code == 0 - - def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") From 16a78edfe7a9f058f56bd9acb76c98477bb5facd Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 13:09:32 -0700 Subject: [PATCH 6/8] fix tests --- tests/unit_tests/cli/hub/test_install.py | 18 +++++++++++++++++- tests/unit_tests/hub/test_hub_install.py | 8 ++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 21eb5abe9..87b40dd97 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -205,6 +205,22 @@ def test_other_exception(self, mocker): sys_exit_spy.assert_called_once_with(1) + def test_install_with_upgrade_flag(self, mocker): + mock_install = mocker.patch("guardrails.hub.install.install") + runner = CliRunner() + result = runner.invoke( + hub_command, ["install", "--upgrade", "hub://guardrails/test-validator"] + ) + + mock_install.assert_called_once_with( + "hub://guardrails/test-validator", + install_local_models=None, + quiet=False, + install_local_models_confirm=ANY, + upgrade=True, + ) + + assert result.exit_code == 0 def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") @@ -216,4 +232,4 @@ def test_get_site_packages_location(mocker): mock_pip_process.assert_called_once_with("show", "pip", format="json") - assert response == "/site-pacakges" + assert response == "/site-pacakges" \ No newline at end of file diff --git a/tests/unit_tests/hub/test_hub_install.py b/tests/unit_tests/hub/test_hub_install.py index 8677c9b54..4b35feec9 100644 --- a/tests/unit_tests/hub/test_hub_install.py +++ b/tests/unit_tests/hub/test_hub_install.py @@ -99,7 +99,7 @@ def test_install_local_models__false(self, mocker, use_remote_inferencing): ) mock_pip_install_hub_module.assert_called_once_with( - self.manifest, self.site_packages, quiet=ANY, logger=ANY + self.manifest, self.site_packages, quiet=ANY, upgrade=ANY, logger=ANY ) mock_add_to_hub_init.assert_called_once_with(self.manifest, self.site_packages) @@ -160,7 +160,7 @@ def test_install_local_models__true(self, mocker, use_remote_inferencing): ) mock_pip_install_hub_module.assert_called_once_with( - self.manifest, self.site_packages, quiet=ANY, logger=ANY + self.manifest, self.site_packages, quiet=ANY, upgrade=ANY, logger=ANY ) mock_add_to_hub_init.assert_called_once_with(self.manifest, self.site_packages) @@ -221,7 +221,7 @@ def test_install_local_models__none(self, mocker, use_remote_inferencing): ) mock_pip_install_hub_module.assert_called_once_with( - self.manifest, self.site_packages, quiet=ANY, logger=ANY + self.manifest, self.site_packages, quiet=ANY, upgrade=ANY, logger=ANY ) mock_add_to_hub_init.assert_called_once_with(self.manifest, self.site_packages) @@ -278,7 +278,7 @@ def test_happy_path(self, mocker, use_remote_inferencing): ) mock_pip_install_hub_module.assert_called_once_with( - self.manifest, self.site_packages, quiet=ANY, logger=ANY + self.manifest, self.site_packages, quiet=ANY, upgrade=ANY, logger=ANY ) mock_add_to_hub_init.assert_called_once_with(self.manifest, self.site_packages) From 5d75b30aff82fe839f73f24029279ed961773d33 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 13:11:18 -0700 Subject: [PATCH 7/8] lint --- tests/unit_tests/cli/hub/test_install.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 87b40dd97..28bc85fa1 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -217,11 +217,12 @@ def test_install_with_upgrade_flag(self, mocker): install_local_models=None, quiet=False, install_local_models_confirm=ANY, - upgrade=True, + upgrade=True, ) assert result.exit_code == 0 + def test_get_site_packages_location(mocker): mock_pip_process = mocker.patch("guardrails.cli.hub.utils.pip_process") mock_pip_process.return_value = {"Location": "/site-pacakges"} @@ -232,4 +233,4 @@ def test_get_site_packages_location(mocker): mock_pip_process.assert_called_once_with("show", "pip", format="json") - assert response == "/site-pacakges" \ No newline at end of file + assert response == "/site-pacakges" From ffb6825fce3f58b3578c03ec720e36340b7e7359 Mon Sep 17 00:00:00 2001 From: Aarav Navani Date: Tue, 10 Sep 2024 13:18:20 -0700 Subject: [PATCH 8/8] tests --- tests/unit_tests/cli/hub/test_install.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit_tests/cli/hub/test_install.py b/tests/unit_tests/cli/hub/test_install.py index 28bc85fa1..6fd186322 100644 --- a/tests/unit_tests/cli/hub/test_install.py +++ b/tests/unit_tests/cli/hub/test_install.py @@ -29,6 +29,7 @@ def test_install_local_models__false(self, mocker): "hub://guardrails/test-validator", install_local_models=False, quiet=False, + upgrade=False, install_local_models_confirm=ANY, ) @@ -45,6 +46,7 @@ def test_install_local_models__true(self, mocker): "hub://guardrails/test-validator", install_local_models=True, quiet=False, + upgrade=False, install_local_models_confirm=ANY, ) @@ -61,6 +63,7 @@ def test_install_local_models__none(self, mocker): "hub://guardrails/test-validator", install_local_models=None, quiet=False, + upgrade=False, install_local_models_confirm=ANY, ) @@ -77,6 +80,7 @@ def test_install_quiet(self, mocker): "hub://guardrails/test-validator", install_local_models=None, quiet=True, + upgrade=False, install_local_models_confirm=ANY, )