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

Allow skipping maintenance tasks for list command #1163

Merged
merged 6 commits into from
Jan 4, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## dev

- Allow skipping maintenance tasks during list command

## 1.4.1

- Set default logging level to WARNING, so debug log messages won't be shown without passing additional flags such as `--verbose`
Expand Down
7 changes: 6 additions & 1 deletion src/pipx/commands/list_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Any, Collection, Dict, Tuple

from pipx import constants
from pipx import constants, shared_libs
from pipx.colors import bold
from pipx.commands.common import VenvProblems, get_venv_summary, venv_health_check
from pipx.constants import EXIT_CODE_LIST_PROBLEM, EXIT_CODE_OK, ExitCode
Expand Down Expand Up @@ -89,12 +89,17 @@ def list_packages(
include_injected: bool,
json_format: bool,
short_format: bool,
skip_maintenance: bool,
) -> ExitCode:
"""Returns pipx exit code."""
venv_dirs: Collection[Path] = sorted(venv_container.iter_venv_dirs())
if not venv_dirs:
print(f"nothing has been installed with pipx {sleep}", file=sys.stderr)

if skip_maintenance:
shared_libs.shared_libs.skip_upgrade = True
logger.info("Skipping shared libs maintenance tasks")

venv_container.verify_shared_libs()

if json_format:
Expand Down
5 changes: 4 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ def run_pipx_command(args: argparse.Namespace) -> ExitCode: # noqa: C901
force=args.force,
)
elif args.command == "list":
return commands.list_packages(venv_container, args.include_injected, args.json, args.short)
return commands.list_packages(
venv_container, args.include_injected, args.json, args.short, args.skip_maintenance
)
elif args.command == "uninstall":
return commands.uninstall(venv_dir, constants.LOCAL_BIN_DIR, constants.LOCAL_MAN_DIR, verbose)
elif args.command == "uninstall-all":
Expand Down Expand Up @@ -566,6 +568,7 @@ def _add_list(subparsers: argparse._SubParsersAction, shared_parser: argparse.Ar
g = p.add_mutually_exclusive_group()
g.add_argument("--json", action="store_true", help="Output rich data in json format.")
g.add_argument("--short", action="store_true", help="List packages only.")
g.add_argument("--skip-maintenance", action="store_true", help="Skip maintenance tasks.")


def _add_run(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/pipx/shared_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self) -> None:
self._site_packages: Optional[Path] = None
self.has_been_updated_this_run = False
self.has_been_logged_this_run = False
self.skip_upgrade = False

@property
def site_packages(self) -> Path:
Expand Down Expand Up @@ -65,7 +66,7 @@ def is_valid(self) -> bool:

@property
def needs_upgrade(self) -> bool:
if self.has_been_updated_this_run:
if self.has_been_updated_this_run or self.skip_upgrade:
return False

if not self.pip_path.is_file():
Expand Down
28 changes: 27 additions & 1 deletion tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import re
import time

import pytest # type: ignore

Expand All @@ -12,7 +14,7 @@
run_pipx_cli,
)
from package_info import PKG
from pipx import constants
from pipx import constants, shared_libs
from pipx.pipx_metadata_file import PackageInfo, _json_decoder_object_hook


Expand Down Expand Up @@ -128,3 +130,27 @@ def test_list_short(pipx_temp_env, monkeypatch, capsys):

assert "pycowsay 0.0.0.2" in captured.out
assert "pylint 2.3.1" in captured.out


def test_skip_maintenance(pipx_temp_env):
assert not run_pipx_cli(["install", PKG["pycowsay"]["spec"]])
assert not run_pipx_cli(["install", PKG["pylint"]["spec"]])

now = time.time()
shared_libs.shared_libs.create(verbose=True)
shared_libs.shared_libs.has_been_updated_this_run = False

access_time = now # this can be anything
os.utime(shared_libs.shared_libs.pip_path, (access_time, -shared_libs.SHARED_LIBS_MAX_AGE_SEC - 5 * 60 + now))
assert shared_libs.shared_libs.needs_upgrade
run_pipx_cli(["list"])
assert shared_libs.shared_libs.has_been_updated_this_run
assert not shared_libs.shared_libs.needs_upgrade

os.utime(shared_libs.shared_libs.pip_path, (access_time, -shared_libs.SHARED_LIBS_MAX_AGE_SEC - 5 * 60 + now))
shared_libs.shared_libs.has_been_updated_this_run = False
assert shared_libs.shared_libs.needs_upgrade
run_pipx_cli(["list", "--skip-maintenance"])
shared_libs.shared_libs.skip_upgrade = False
assert not shared_libs.shared_libs.has_been_updated_this_run
assert shared_libs.shared_libs.needs_upgrade