Skip to content

Fix quiet install #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions guardrails/cli/hub/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
46 changes: 23 additions & 23 deletions guardrails/cli/hub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down