[commands/cache] Add file size information.

This commit is contained in:
Ellen Marie Dash 2020-01-06 15:51:37 -05:00
parent 61a0adcfe7
commit 554133a90e
2 changed files with 65 additions and 10 deletions

View File

@ -4,10 +4,10 @@ import logging
import os
import textwrap
import pip._internal.utils.filesystem as filesystem
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.exceptions import CommandError, PipError
from pip._internal.utils.filesystem import find_files
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
@ -78,13 +78,18 @@ class CacheCommand(Command):
# type: (Values, List[Any]) -> None
num_packages = len(self._find_wheels(options, '*'))
cache_location = self._wheels_cache_dir(options)
cache_size = filesystem.friendly_directory_size(cache_location)
message = textwrap.dedent("""
Cache info:
Location: {location}
Packages: {package_count}
Size: {size}
""").format(
location=self._wheels_cache_dir(options),
location=cache_location,
package_count=num_packages,
size=cache_size,
).strip()
logger.info(message)
@ -100,16 +105,18 @@ class CacheCommand(Command):
pattern = '*'
files = self._find_wheels(options, pattern)
wheels = sorted(set(map(lambda f: os.path.basename(f), files)))
if not wheels:
if not files:
logger.info('Nothing is currently cached.')
return
result = 'Current cache contents:\n'
for wheel in wheels:
result += ' - %s\n' % wheel
logger.info(result.strip())
results = []
for filename in files:
wheel = os.path.basename(filename)
size = filesystem.friendly_file_size(filename)
results.append(' - {} ({})'.format(wheel, size))
logger.info('Current cache contents:\n')
logger.info('\n'.join(sorted(results)))
def remove_cache_items(self, options, args):
# type: (Values, List[Any]) -> None
@ -142,4 +149,4 @@ class CacheCommand(Command):
def _find_wheels(self, options, pattern):
# type: (Values, str) -> List[str]
wheel_dir = self._wheels_cache_dir(options)
return find_files(wheel_dir, pattern + '*.whl')
return filesystem.find_files(wheel_dir, pattern + '*.whl')

View File

@ -18,7 +18,7 @@ from pip._internal.utils.compat import get_path_uid
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
if MYPY_CHECK_RUNNING:
from typing import Any, BinaryIO, Iterator, List
from typing import Any, BinaryIO, Iterator, List, Union
class NamedTemporaryFileResult(BinaryIO):
@property
@ -188,3 +188,51 @@ def find_files(path, pattern):
matches = fnmatch.filter(files, pattern)
result.extend(os.path.join(root, f) for f in matches)
return result
def _friendly_size(size):
# type: (Union[float, int]) -> str
suffix = 'B'
if size > 1000:
size /= 1000
suffix = 'KB'
if size > 1000:
size /= 1000
suffix = 'MB'
if size > 1000:
size /= 1000
suffix = 'GB'
size = round(size, 1)
return '{} {}'.format(size, suffix)
def file_size(path):
# type: (str) -> Union[int, float]
# If it's a symlink, return 0.
if os.path.islink(path):
return 0
return os.path.getsize(path)
def friendly_file_size(path):
# type: (str) -> str
return _friendly_size(file_size(path))
def directory_size(path):
# type: (str) -> Union[int, float]
size = 0.0
for root, _dirs, files in os.walk(path):
for filename in files:
file_path = os.path.join(root, filename)
size += file_size(file_path)
return size
def friendly_directory_size(path):
# type: (str) -> str
return _friendly_size(directory_size(path))