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..16f265394 100644 --- a/guardrails/cli/hub/utils.py +++ b/guardrails/cli/hub/utils.py @@ -19,42 +19,42 @@ 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}") - 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) - 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: - 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()) + if not quiet: + logger.debug( + 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 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}\nExit code: {exc.returncode}\nstdout: {exc.output.decode()}" ) - ) 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 trying to {action} {package}: {str(e)}" + ) sys.exit(1)