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

Return file size along with count on cache purge or removal #13108

Merged
merged 9 commits into from
Dec 12, 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
1 change: 1 addition & 0 deletions news/12176.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pip now returns the size, along with the number, of files cleared on ``pip cache purge`` and ``pip cache remove``
5 changes: 4 additions & 1 deletion src/pip/_internal/commands/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pip._internal.exceptions import CommandError, PipError
from pip._internal.utils import filesystem
from pip._internal.utils.logging import getLogger
from pip._internal.utils.misc import format_size

logger = getLogger(__name__)

Expand Down Expand Up @@ -180,10 +181,12 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
if not files:
logger.warning(no_matching_msg)

bytes_removed = 0
for filename in files:
bytes_removed += os.stat(filename).st_size
os.unlink(filename)
logger.verbose("Removed %s", filename)
logger.info("Files removed: %s", len(files))
logger.info("Files removed: %s (%s)", len(files), format_size(bytes_removed))

def purge_cache(self, options: Values, args: List[Any]) -> None:
if args:
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_cache_purge_with_empty_cache(script: PipTestEnvironment) -> None:
and exit without an error code."""
result = script.pip("cache", "purge", allow_stderr_warning=True)
assert result.stderr == "WARNING: No matching packages\n"
assert result.stdout == "Files removed: 0\n"
assert result.stdout == "Files removed: 0 (0 bytes)\n"


@pytest.mark.usefixtures("populate_wheel_cache")
Expand All @@ -265,7 +265,7 @@ def test_cache_remove_with_bad_pattern(script: PipTestEnvironment) -> None:
and exit without an error code."""
result = script.pip("cache", "remove", "aaa", allow_stderr_warning=True)
assert result.stderr == 'WARNING: No matching packages for pattern "aaa"\n'
assert result.stdout == "Files removed: 0\n"
assert result.stdout == "Files removed: 0 (0 bytes)\n"


def test_cache_list_too_many_args(script: PipTestEnvironment) -> None:
Expand Down
Loading