From 8f8678987b4496384452143b6eeec88d51b14510 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Mon, 6 Mar 2017 15:56:41 -0800 Subject: [PATCH] Improve readability of code and output for the images command Signed-off-by: Joffrey F --- compose/cli/main.py | 13 ++++++++----- compose/cli/utils.py | 13 +++++++++++++ tests/acceptance/cli_test.py | 5 ++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index 894dd9abb..98fc4e451 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -47,6 +47,7 @@ from .formatter import Formatter from .log_printer import build_log_presenters from .log_printer import LogPrinter from .utils import get_version_info +from .utils import human_readable_file_size from .utils import yesno @@ -496,10 +497,11 @@ class TopLevelCommand(object): key=attrgetter('name')) if options['-q']: - for container in containers: - print(str.split(str(container.image), ':')[1]) + for image in set(c.image for c in containers): + print(image.split(':')[1]) else: headers = [ + 'Container', 'Repository', 'Tag', 'Image Id', @@ -508,10 +510,11 @@ class TopLevelCommand(object): rows = [] for container in containers: image_config = container.image_config - repo_tags = str.split(str(image_config['RepoTags'][0]), ':') - image_id = str.split(str(container.image), ':')[1][0:12] - size = round(int(image_config['Size']) / float(1 << 20), 1) + repo_tags = image_config['RepoTags'][0].split(':') + image_id = image_config['Id'].split(':')[1][:12] + size = human_readable_file_size(image_config['Size']) rows.append([ + container.name, repo_tags[0], repo_tags[1], image_id, diff --git a/compose/cli/utils.py b/compose/cli/utils.py index 580bd1b07..4d4fc4c18 100644 --- a/compose/cli/utils.py +++ b/compose/cli/utils.py @@ -2,6 +2,7 @@ from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals +import math import os import platform import ssl @@ -135,3 +136,15 @@ def unquote_path(s): if s[0] == '"' and s[-1] == '"': return s[1:-1] return s + + +def human_readable_file_size(size): + suffixes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', ] + order = int(math.log(size, 2) / 10) if size else 0 + if order >= len(suffixes): + order = len(suffixes) - 1 + + return '{0:.3g} {1}'.format( + size / float(1 << (order * 10)), + suffixes[order] + ) diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index ef907d541..bec83ba1c 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -1991,10 +1991,13 @@ class CLITestCase(DockerClientTestCase): self.project.get_service('simple').create_container() result = self.dispatch(['images']) assert 'busybox' in result.stdout + assert 'simplecomposefile_simple_1' in result.stdout def test_images_default_composefile(self): self.base_dir = 'tests/fixtures/multiple-composefiles' self.dispatch(['up', '-d']) result = self.dispatch(['images']) - self.assertIn('busybox', result.stdout) + assert 'busybox' in result.stdout + assert 'multiplecomposefiles_another_1' in result.stdout + assert 'multiplecomposefiles_simple_1' in result.stdout