#!/usr/bin/env python

"""
List test results based on TestStatus files. Returns True if
no errors occured (not based on test statuses).
"""

import acme_util
acme_util.check_minimum_python_version(2, 7)
import wait_for_tests

import argparse, sys, os

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
usage="""\n%s <Glob of TestStatus> [<Path/Glob to TestStatus> ...]  [--verbose]
OR
%s --help
OR
%s --test

\033[1mEXAMPLES:\033[0m
    \033[1;32m# Wait for all tests in a test area\033[0m
    > %s path/to/testarea/*/TestStatus
""" % ((os.path.basename(args[0]), ) * 4),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

    parser.add_argument("paths", nargs="+", help="Paths to TestStatus files.")

    parser.add_argument("-v", "--verbose", action="store_true",
                        help="Print extra information")

    args = parser.parse_args(args[1:])

    acme_util.set_verbosity(args.verbose)

    return args.paths

###############################################################################
def cs_status(test_paths):
###############################################################################
    for test_path in test_paths:
        statuses, test_name = wait_for_tests.parse_test_status_file(test_path)
        summary = wait_for_tests.reduce_stati(statuses)
        print "%s (Overall: %s), details:" % (test_name, summary)
        for phase, status in statuses.iteritems():
            print "  %s %s" % (status, phase)

###############################################################################
def _main_func(description):
###############################################################################
    acme_util.stop_buffering_output()

    test_paths = parse_command_line(sys.argv, description)

    cs_status(test_paths)

###############################################################################

if (__name__ == "__main__"):
    _main_func(__doc__)
