mirror of https://github.com/pypa/pip
[commands/cache] Add file size information.
This commit is contained in:
parent
61a0adcfe7
commit
554133a90e
|
@ -4,10 +4,10 @@ import logging
|
||||||
import os
|
import os
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
import pip._internal.utils.filesystem as filesystem
|
||||||
from pip._internal.cli.base_command import Command
|
from pip._internal.cli.base_command import Command
|
||||||
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
||||||
from pip._internal.exceptions import CommandError, PipError
|
from pip._internal.exceptions import CommandError, PipError
|
||||||
from pip._internal.utils.filesystem import find_files
|
|
||||||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
||||||
|
|
||||||
if MYPY_CHECK_RUNNING:
|
if MYPY_CHECK_RUNNING:
|
||||||
|
@ -78,13 +78,18 @@ class CacheCommand(Command):
|
||||||
# type: (Values, List[Any]) -> None
|
# type: (Values, List[Any]) -> None
|
||||||
num_packages = len(self._find_wheels(options, '*'))
|
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("""
|
message = textwrap.dedent("""
|
||||||
Cache info:
|
Cache info:
|
||||||
Location: {location}
|
Location: {location}
|
||||||
Packages: {package_count}
|
Packages: {package_count}
|
||||||
|
Size: {size}
|
||||||
""").format(
|
""").format(
|
||||||
location=self._wheels_cache_dir(options),
|
location=cache_location,
|
||||||
package_count=num_packages,
|
package_count=num_packages,
|
||||||
|
size=cache_size,
|
||||||
).strip()
|
).strip()
|
||||||
|
|
||||||
logger.info(message)
|
logger.info(message)
|
||||||
|
@ -100,16 +105,18 @@ class CacheCommand(Command):
|
||||||
pattern = '*'
|
pattern = '*'
|
||||||
|
|
||||||
files = self._find_wheels(options, 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.')
|
logger.info('Nothing is currently cached.')
|
||||||
return
|
return
|
||||||
|
|
||||||
result = 'Current cache contents:\n'
|
results = []
|
||||||
for wheel in wheels:
|
for filename in files:
|
||||||
result += ' - %s\n' % wheel
|
wheel = os.path.basename(filename)
|
||||||
logger.info(result.strip())
|
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):
|
def remove_cache_items(self, options, args):
|
||||||
# type: (Values, List[Any]) -> None
|
# type: (Values, List[Any]) -> None
|
||||||
|
@ -142,4 +149,4 @@ class CacheCommand(Command):
|
||||||
def _find_wheels(self, options, pattern):
|
def _find_wheels(self, options, pattern):
|
||||||
# type: (Values, str) -> List[str]
|
# type: (Values, str) -> List[str]
|
||||||
wheel_dir = self._wheels_cache_dir(options)
|
wheel_dir = self._wheels_cache_dir(options)
|
||||||
return find_files(wheel_dir, pattern + '*.whl')
|
return filesystem.find_files(wheel_dir, pattern + '*.whl')
|
||||||
|
|
|
@ -18,7 +18,7 @@ from pip._internal.utils.compat import get_path_uid
|
||||||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
|
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
|
||||||
|
|
||||||
if MYPY_CHECK_RUNNING:
|
if MYPY_CHECK_RUNNING:
|
||||||
from typing import Any, BinaryIO, Iterator, List
|
from typing import Any, BinaryIO, Iterator, List, Union
|
||||||
|
|
||||||
class NamedTemporaryFileResult(BinaryIO):
|
class NamedTemporaryFileResult(BinaryIO):
|
||||||
@property
|
@property
|
||||||
|
@ -188,3 +188,51 @@ def find_files(path, pattern):
|
||||||
matches = fnmatch.filter(files, pattern)
|
matches = fnmatch.filter(files, pattern)
|
||||||
result.extend(os.path.join(root, f) for f in matches)
|
result.extend(os.path.join(root, f) for f in matches)
|
||||||
return result
|
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))
|
||||||
|
|
Loading…
Reference in New Issue