Skip to content

Commit c75cae3

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

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/pip/_internal/commands/cache.py

+14
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,21 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
180180
for filename in files:
181181
os.unlink(filename)
182182
logger.verbose("Removed %s", filename)
183+
184+
dirs = filesystem.list_empty_subdirs(
185+
self._cache_dir(options, "http")
186+
) + filesystem.list_empty_subdirs(self._cache_dir(options, "wheels"))
187+
for dirname in dirs:
188+
os.rmdir(dirname)
189+
190+
# selfcheck.json is no longer used by pip.
191+
selfcheck_json = self._cache_dir(options, "selfcheck.json")
192+
if os.path.isfile(selfcheck_json):
193+
os.remove(selfcheck_json)
194+
logger.verbose("Removed legacy selfcheck.json file")
195+
183196
logger.info("Files removed: %s", len(files))
197+
logger.info("Empty directories removed: %s", len(dirs))
184198

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

src/pip/_internal/utils/filesystem.py

+17
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,20 @@ def directory_size(path: str) -> Union[int, float]:
180180

181181
def format_directory_size(path: str) -> str:
182182
return format_size(directory_size(path))
183+
184+
185+
def list_empty_subdirs(path):
186+
# type: (str) -> List[str]
187+
"""Returns a list of absolute paths of empty directories beneath path."""
188+
result = [] # type: List[str]
189+
for root, dirs, _files in os.walk(path, topdown=False):
190+
result.extend(os.path.join(root, d) for d in dirs)
191+
return result
192+
193+
194+
def remove_subdirs(path):
195+
# type: (str) -> None
196+
"""Removes all subdirectories under path."""
197+
for entry in os.scandir(path):
198+
if entry.is_dir():
199+
shutil.rmtree(entry.path)

0 commit comments

Comments
 (0)