mirror of https://github.com/pypa/pip
Use format options for abspath
This commit is contained in:
parent
b76a4c0153
commit
9450f8837a
|
@ -37,18 +37,22 @@ class CacheCommand(Command):
|
||||||
usage = """
|
usage = """
|
||||||
%prog dir
|
%prog dir
|
||||||
%prog info
|
%prog info
|
||||||
%prog list [<pattern>] [--abspath]
|
%prog list [<pattern>] [--format=[human, abspath]]
|
||||||
%prog remove <pattern>
|
%prog remove <pattern>
|
||||||
%prog purge
|
%prog purge
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def add_options(self):
|
def add_options(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
|
|
||||||
self.cmd_opts.add_option(
|
self.cmd_opts.add_option(
|
||||||
'--abspath',
|
'--format',
|
||||||
dest='abspath',
|
action='store',
|
||||||
action='store_true',
|
dest='list_format',
|
||||||
help='List the absolute path of wheels')
|
default="human",
|
||||||
|
choices=('human', 'abspath'),
|
||||||
|
help="Select the output format among: human (default) or abspath"
|
||||||
|
)
|
||||||
|
|
||||||
self.parser.insert_option_group(0, self.cmd_opts)
|
self.parser.insert_option_group(0, self.cmd_opts)
|
||||||
|
|
||||||
|
@ -126,22 +130,34 @@ class CacheCommand(Command):
|
||||||
pattern = '*'
|
pattern = '*'
|
||||||
|
|
||||||
files = self._find_wheels(options, pattern)
|
files = self._find_wheels(options, pattern)
|
||||||
|
if options.list_format == 'human':
|
||||||
|
self.format_for_human(files)
|
||||||
|
else:
|
||||||
|
self.format_for_abspath(files)
|
||||||
|
|
||||||
|
def format_for_human(self, files):
|
||||||
|
# type: (List[str]) -> None
|
||||||
if not files:
|
if not files:
|
||||||
if not options.abspath:
|
logger.info('Nothing cached.')
|
||||||
logger.info('Nothing cached.')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for filename in files:
|
for filename in files:
|
||||||
wheel = os.path.basename(filename)
|
wheel = os.path.basename(filename)
|
||||||
size = filesystem.format_file_size(filename)
|
size = filesystem.format_file_size(filename)
|
||||||
if options.abspath:
|
results.append(' - {} ({})'.format(wheel, size))
|
||||||
results.append(filename)
|
logger.info('Cache contents:\n')
|
||||||
else:
|
logger.info('\n'.join(sorted(results)))
|
||||||
results.append(' - {} ({})'.format(wheel, size))
|
|
||||||
if not options.abspath:
|
def format_for_abspath(self, files):
|
||||||
logger.info('Cache contents:\n')
|
# type: (List[str]) -> None
|
||||||
|
if not files:
|
||||||
|
return
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for filename in files:
|
||||||
|
results.append(filename)
|
||||||
|
|
||||||
logger.info('\n'.join(sorted(results)))
|
logger.info('\n'.join(sorted(results)))
|
||||||
|
|
||||||
def remove_cache_items(self, options, args):
|
def remove_cache_items(self, options, args):
|
||||||
|
|
|
@ -72,7 +72,7 @@ def list_matches_wheel(wheel_name, result):
|
||||||
|
|
||||||
def list_matches_wheel_abspath(wheel_name, result):
|
def list_matches_wheel_abspath(wheel_name, result):
|
||||||
"""Returns True if any line in `result`, which should be the output of
|
"""Returns True if any line in `result`, which should be the output of
|
||||||
a `pip cache list --abspath` call, is a valid path and belongs to
|
a `pip cache list --format=abspath` call, is a valid path and belongs to
|
||||||
`wheel_name`.
|
`wheel_name`.
|
||||||
|
|
||||||
E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
|
E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
|
||||||
|
@ -147,9 +147,9 @@ def test_cache_list(script):
|
||||||
|
|
||||||
@pytest.mark.usefixtures("populate_wheel_cache")
|
@pytest.mark.usefixtures("populate_wheel_cache")
|
||||||
def test_cache_list_abspath(script):
|
def test_cache_list_abspath(script):
|
||||||
"""Running `pip cache list --abspath` should return full
|
"""Running `pip cache list --format=abspath` should return full
|
||||||
paths of exactly what the populate_wheel_cache fixture adds."""
|
paths of exactly what the populate_wheel_cache fixture adds."""
|
||||||
result = script.pip('cache', 'list', '--abspath')
|
result = script.pip('cache', 'list', '--format=abspath')
|
||||||
|
|
||||||
assert list_matches_wheel_abspath('yyy-1.2.3', result)
|
assert list_matches_wheel_abspath('yyy-1.2.3', result)
|
||||||
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
||||||
|
@ -167,9 +167,9 @@ def test_cache_list_with_empty_cache(script):
|
||||||
|
|
||||||
@pytest.mark.usefixtures("empty_wheel_cache")
|
@pytest.mark.usefixtures("empty_wheel_cache")
|
||||||
def test_cache_list_with_empty_cache_abspath(script):
|
def test_cache_list_with_empty_cache_abspath(script):
|
||||||
"""Running `pip cache list --abspath` with an empty cache should not
|
"""Running `pip cache list --format=abspath` with an empty cache should not
|
||||||
print anything and exit."""
|
print anything and exit."""
|
||||||
result = script.pip('cache', 'list', '--abspath')
|
result = script.pip('cache', 'list', '--format=abspath')
|
||||||
assert result.stdout.strip() == ""
|
assert result.stdout.strip() == ""
|
||||||
|
|
||||||
|
|
||||||
|
@ -193,9 +193,10 @@ def test_cache_list_name_match(script):
|
||||||
|
|
||||||
@pytest.mark.usefixtures("populate_wheel_cache")
|
@pytest.mark.usefixtures("populate_wheel_cache")
|
||||||
def test_cache_list_name_match_abspath(script):
|
def test_cache_list_name_match_abspath(script):
|
||||||
"""Running `pip cache list zzz --abspath` should list paths of
|
"""Running `pip cache list zzz --format=abspath` should list paths of
|
||||||
zzz-4.5.6, zzz-4.5.7, zzz-7.8.9, but nothing else."""
|
zzz-4.5.6, zzz-4.5.7, zzz-7.8.9, but nothing else."""
|
||||||
result = script.pip('cache', 'list', 'zzz', '--abspath', '--verbose')
|
result = script.pip('cache', 'list', 'zzz', '--format=abspath',
|
||||||
|
'--verbose')
|
||||||
|
|
||||||
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
|
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
|
||||||
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
||||||
|
@ -217,9 +218,10 @@ def test_cache_list_name_and_version_match(script):
|
||||||
|
|
||||||
@pytest.mark.usefixtures("populate_wheel_cache")
|
@pytest.mark.usefixtures("populate_wheel_cache")
|
||||||
def test_cache_list_name_and_version_match_abspath(script):
|
def test_cache_list_name_and_version_match_abspath(script):
|
||||||
"""Running `pip cache list zzz-4.5.6 --abspath` should list path of
|
"""Running `pip cache list zzz-4.5.6 --format=abspath` should list path of
|
||||||
zzz-4.5.6, but nothing else."""
|
zzz-4.5.6, but nothing else."""
|
||||||
result = script.pip('cache', 'list', 'zzz-4.5.6', '--abspath', '--verbose')
|
result = script.pip('cache', 'list', 'zzz-4.5.6', '--format=abspath',
|
||||||
|
'--verbose')
|
||||||
|
|
||||||
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
|
assert not list_matches_wheel_abspath('yyy-1.2.3', result)
|
||||||
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
assert list_matches_wheel_abspath('zzz-4.5.6', result)
|
||||||
|
|
Loading…
Reference in New Issue