Skip to content

Commit 94d1069

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

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/pip/_internal/commands/cache.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,34 @@ def remove_cache_items(self, options, args):
179179

180180
files = self._find_wheels(options, args[0])
181181

182-
# Only fetch http files if no specific pattern given
183-
if args[0] == '*':
184-
files += self._find_http_files(options)
185-
186182
if not files:
187183
raise CommandError('No matching packages')
188184

189185
for filename in files:
190186
os.unlink(filename)
191187
logger.debug('Removed %s', filename)
188+
189+
dirs = filesystem.list_empty_subdirs(self._cache_dir(options, 'http')) + \
190+
filesystem.list_empty_subdirs(self._cache_dir(options, 'wheels'))
191+
for dirname in dirs:
192+
os.rmdir(dirname)
193+
192194
logger.info('Files removed: %s', len(files))
195+
logger.info('Empty directories removed: %s', len(dirs))
193196

194197
def purge_cache(self, options, args):
195198
# type: (Values, List[Any]) -> None
196199
if args:
197200
raise CommandError('Too many arguments')
198201

199-
return self.remove_cache_items(options, ['*'])
202+
# Remove everything in the "http" and "wheels" cache directories.
203+
filesystem.remove_subdirs(self._cache_dir(options, 'http'))
204+
filesystem.remove_subdirs(self._cache_dir(options, 'wheels'))
205+
206+
# selfcheck.json is no longer used by pip.
207+
selfcheck_json = self._cache_dir(options, 'selfcheck.json')
208+
if os.path.isfile(selfcheck_json):
209+
os.remove(selfcheck_json)
200210

201211
def _cache_dir(self, options, subdir):
202212
# type: (Values, str) -> str

src/pip/_internal/utils/filesystem.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,21 @@ def directory_size(path):
198198
def format_directory_size(path):
199199
# type: (str) -> str
200200
return format_size(directory_size(path))
201+
202+
203+
def list_empty_subdirs(path):
204+
# type: (str) -> List[str]
205+
"""Returns a list of absolute paths of empty directories beneath path.
206+
"""
207+
result = [] # type: List[str]
208+
for root, dirs, _files in os.walk(path, topdown=False):
209+
result.extend(os.path.join(root, d) for d in dirs)
210+
return result
211+
212+
213+
def remove_subdirs(path):
214+
# type: (str) -> None
215+
"""Removes all subdirectories under path."""
216+
for entry in os.scandir(path):
217+
if entry.is_dir():
218+
shutil.rmtree(entry.path)

0 commit comments

Comments
 (0)