Improve readability of code and output for the images command

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2017-03-06 15:56:41 -08:00
parent 815a3af6d2
commit 8f8678987b
3 changed files with 25 additions and 6 deletions

View File

@ -47,6 +47,7 @@ from .formatter import Formatter
from .log_printer import build_log_presenters from .log_printer import build_log_presenters
from .log_printer import LogPrinter from .log_printer import LogPrinter
from .utils import get_version_info from .utils import get_version_info
from .utils import human_readable_file_size
from .utils import yesno from .utils import yesno
@ -496,10 +497,11 @@ class TopLevelCommand(object):
key=attrgetter('name')) key=attrgetter('name'))
if options['-q']: if options['-q']:
for container in containers: for image in set(c.image for c in containers):
print(str.split(str(container.image), ':')[1]) print(image.split(':')[1])
else: else:
headers = [ headers = [
'Container',
'Repository', 'Repository',
'Tag', 'Tag',
'Image Id', 'Image Id',
@ -508,10 +510,11 @@ class TopLevelCommand(object):
rows = [] rows = []
for container in containers: for container in containers:
image_config = container.image_config image_config = container.image_config
repo_tags = str.split(str(image_config['RepoTags'][0]), ':') repo_tags = image_config['RepoTags'][0].split(':')
image_id = str.split(str(container.image), ':')[1][0:12] image_id = image_config['Id'].split(':')[1][:12]
size = round(int(image_config['Size']) / float(1 << 20), 1) size = human_readable_file_size(image_config['Size'])
rows.append([ rows.append([
container.name,
repo_tags[0], repo_tags[0],
repo_tags[1], repo_tags[1],
image_id, image_id,

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import unicode_literals from __future__ import unicode_literals
import math
import os import os
import platform import platform
import ssl import ssl
@ -135,3 +136,15 @@ def unquote_path(s):
if s[0] == '"' and s[-1] == '"': if s[0] == '"' and s[-1] == '"':
return s[1:-1] return s[1:-1]
return s 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]
)

View File

@ -1991,10 +1991,13 @@ class CLITestCase(DockerClientTestCase):
self.project.get_service('simple').create_container() self.project.get_service('simple').create_container()
result = self.dispatch(['images']) result = self.dispatch(['images'])
assert 'busybox' in result.stdout assert 'busybox' in result.stdout
assert 'simplecomposefile_simple_1' in result.stdout
def test_images_default_composefile(self): def test_images_default_composefile(self):
self.base_dir = 'tests/fixtures/multiple-composefiles' self.base_dir = 'tests/fixtures/multiple-composefiles'
self.dispatch(['up', '-d']) self.dispatch(['up', '-d'])
result = self.dispatch(['images']) 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