Skip to content

Commit f417270

Browse files
committed
[commands/cache] make pip cache purge remove everything from http + wheels caches; make pip cache remove prune empty directories.
1 parent c247ddc commit f417270

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/pip/_internal/commands/cache.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import os
23
import textwrap
34
from optparse import Values
@@ -184,7 +185,23 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
184185
for filename in files:
185186
os.unlink(filename)
186187
logger.verbose("Removed %s", filename)
188+
189+
http_dirs = filesystem.subdirs_with_no_files(self._cache_dir(options, "http"))
190+
wheel_dirs = filesystem.subdirs_with_no_files(
191+
self._cache_dir(options, "wheels")
192+
)
193+
for dirname in itertools.chain(http_dirs, wheel_dirs):
194+
os.rmdir(dirname)
195+
logger.verbose("Removed %s", dirname)
196+
197+
# selfcheck.json is no longer used by pip.
198+
selfcheck_json = self._cache_dir(options, "selfcheck.json")
199+
if os.path.isfile(selfcheck_json):
200+
os.remove(selfcheck_json)
201+
logger.verbose("Removed legacy selfcheck.json file")
202+
187203
logger.info("Files removed: %s", len(files))
204+
logger.info("Empty directories removed: %s", len(http_dirs) + len(wheel_dirs))
188205

189206
def purge_cache(self, options: Values, args: List[Any]) -> None:
190207
if args:

src/pip/_internal/utils/filesystem.py

+52
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import random
55
import sys
66
from contextlib import contextmanager
7+
from pathlib import Path
78
from tempfile import NamedTemporaryFile
89
from typing import Any, BinaryIO, Generator, List, Union, cast
910

1011
from pip._vendor.tenacity import retry, stop_after_delay, wait_fixed
1112

13+
from pip._internal.exceptions import PipError
1214
from pip._internal.utils.compat import get_path_uid
1315
from pip._internal.utils.misc import format_size
1416

@@ -151,3 +153,53 @@ def directory_size(path: str) -> Union[int, float]:
151153

152154
def format_directory_size(path: str) -> str:
153155
return format_size(directory_size(path))
156+
157+
158+
def _leaf_subdirs(path: Path) -> Generator[Path, None, None]:
159+
"""Traverses the file tree, finding every empty directory."""
160+
161+
path_obj = Path(path)
162+
163+
if not path_obj.exists():
164+
return
165+
166+
for item in path_obj.iterdir():
167+
if not item.is_dir():
168+
continue
169+
170+
subitems = item.iterdir()
171+
172+
# ASSUMPTION: Nothing in subitems will be None or False.
173+
if not any(subitems):
174+
yield item
175+
176+
if not any(subitem.is_file() for subitem in subitems):
177+
yield from _leaf_subdirs(item)
178+
179+
180+
def _leaf_parents_without_files(path: Path, leaf: Path) -> Generator[str, None, None]:
181+
"""Yields +leaf+ and each parent directory below +path+, until one of
182+
them includes a file (as opposed to directories or nothing)."""
183+
184+
if not str(leaf).startswith(str(path)):
185+
# If +leaf+ is not a subdirectory of +path+, bail early to avoid
186+
# an endless loop.
187+
raise PipError("leaf is not a subdirectory of path")
188+
189+
path = Path(path)
190+
leaf = Path(leaf)
191+
while leaf != path:
192+
if all(item.is_dir() for item in leaf.iterdir()):
193+
yield str(leaf)
194+
else:
195+
break
196+
leaf = leaf.parent
197+
198+
199+
def subdirs_with_no_files(path_str: str) -> Generator[str, None, None]:
200+
"""Yields every subdirectory of +path_str+ that has no files under it."""
201+
202+
path = Path(path_str)
203+
204+
for leaf in _leaf_subdirs(path):
205+
yield from _leaf_parents_without_files(path, leaf)

0 commit comments

Comments
 (0)