#!/usr/bin/env python

import argparse
import atexit
import logging
import sys
from pprint import pformat
from docker import Client

DEFAULT_DOCKER_BASE_URL = 'unix://var/run/docker.sock'
HELP_DOCKER_BASE_URL =  ('Refers to the protocol+hostname+port where the '
    'Docker server is hosted. Defaults to %s') % DEFAULT_DOCKER_BASE_URL
DEFAULT_DOCKER_API_VERSION = 'auto'
HELP_DOCKER_API_VERSION =  ('The version of the API the client will use. '
    'Defaults to use the API version provided by the server')
DEFAULT_DOCKER_HTTP_TIMEOUT = 5
HELP_DOCKER_HTTP_TIMEOUT =  ('The HTTP request timeout, in seconds. '
    'Defaults to %d secs') % DEFAULT_DOCKER_HTTP_TIMEOUT
DEFAULT_IMAGES_TO_KEEP = 2
HELP_IMAGES_TO_KEEP =  ('How many docker images to keep. '
    'Defaults to %d images') % DEFAULT_IMAGES_TO_KEEP
HELP_KEEP_NONE_IMAGES =  'Keep <none> images'

def _exit():
    logging.shutdown()

def debug_var(name, var):
    logging.debug('Var %s has: %s' % (name, pformat(var)))

def setup_parser(parser):
    parser.add_argument('--debug', help='debug mode', action='store_true')
    parser.add_argument('--base-url', help=HELP_DOCKER_BASE_URL, default=DEFAULT_DOCKER_BASE_URL)
    parser.add_argument('--api-version', help=HELP_DOCKER_API_VERSION, default=DEFAULT_DOCKER_API_VERSION)
    parser.add_argument('--http-timeout', help=HELP_DOCKER_HTTP_TIMEOUT, default=DEFAULT_DOCKER_HTTP_TIMEOUT, type=int)
    parser.add_argument('--images-to-keep', help=HELP_IMAGES_TO_KEEP, default=DEFAULT_IMAGES_TO_KEEP, type=int)
    parser.add_argument('--keep-none-images', help=HELP_KEEP_NONE_IMAGES, action='store_true')
    return parser

def validate_args(args):
    if args.http_timeout < 0:
        sys.stderr.write('HTTP timeout should be 0 or bigger\n')
        sys.exit(1)
    if args.images_to_keep < 0:
        sys.stderr.write('Images to keep should be 0 or bigger\n')
        sys.exit(1)

def split_by_none((non_none, none), dict_):
    if u'<none>:<none>' in dict_[u'RepoTags']:
        none.append(dict_)
    else:
        non_none.append(dict_)
    return (non_none, none)

def split_images(images):
    return reduce(split_by_none, images, ([], []))

def remove_keys_from_dict(keys, dict_):
    return { k: v for k, v in dict_.iteritems() if k not in keys }

def add_image_to_grp_images(grp_images, image):
    repo, _ = image[u'RepoTags'][0].split(':')
    new_image = remove_keys_from_dict([u'RepoTags'], image)
    new_image[u'Tags'] = [e.split(':')[-1] for e in image[u'RepoTags']]
    if repo in grp_images:
        grp_images[repo].append(new_image)
    else:
        grp_images[repo] = [new_image]
    return grp_images

def group_by_repo(images):
    return reduce(add_image_to_grp_images, images, {})

def main():
    atexit.register(func=_exit)
    parser = setup_parser(argparse.ArgumentParser(description='Clean old docker images'))
    args = parser.parse_args()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    if args.debug:
        debug_var(name='args', var=args)
    validate_args(args)
    client = Client(base_url=args.base_url, version=args.api_version, timeout=args.http_timeout)
    images = client.images()
    if args.debug:
        debug_var(name='images', var=images)
    non_none_images, none_images = split_images(images)
    if args.debug:
        debug_var(name='non_none_images', var=non_none_images)
        debug_var(name='none_images', var=none_images)
    grp_images = group_by_repo(non_none_images)
    if args.debug:
        debug_var(name='grp_images', var=grp_images)

if __name__ == '__main__':
    main()

@@ -99,16 +99,48 @@
 pformat%0A
+from operator import itemgetter%0A
 from doc
@@ -2741,16 +2741,239 @@
 s, %7B%7D)%0A%0A
+def reverse_sort_images_created(images):%0A    return sorted(images, key=itemgetter(u'Created'), reverse=True)%0A%0Adef sort_images_in_repos(repos):%0A    return %7B k: reverse_sort_images_created(v) for k, v in repos.iteritems() %7D%0A%0A
 def main
@@ -3672,21 +3672,37 @@
     
-grp_images = 
+repos = sort_images_in_repos(
 grou
@@ -3723,24 +3723,25 @@
 none_images)
+)
 %0A    if args
@@ -3776,34 +3776,24 @@
 me='
-grp_images', var=grp_image
+repos', var=repo
 s)%0A%0A

Add function to sort images inside repos