From 82d0e5cff7eb573b5efd4353945829c24f5fa4b8 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Sun, 19 May 2024 02:37:32 -0700 Subject: [PATCH 1/2] fix pip process so quiet install works --- guardrails/cli/hub/install.py | 20 +++++++++++++------- guardrails/cli/hub/utils.py | 31 ++++++++++++++++++------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/guardrails/cli/hub/install.py b/guardrails/cli/hub/install.py index 147e1a568..ffd74eb18 100644 --- a/guardrails/cli/hub/install.py +++ b/guardrails/cli/hub/install.py @@ -134,19 +134,24 @@ def get_install_url(manifest: ModuleManifest) -> str: return git_url -def install_hub_module(module_manifest: ModuleManifest, site_packages: str): +def install_hub_module(module_manifest: ModuleManifest, site_packages: str, quiet: bool): install_url = get_install_url(module_manifest) install_directory = get_hub_directory(module_manifest, site_packages) + pip_flags = [f"--target={install_directory}", "--no-deps"] + if quiet: + pip_flags.append("-q") + # Install validator module in namespaced directory under guardrails.hub download_output = pip_process( - "install", install_url, [f"--target={install_directory}", "--no-deps", "-q"] + "install", install_url, pip_flags, quiet=quiet ) - logger.info(download_output) + if not quiet: + logger.info(download_output) # Install validator module's dependencies in normal site-packages directory inspect_output = pip_process( - "inspect", flags=[f"--path={install_directory}"], format=json_format + "inspect", flags=[f"--path={install_directory}"], format=json_format, quiet=quiet ) # throw if inspect_output is a string. Mostly for pyright @@ -167,8 +172,9 @@ def install_hub_module(module_manifest: ModuleManifest, site_packages: str): versions = req_info.at(1, "").strip("()") # type: ignore if name: install_spec = name if not versions else f"{name}{versions}" - dep_install_output = pip_process("install", install_spec) - logger.info(dep_install_output) + dep_install_output = pip_process("install", install_spec, quiet=quiet) + if not quiet: + logger.info(dep_install_output) @hub_command.command() @@ -204,7 +210,7 @@ def install( with console.status("Downloading dependencies", spinner="bouncingBar") as status: if not quiet: status.update("Downloading dependencies") - install_hub_module(module_manifest, site_packages) + install_hub_module(module_manifest, site_packages, quiet=quiet) # Post-install with console.status("Running post-install setup", spinner="bouncingBar") as status: diff --git a/guardrails/cli/hub/utils.py b/guardrails/cli/hub/utils.py index 63404eb6c..a72e5f94f 100644 --- a/guardrails/cli/hub/utils.py +++ b/guardrails/cli/hub/utils.py @@ -19,6 +19,7 @@ def pip_process( package: str = "", flags: List[str] = [], format: Union[Literal["string"], Literal["json"]] = string_format, + quiet: bool = False, ) -> Union[str, dict]: try: logger.debug(f"running pip {action} {' '.join(flags)} {package}") @@ -27,33 +28,37 @@ def pip_process( if package: command.append(package) output = subprocess.check_output(command) - logger.debug(f"decoding output from pip {action} {package}") + if not quiet: + logger.debug(f"decoding output from pip {action} {package}") if format == json_format: parsed = BytesHeaderParser().parsebytes(output) try: return json.loads(str(parsed)) except Exception: - logger.debug( - f"json parse exception in decoding output from pip {action} {package}. Falling back to accumulating the byte stream", # noqa - ) + if not quiet: + logger.debug( + f"json parse exception in decoding output from pip {action} {package}. Falling back to accumulating the byte stream", # noqa + ) accumulator = {} for key, value in parsed.items(): accumulator[key] = value return accumulator return str(output.decode()) except subprocess.CalledProcessError as exc: - logger.error( - ( - f"Failed to {action} {package}\n" - f"Exit code: {exc.returncode}\n" - f"stdout: {exc.output}" + if not quiet: + logger.error( + ( + f"Failed to {action} {package}\n" + f"Exit code: {exc.returncode}\n" + f"stdout: {exc.output}" + ) ) - ) sys.exit(1) except Exception as e: - logger.error( - f"An unexpected exception occurred while try to {action} {package}!", - e, + if not quiet: + logger.error( + f"An unexpected exception occurred while try to {action} {package}!", + e, ) sys.exit(1) From d761c2b7b3af9cde34307fe4fa70f5ef7cd402ea Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Mon, 20 May 2024 01:36:43 -0700 Subject: [PATCH 2/2] remove warnings --- guardrails/cli/hub/utils.py | 39 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/guardrails/cli/hub/utils.py b/guardrails/cli/hub/utils.py index a72e5f94f..16f265394 100644 --- a/guardrails/cli/hub/utils.py +++ b/guardrails/cli/hub/utils.py @@ -19,47 +19,42 @@ def pip_process( package: str = "", flags: List[str] = [], format: Union[Literal["string"], Literal["json"]] = string_format, - quiet: bool = False, + quiet: bool = False ) -> Union[str, dict]: try: - logger.debug(f"running pip {action} {' '.join(flags)} {package}") - command = [sys.executable, "-m", "pip", action] - command.extend(flags) + command = [sys.executable, "-m", "pip", action] + flags if package: command.append(package) - output = subprocess.check_output(command) - if not quiet: - logger.debug(f"decoding output from pip {action} {package}") + + stderr = subprocess.DEVNULL if quiet else None + + output = subprocess.check_output(command, stderr=stderr) + if format == json_format: parsed = BytesHeaderParser().parsebytes(output) try: return json.loads(str(parsed)) except Exception: - if not quiet: + if not quiet: logger.debug( - f"json parse exception in decoding output from pip {action} {package}. Falling back to accumulating the byte stream", # noqa + f"JSON parse exception in decoding output from pip {action} {package}. Falling back to accumulating the byte stream", ) - accumulator = {} - for key, value in parsed.items(): - accumulator[key] = value - return accumulator - return str(output.decode()) + accumulator = {} + for key, value in parsed.items(): + accumulator[key] = value + return accumulator + return output.decode() except subprocess.CalledProcessError as exc: if not quiet: logger.error( - ( - f"Failed to {action} {package}\n" - f"Exit code: {exc.returncode}\n" - f"stdout: {exc.output}" - ) + f"Failed to {action} {package}\nExit code: {exc.returncode}\nstdout: {exc.output.decode()}" ) sys.exit(1) except Exception as e: if not quiet: logger.error( - f"An unexpected exception occurred while try to {action} {package}!", - e, - ) + f"An unexpected exception occurred while trying to {action} {package}: {str(e)}" + ) sys.exit(1)