Skip to content
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

refactor: unify batch behavior of "install-all", "reinstall-all" and "upgrade-all" #1357

Merged
merged 4 commits into from
Apr 23, 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
2 changes: 1 addition & 1 deletion src/pipx/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def install_all(
else:
installed.append(venv_dir.name)
if len(installed) == 0:
print(f"{sleep} No packages installed after running 'pipx install-all {spec_metadata_file}'")
print(f"No packages installed after running 'pipx install-all {spec_metadata_file}' {sleep}")
dukecat0 marked this conversation as resolved.
Show resolved Hide resolved
if len(failed) > 0:
raise PipxError(f"The following package(s) failed to install: {', '.join(failed)}")
# Any failure to install will raise PipxError, otherwise success
Expand Down
8 changes: 5 additions & 3 deletions src/pipx/commands/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def reinstall_all(
) -> ExitCode:
"""Returns pipx exit code."""
failed: List[str] = []
reinstalled: List[str] = []

# iterate on all packages and reinstall them
# for the first one, we also trigger
Expand All @@ -120,7 +121,7 @@ def reinstall_all(
if venv_dir.name in skip:
continue
try:
package_exit = reinstall(
reinstall(
venv_dir=venv_dir,
local_bin_dir=local_bin_dir,
local_man_dir=local_man_dir,
Expand All @@ -134,8 +135,9 @@ def reinstall_all(
failed.append(venv_dir.name)
else:
first_reinstall = False
if package_exit != 0:
failed.append(venv_dir.name)
reinstalled.append(venv_dir.name)
if len(reinstalled) == 0:
print(f"No packages reinstalled after running 'pipx reinstall-all' {sleep}")
if len(failed) > 0:
raise PipxError(f"The following package(s) failed to reinstall: {', '.join(failed)}")
# Any failure to install will raise PipxError, otherwise success
Expand Down
30 changes: 14 additions & 16 deletions src/pipx/commands/upgrade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import sys
from pathlib import Path
from typing import Dict, List, Optional, Sequence

Expand Down Expand Up @@ -220,15 +221,16 @@ def upgrade_all(
python_flag_passed: bool = False,
) -> ExitCode:
"""Returns pipx exit code."""
venv_error = False
venvs_upgraded = 0
failed: List[str] = []
upgraded: List[str] = []

for venv_dir in venv_container.iter_venv_dirs():
venv = Venv(venv_dir, verbose=verbose)
venv.check_upgrade_shared_libs(pip_args=pip_args, verbose=verbose)
if venv_dir.name in skip or "--editable" in venv.pipx_metadata.main_package.pip_args:
continue
try:
venvs_upgraded += _upgrade_venv(
_upgrade_venv(
venv_dir,
venv.pipx_metadata.main_package.pip_args,
verbose=verbose,
Expand All @@ -237,18 +239,14 @@ def upgrade_all(
force=force,
python_flag_passed=python_flag_passed,
)

except PipxError as e:
venv_error = True
logger.error(f"Error encountered when upgrading {venv_dir.name}:")
logger.error(f"{e}\n")

if venvs_upgraded == 0:
print(f"Versions did not change after running 'pipx upgrade' for each package {sleep}")
if venv_error:
raise PipxError(
"\nSome packages encountered errors during upgrade.\n" " See specific error messages above.",
wrap_message=False,
)

print(e, file=sys.stderr)
failed.append(venv_dir.name)
else:
upgraded.append(venv_dir.name)
if len(upgraded) == 0:
print(f"No packages upgraded after running 'pipx upgrade-all' {sleep}")
if len(failed) > 0:
raise PipxError(f"The following package(s) failed to upgrade: {','.join(failed)}")
# Any failure to install will raise PipxError, otherwise success
return EXIT_CODE_OK
6 changes: 6 additions & 0 deletions tests/test_reinstall_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_reinstall_all(pipx_temp_env, capsys):
assert not run_pipx_cli(["reinstall-all", "--python", sys.executable])


def test_reinstall_all_none(pipx_temp_env, capsys):
assert not run_pipx_cli(["reinstall-all"])
captured = capsys.readouterr()
assert "No packages reinstalled after running 'pipx reinstall-all'" in captured.out


@pytest.mark.parametrize("metadata_version", PIPX_METADATA_LEGACY_VERSIONS)
def test_reinstall_all_legacy_venv(pipx_temp_env, capsys, metadata_version):
assert not run_pipx_cli(["install", "pycowsay"])
Expand Down
8 changes: 7 additions & 1 deletion tests/test_upgrade_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def test_upgrade_all(pipx_temp_env, capsys):
assert not run_pipx_cli(["upgrade-all"])


def test_upgrade_all_none(pipx_temp_env, capsys):
assert not run_pipx_cli(["upgrade-all"])
captured = capsys.readouterr()
assert "No packages upgraded after running 'pipx upgrade-all'" in captured.out


@pytest.mark.parametrize("metadata_version", PIPX_METADATA_LEGACY_VERSIONS)
def test_upgrade_all_legacy_venv(pipx_temp_env, capsys, caplog, metadata_version):
assert run_pipx_cli(["upgrade", "pycowsay"])
Expand All @@ -17,6 +23,6 @@ def test_upgrade_all_legacy_venv(pipx_temp_env, capsys, caplog, metadata_version
if metadata_version is None:
capsys.readouterr()
assert run_pipx_cli(["upgrade-all"])
assert "Error encountered when upgrading pycowsay" in caplog.text
assert "The following package(s) failed to upgrade: pycowsay" in caplog.text
else:
assert not run_pipx_cli(["upgrade-all"])