Skip to content

Commit

Permalink
[commands/cache] make pip cache purge remove everything from http +…
Browse files Browse the repository at this point in the history
… wheels caches; make `pip cache remove` prune empty directories.
  • Loading branch information
duckinator committed Sep 24, 2021
1 parent b55ec00 commit 11db9a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/pip/_internal/commands/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,21 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
for filename in files:
os.unlink(filename)
logger.verbose("Removed %s", filename)

dirs = filesystem.list_empty_subdirs(
self._cache_dir(options, "http")
) + filesystem.list_empty_subdirs(self._cache_dir(options, "wheels"))
for dirname in dirs:
os.rmdir(dirname)

# selfcheck.json is no longer used by pip.
selfcheck_json = self._cache_dir(options, "selfcheck.json")
if os.path.isfile(selfcheck_json):
os.remove(selfcheck_json)
logger.verbose("Removed legacy selfcheck.json file")

logger.info("Files removed: %s", len(files))
logger.info("Empty directories removed: %s", len(dirs))

def purge_cache(self, options: Values, args: List[Any]) -> None:
if args:
Expand Down
9 changes: 9 additions & 0 deletions src/pip/_internal/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,12 @@ def directory_size(path: str) -> Union[int, float]:

def format_directory_size(path: str) -> str:
return format_size(directory_size(path))


def list_empty_subdirs(path):
# type: (str) -> List[str]
"""Returns a list of absolute paths of empty directories beneath path."""
result = [] # type: List[str]
for root, dirs, _files in os.walk(path, topdown=False):
result.extend(os.path.join(root, d) for d in dirs)
return result

0 comments on commit 11db9a5

Please sign in to comment.