From 554133a90eb9611b3c828b5b65a5f91fc6fe0a01 Mon Sep 17 00:00:00 2001 From: Ellen Marie Dash Date: Mon, 6 Jan 2020 15:51:37 -0500 Subject: [PATCH] [commands/cache] Add file size information. --- src/pip/_internal/commands/cache.py | 25 +++++++++----- src/pip/_internal/utils/filesystem.py | 50 ++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/pip/_internal/commands/cache.py b/src/pip/_internal/commands/cache.py index 722732655..80ea5c8f9 100644 --- a/src/pip/_internal/commands/cache.py +++ b/src/pip/_internal/commands/cache.py @@ -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') diff --git a/src/pip/_internal/utils/filesystem.py b/src/pip/_internal/utils/filesystem.py index 2772e0880..7e369e4a8 100644 --- a/src/pip/_internal/utils/filesystem.py +++ b/src/pip/_internal/utils/filesystem.py @@ -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))