Skip to content

Commit 545dcc5

Browse files
committed
Use format options for abspath
1 parent 8962162 commit 545dcc5

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/pip/_internal/commands/cache.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,22 @@ class CacheCommand(Command):
3737
usage = """
3838
%prog dir
3939
%prog info
40-
%prog list [<pattern>] [--abspath]
40+
%prog list [<pattern>] [--format=[human, abspath]]
4141
%prog remove <pattern>
4242
%prog purge
4343
"""
4444

4545
def add_options(self):
4646
# type: () -> None
47+
4748
self.cmd_opts.add_option(
48-
'--abspath',
49-
dest='abspath',
50-
action='store_true',
51-
help='List the absolute path of wheels')
49+
'--format',
50+
action='store',
51+
dest='list_format',
52+
default="human",
53+
choices=('human', 'abspath'),
54+
help="Select the output format among: human (default) or abspath"
55+
)
5256

5357
self.parser.insert_option_group(0, self.cmd_opts)
5458

@@ -126,22 +130,34 @@ def list_cache_items(self, options, args):
126130
pattern = '*'
127131

128132
files = self._find_wheels(options, pattern)
133+
if options.list_format == 'human':
134+
self.format_for_human(files)
135+
else:
136+
self.format_for_abspath(files)
129137

138+
def format_for_human(self, files):
139+
# type: (List[str]) -> None
130140
if not files:
131-
if not options.abspath:
132-
logger.info('Nothing cached.')
141+
logger.info('Nothing cached.')
133142
return
134143

135144
results = []
136145
for filename in files:
137146
wheel = os.path.basename(filename)
138147
size = filesystem.format_file_size(filename)
139-
if options.abspath:
140-
results.append(filename)
141-
else:
142-
results.append(' - {} ({})'.format(wheel, size))
143-
if not options.abspath:
144-
logger.info('Cache contents:\n')
148+
results.append(' - {} ({})'.format(wheel, size))
149+
logger.info('Cache contents:\n')
150+
logger.info('\n'.join(sorted(results)))
151+
152+
def format_for_abspath(self, files):
153+
# type: (List[str]) -> None
154+
if not files:
155+
return
156+
157+
results = []
158+
for filename in files:
159+
results.append(filename)
160+
145161
logger.info('\n'.join(sorted(results)))
146162

147163
def remove_cache_items(self, options, args):

tests/functional/test_cache.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def test_cache_list(script):
149149
def test_cache_list_abspath(script):
150150
"""Running `pip cache list --abspath` should return full
151151
paths of exactly what the populate_wheel_cache fixture adds."""
152-
result = script.pip('cache', 'list', '--abspath')
152+
result = script.pip('cache', 'list', '--format=abspath')
153153

154154
assert list_matches_wheel_abspath('yyy-1.2.3', result)
155155
assert list_matches_wheel_abspath('zzz-4.5.6', result)
@@ -169,7 +169,7 @@ def test_cache_list_with_empty_cache(script):
169169
def test_cache_list_with_empty_cache_abspath(script):
170170
"""Running `pip cache list --abspath` with an empty cache should not
171171
print anything and exit."""
172-
result = script.pip('cache', 'list', '--abspath')
172+
result = script.pip('cache', 'list', '--format=abspath')
173173
assert result.stdout.strip() == ""
174174

175175

@@ -195,7 +195,8 @@ def test_cache_list_name_match(script):
195195
def test_cache_list_name_match_abspath(script):
196196
"""Running `pip cache list zzz --abspath` should list paths of
197197
zzz-4.5.6, zzz-4.5.7, zzz-7.8.9, but nothing else."""
198-
result = script.pip('cache', 'list', 'zzz', '--abspath', '--verbose')
198+
result = script.pip('cache', 'list', 'zzz', '--format=abspath',
199+
'--verbose')
199200

200201
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
201202
assert list_matches_wheel_abspath('zzz-4.5.6', result)
@@ -219,7 +220,8 @@ def test_cache_list_name_and_version_match(script):
219220
def test_cache_list_name_and_version_match_abspath(script):
220221
"""Running `pip cache list zzz-4.5.6 --abspath` should list path of
221222
zzz-4.5.6, but nothing else."""
222-
result = script.pip('cache', 'list', 'zzz-4.5.6', '--abspath', '--verbose')
223+
result = script.pip('cache', 'list', 'zzz-4.5.6', '--format=abspath',
224+
'--verbose')
223225

224226
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
225227
assert list_matches_wheel_abspath('zzz-4.5.6', result)

0 commit comments

Comments
 (0)