Skip to content

Commit bea48bc

Browse files
committed
Add 'pip cache' command.
1 parent 9611394 commit bea48bc

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

src/pip/_internal/commands/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
'pip._internal.commands.search', 'SearchCommand',
6161
'Search PyPI for packages.',
6262
)),
63+
('cache', CommandInfo(
64+
'pip._internal.commands.cache', 'CacheCommand',
65+
"Inspect and manage pip's caches.",
66+
)),
6367
('wheel', CommandInfo(
6468
'pip._internal.commands.wheel', 'WheelCommand',
6569
'Build wheels from your requirements.',

src/pip/_internal/commands/cache.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from __future__ import absolute_import
2+
3+
import logging
4+
import os
5+
import textwrap
6+
7+
from pip._internal.cli.base_command import Command
8+
from pip._internal.exceptions import CommandError
9+
from pip._internal.utils.filesystem import find_files
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
class CacheCommand(Command):
15+
"""
16+
Inspect and manage pip's caches.
17+
18+
Subcommands:
19+
info:
20+
Show information about the caches.
21+
list [name]:
22+
List filenames of packages stored in the cache.
23+
remove <pattern>:
24+
Remove one or more package from the cache.
25+
`pattern` can be a glob expression or a package name.
26+
purge:
27+
Remove all items from the cache.
28+
"""
29+
actions = ['info', 'list', 'remove', 'purge']
30+
name = 'cache'
31+
usage = """
32+
%prog <command>"""
33+
summary = "View and manage which packages are available in pip's caches."
34+
35+
def __init__(self, *args, **kw):
36+
super(CacheCommand, self).__init__(*args, **kw)
37+
38+
def run(self, options, args):
39+
if not args:
40+
raise CommandError('Please provide a subcommand.')
41+
42+
if args[0] not in self.actions:
43+
raise CommandError('Invalid subcommand: %s' % args[0])
44+
45+
self.wheel_dir = os.path.join(options.cache_dir, 'wheels')
46+
47+
method = getattr(self, 'action_%s' % args[0])
48+
return method(options, args[1:])
49+
50+
def action_info(self, options, args):
51+
format_args = (options.cache_dir, len(self.find_wheels('*.whl')))
52+
result = textwrap.dedent(
53+
"""\
54+
Cache info:
55+
Location: %s
56+
Packages: %s""" % format_args
57+
)
58+
logger.info(result)
59+
60+
def action_list(self, options, args):
61+
if args and args[0]:
62+
pattern = args[0]
63+
else:
64+
pattern = '*'
65+
66+
files = self.find_wheels(pattern)
67+
wheels = map(self._wheel_info, files)
68+
wheels = sorted(set(wheels))
69+
70+
if not wheels:
71+
logger.info('Nothing is currently cached.')
72+
return
73+
74+
result = 'Current cache contents:\n'
75+
for wheel in wheels:
76+
result += ' - %s\n' % wheel
77+
logger.info(result.strip())
78+
79+
def action_remove(self, options, args):
80+
if not args:
81+
raise CommandError('Please provide a pattern')
82+
83+
files = self.find_wheels(args[0])
84+
if not files:
85+
raise CommandError('No matching packages')
86+
87+
wheels = map(self._wheel_info, files)
88+
result = 'Removing cached wheels for:\n'
89+
for wheel in wheels:
90+
result += '- %s\n' % wheel
91+
92+
for filename in files:
93+
os.unlink(filename)
94+
logger.info(result.strip())
95+
96+
def action_purge(self, options, args):
97+
return self.action_remove(options, '*')
98+
99+
def _wheel_info(self, path):
100+
filename = os.path.splitext(os.path.basename(path))[0]
101+
name, version = filename.split('-')[0:2]
102+
return '%s-%s' % (name, version)
103+
104+
def find_wheels(self, pattern):
105+
return find_files(self.wheel_dir, pattern + '-*.whl')

src/pip/_internal/utils/filesystem.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fnmatch
12
import os
23
import os.path
34
import shutil
@@ -15,7 +16,7 @@
1516
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1617

1718
if MYPY_CHECK_RUNNING:
18-
from typing import BinaryIO, Iterator
19+
from typing import BinaryIO, Iterator, List
1920

2021
class NamedTemporaryFileResult(BinaryIO):
2122
@property
@@ -113,3 +114,14 @@ def replace(src, dest):
113114

114115
else:
115116
replace = _replace_retry(os.replace)
117+
118+
119+
def find_files(path, pattern):
120+
# type: (str, str) -> List[str]
121+
"""Returns a list of absolute paths of files beneath path, recursively,
122+
with filenames which match the UNIX-style shell glob pattern."""
123+
result = [] # type: List[str]
124+
for root, dirs, files in os.walk(path):
125+
matches = fnmatch.filter(files, pattern)
126+
result.extend(os.path.join(root, f) for f in matches)
127+
return result

tests/functional/test_cache.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import shutil
3+
4+
from pip._internal.utils import appdirs
5+
6+
7+
def test_cache_info(script, monkeypatch):
8+
result = script.pip('cache', 'info')
9+
10+
cache_dir = appdirs.user_cache_dir('pip')
11+
12+
assert 'Location: %s' % cache_dir in result.stdout
13+
assert 'Packages: ' in result.stdout
14+
15+
16+
def test_cache_list(script, monkeypatch):
17+
cache_dir = appdirs.user_cache_dir('pip')
18+
wheel_cache_dir = os.path.join(cache_dir, 'wheels')
19+
destination = os.path.join(wheel_cache_dir, 'arbitrary', 'pathname')
20+
os.makedirs(destination)
21+
with open(os.path.join(destination, 'yyy-1.2.3.whl'), 'w'):
22+
pass
23+
with open(os.path.join(destination, 'zzz-4.5.6.whl'), 'w'):
24+
pass
25+
result = script.pip('cache', 'list')
26+
assert 'yyy-1.2.3' in result.stdout
27+
assert 'zzz-4.5.6' in result.stdout
28+
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))
29+
30+
31+
def test_cache_list_with_pattern(script, monkeypatch):
32+
cache_dir = appdirs.user_cache_dir('pip')
33+
wheel_cache_dir = os.path.join(cache_dir, 'wheels')
34+
destination = os.path.join(wheel_cache_dir, 'arbitrary', 'pathname')
35+
os.makedirs(destination)
36+
with open(os.path.join(destination, 'yyy-1.2.3.whl'), 'w'):
37+
pass
38+
with open(os.path.join(destination, 'zzz-4.5.6.whl'), 'w'):
39+
pass
40+
result = script.pip('cache', 'list', 'zzz')
41+
assert 'yyy-1.2.3' not in result.stdout
42+
assert 'zzz-4.5.6' in result.stdout
43+
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))
44+
45+
46+
def test_cache_remove(script, monkeypatch):
47+
cache_dir = appdirs.user_cache_dir("pip")
48+
wheel_cache_dir = os.path.join(cache_dir, "wheels")
49+
destination = os.path.join(wheel_cache_dir, 'arbitrary', 'pathname')
50+
os.makedirs(destination)
51+
with open(os.path.join(wheel_cache_dir, "yyy-1.2.3.whl"), "w"):
52+
pass
53+
with open(os.path.join(wheel_cache_dir, "zzz-4.5.6.whl"), "w"):
54+
pass
55+
56+
script.pip("cache", "remove", expect_error=True)
57+
result = script.pip("cache", "remove", "zzz")
58+
assert 'yyy-1.2.3' not in result.stdout
59+
assert '- zzz-4.5.6' in result.stdout
60+
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))

0 commit comments

Comments
 (0)