#!/usr/bin/env python

"""
Wait for a queued set of E3SM tests to finish by watching the
TestStatus files.  If all tests pass, 0 is returned, otherwise a
non-zero error code is returned. Note that this program waits
for the RUN phase specifically and will not terminate if the
RUN phase didn't happen.
"""

from standard_script_setup import *

import CIME.wait_for_tests

import argparse, sys, os

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
usage="""\n{0} [<Path to TestStatus> <Path to TestStatus> ...]  [--verbose]
OR
{0} --help

\033[1mEXAMPLES:\033[0m
    \033[1;32m# Wait for test in current dir\033[0m
    > {0}
    \033[1;32m# Wait for test in user specified tests\033[0m
    > {0} path/to/testdir
    \033[1;32m# Wait for all tests in a test area\033[0m
    > {0} path/to/testarea/*/TestStatus
""".format(os.path.basename(args[0])),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("paths", default=".", nargs="*", help="Paths to test directories or status file. Pwd default.")

    parser.add_argument("-n", "--no-wait", action="store_true",
                        help="Do not wait for tests to finish")

    parser.add_argument("--no-run", action="store_true",
                        help="Do not expect run phase to be completed")

    parser.add_argument("-t", "--check-throughput", action="store_true",
                        help="Fail if throughput check fails (fail if tests slow down)")

    parser.add_argument("-m", "--check-memory", action="store_true",
                        help="Fail if memory check fails (fail if tests footprint grows)")

    parser.add_argument("-i", "--ignore-namelist-diffs", action="store_true",
                        help="Do not fail a test if the only problem is diffing namelists")

    parser.add_argument("--ignore-memleak", action="store_true",
                        help="Do not fail a test if the only problem is a memleak")

    parser.add_argument("--force-log-upload", action="store_true",
                        help="Always upload logs to cdash, even if test passed")

    parser.add_argument("-b", "--cdash-build-name",
                        help="Build name, implies you want results send to Cdash")

    parser.add_argument("-p", "--cdash-project", default=CIME.wait_for_tests.E3SM_MAIN_CDASH,
                        help="The name of the CDash project where results should be uploaded")

    parser.add_argument("-g", "--cdash-build-group", default=CIME.wait_for_tests.CDASH_DEFAULT_BUILD_GROUP,
                        help="The build group to be used to display results on the CDash dashboard.")

    parser.add_argument("--timeout", type=int,
                        help="Timeout wait in seconds.")

    parser.add_argument("--update-success", action="store_true",
                        help="Record test success in baselines. Only the nightly process should use this in general.")

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

    return args.paths, args.no_wait, args.check_throughput, args.check_memory, args.ignore_namelist_diffs, args.ignore_memleak, args.cdash_build_name, args.cdash_project, args.cdash_build_group, args.timeout, args.force_log_upload, args.no_run, args.update_success

###############################################################################
def _main_func(description):
###############################################################################
    test_paths, no_wait, check_throughput, check_memory, ignore_namelist_diffs, ignore_memleak, cdash_build_name, cdash_project, cdash_build_group, timeout, force_log_upload, no_run, update_success = \
        parse_command_line(sys.argv, description)

    sys.exit(0 if CIME.wait_for_tests.wait_for_tests(test_paths,
                                                     no_wait=no_wait,
                                                     check_throughput=check_throughput,
                                                     check_memory=check_memory,
                                                     ignore_namelists=ignore_namelist_diffs,
                                                     ignore_memleak=ignore_memleak,
                                                     cdash_build_name=cdash_build_name,
                                                     cdash_project=cdash_project,
                                                     cdash_build_group=cdash_build_group,
                                                     timeout=timeout,
                                                     force_log_upload=force_log_upload,
                                                     no_run=no_run,
                                                     update_success=update_success)
             else CIME.utils.TESTS_FAILED_ERR_CODE)

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

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