def print_tryjobs(options, builds):
    'Prints nicely result of fetch_try_jobs.'
    if (not builds):
        print('No tryjobs scheduled')
        return
    builds = builds.copy()
    builder_names_cache = {}

    def get_builder(b):
        try:
            return builder_names_cache[b['id']]
        except KeyError:
            try:
                parameters = json.loads(b['parameters_json'])
                name = parameters['builder_name']
            except (ValueError, KeyError) as error:
                print(('WARNING: failed to get builder name for build %s: %s' % (b['id'], error)))
                name = None
            builder_names_cache[b['id']] = name
            return name

    def get_bucket(b):
        bucket = b['bucket']
        if bucket.startswith('master.'):
            return bucket[len('master.'):]
        return bucket
    if options.print_master:
        name_fmt = ('%%-%ds %%-%ds' % (max((len(str(get_bucket(b))) for b in builds.itervalues())), max((len(str(get_builder(b))) for b in builds.itervalues()))))

        def get_name(b):
            return (name_fmt % (get_bucket(b), get_builder(b)))
    else:
        name_fmt = ('%%-%ds' % max((len(str(get_builder(b))) for b in builds.itervalues())))

        def get_name(b):
            return (name_fmt % get_builder(b))

    def sort_key(b):
        return (b['status'], b.get('result'), get_name(b), b.get('url'))

    def pop(title, f, color=None, **kwargs):
        'Pop matching builds from `builds` dict and print them.'
        if ((not options.color) or (color is None)):
            colorize = str
        else:
            colorize = (lambda x: ('%s%s%s' % (color, x, Fore.RESET)))
        result = []
        for b in builds.values():
            if all(((b.get(k) == v) for (k, v) in kwargs.iteritems())):
                builds.pop(b['id'])
                result.append(b)
        if result:
            print(colorize(title))
            for b in sorted(result, key=sort_key):
                print(' ', colorize('\t'.join(map(str, f(b)))))
    total = len(builds)
    pop(status='COMPLETED', result='SUCCESS', title='Successes:', color=Fore.GREEN, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='FAILURE', failure_reason='INFRA_FAILURE', title='Infra Failures:', color=Fore.MAGENTA, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='FAILURE', failure_reason='BUILD_FAILURE', title='Failures:', color=Fore.RED, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='CANCELED', title='Canceled:', color=Fore.MAGENTA, f=(lambda b: (get_name(b),)))
    pop(status='COMPLETED', result='FAILURE', failure_reason='INVALID_BUILD_DEFINITION', title='Wrong master/builder name:', color=Fore.MAGENTA, f=(lambda b: (get_name(b),)))
    pop(status='COMPLETED', result='FAILURE', title='Other failures:', f=(lambda b: (get_name(b), b.get('failure_reason'), b.get('url'))))
    pop(status='COMPLETED', title='Other finished:', f=(lambda b: (get_name(b), b.get('result'), b.get('url'))))
    pop(status='STARTED', title='Started:', color=Fore.YELLOW, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='SCHEDULED', title='Scheduled:', f=(lambda b: (get_name(b), ('id=%s' % b['id']))))
    pop(title='Other:', f=(lambda b: (get_name(b), ('id=%s' % b['id']))))
    assert (len(builds) == 0)
    print(('Total: %d tryjobs' % total))
Prints nicely result of fetch_try_jobs.
def print_tryjobs(options, builds):
    
    if (not builds):
        print('No tryjobs scheduled')
        return
    builds = builds.copy()
    builder_names_cache = {}

    def get_builder(b):
        try:
            return builder_names_cache[b['id']]
        except KeyError:
            try:
                parameters = json.loads(b['parameters_json'])
                name = parameters['builder_name']
            except (ValueError, KeyError) as error:
                print(('WARNING: failed to get builder name for build %s: %s' % (b['id'], error)))
                name = None
            builder_names_cache[b['id']] = name
            return name

    def get_bucket(b):
        bucket = b['bucket']
        if bucket.startswith('master.'):
            return bucket[len('master.'):]
        return bucket
    if options.print_master:
        name_fmt = ('%%-%ds %%-%ds' % (max((len(str(get_bucket(b))) for b in builds.itervalues())), max((len(str(get_builder(b))) for b in builds.itervalues()))))

        def get_name(b):
            return (name_fmt % (get_bucket(b), get_builder(b)))
    else:
        name_fmt = ('%%-%ds' % max((len(str(get_builder(b))) for b in builds.itervalues())))

        def get_name(b):
            return (name_fmt % get_builder(b))

    def sort_key(b):
        return (b['status'], b.get('result'), get_name(b), b.get('url'))

    def pop(title, f, color=None, **kwargs):
        'Pop matching builds from `builds` dict and print them.'
        if ((not options.color) or (color is None)):
            colorize = str
        else:
            colorize = (lambda x: ('%s%s%s' % (color, x, Fore.RESET)))
        result = []
        for b in builds.values():
            if all(((b.get(k) == v) for (k, v) in kwargs.iteritems())):
                builds.pop(b['id'])
                result.append(b)
        if result:
            print(colorize(title))
            for b in sorted(result, key=sort_key):
                print(' ', colorize('\t'.join(map(str, f(b)))))
    total = len(builds)
    pop(status='COMPLETED', result='SUCCESS', title='Successes:', color=Fore.GREEN, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='FAILURE', failure_reason='INFRA_FAILURE', title='Infra Failures:', color=Fore.MAGENTA, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='FAILURE', failure_reason='BUILD_FAILURE', title='Failures:', color=Fore.RED, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='COMPLETED', result='CANCELED', title='Canceled:', color=Fore.MAGENTA, f=(lambda b: (get_name(b),)))
    pop(status='COMPLETED', result='FAILURE', failure_reason='INVALID_BUILD_DEFINITION', title='Wrong master/builder name:', color=Fore.MAGENTA, f=(lambda b: (get_name(b),)))
    pop(status='COMPLETED', result='FAILURE', title='Other failures:', f=(lambda b: (get_name(b), b.get('failure_reason'), b.get('url'))))
    pop(status='COMPLETED', title='Other finished:', f=(lambda b: (get_name(b), b.get('result'), b.get('url'))))
    pop(status='STARTED', title='Started:', color=Fore.YELLOW, f=(lambda b: (get_name(b), b.get('url'))))
    pop(status='SCHEDULED', title='Scheduled:', f=(lambda b: (get_name(b), ('id=%s' % b['id']))))
    pop(title='Other:', f=(lambda b: (get_name(b), ('id=%s' % b['id']))))
    assert (len(builds) == 0)
    print(('Total: %d tryjobs' % total))