Skip to content

Commit

Permalink
Return file size along with count on cache purge or removal (#13108)
Browse files Browse the repository at this point in the history
  • Loading branch information
charwick authored Dec 12, 2024
1 parent 947917b commit 8936fee
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
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

0 comments on commit 8936fee

Please sign in to comment.