#!/usr/bin/env python

"""
This script recreates and acme suite within the CESM testlist.xml
file, which should be given as the only argument. It deletes any
existing category from the XML file, draws from information within
this file to create new test entries, and then inserts these entries
into the XML file.
"""

import acme_util
from acme_util import expect
acme_util.check_minimum_python_version(2, 7)

import update_acme_tests

import sys, argparse, os

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
usage="""\n%s testlist.xml [<test category> <test category> ...] [--verbose]
OR
%s --help
OR
%s --test

\033[1mEXAMPLES:\033[0m
    \033[1;32m# Update all acme test suites for all platforms \033[0m
    > %s ../scripts/Testing/Testlistxml/testlist_allactive.xml
    \033[1;32m# Update acme_developer tests for all platforms \033[0m
    > %s ../scripts/Testing/Testlistxml/testlist_allactive.xml acme_developer
    \033[1;32m# Add acme_developer tests for a new platform \033[0m
    > %s ../scripts/Testing/Testlistxml/testlist_allactive.xml acme_developer -p machine,compiler
""" % ((os.path.basename(args[0]), ) * 6),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

    parser.add_argument("test_list_path", help="The path to test lists XML file")

    parser.add_argument("categories", nargs="*",
                        help="The test categories to update. Default will update all. Test categories: %s" % (", ".join(update_acme_tests.get_test_suites())))

    parser.add_argument("-p", "--platform",
                        help="Only add tests for a specific platform, format=machine,compiler. Useful for adding new platforms.")

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

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

    acme_util.set_verbosity(args.verbose)

    expect(os.path.isfile(args.test_list_path),
           "'%s' is not a valid file" % args.test_list_path)

    expect(args.platform is None or len(args.platform.split(",")) == 2,
           "-p value must be in format: 'machine,compiler'")

    if (not args.categories):
        args.categories = update_acme_tests.get_test_suites()

    return args.categories, args.test_list_path, args.platform

###############################################################################
def _main_func(description):
###############################################################################
    if ("--test" in sys.argv):
        acme_util.run_cmd("python -m doctest %s/update_acme_tests.py -v" % os.path.dirname(sys.argv[0]), arg_stdout=None, arg_stderr=None)
        return

    categories, test_list_path, platform = parse_command_line(sys.argv, description)

    acme_util.stop_buffering_output()

    update_acme_tests.update_acme_tests(test_list_path, categories, platform)

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