Skip to content

Commit 226d1c2

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

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
@@ -181,24 +181,34 @@ def remove_cache_items(self, options, args):
181181

182182
files = self._find_wheels(options, args[0])
183183

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

191187
for filename in files:
192188
os.unlink(filename)
193189
logger.debug('Removed %s', filename)
190+
191+
dirs = filesystem.list_empty_subdirs(self._cache_dir(options, 'http')) + \
192+
filesystem.list_empty_subdirs(self._cache_dir(options, 'wheels'))
193+
for dirname in dirs:
194+
os.rmdir(dirname)
195+
194196
logger.info('Files removed: %s', len(files))
197+
logger.info('Empty directories removed: %s', len(dirs))
195198

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

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

203213
def _cache_dir(self, options, subdir):
204214
# type: (Values, str) -> str

src/pip/_internal/utils/filesystem.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,21 @@ def directory_size(path):
222222
def format_directory_size(path):
223223
# type: (str) -> str
224224
return format_size(directory_size(path))
225+
226+
227+
def list_empty_subdirs(path):
228+
# type: (str) -> List[str]
229+
"""Returns a list of absolute paths of empty directories beneath path.
230+
"""
231+
result = [] # type: List[str]
232+
for root, dirs, _files in os.walk(path, topdown=False):
233+
result.extend(os.path.join(root, d) for d in dirs)
234+
return result
235+
236+
237+
def remove_subdirs(path):
238+
# type: (str) -> None
239+
"""Removes all subdirectories under path."""
240+
for entry in os.scandir(path):
241+
if entry.is_dir():
242+
shutil.rmtree(entry.path)

0 commit comments

Comments
 (0)