#!/usr/bin/env python3

"""
Copyright (c) 2015, Intel Corporation

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Intel Corporation nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from subprocess import call,check_output
from pprint import pprint
from numpy import *
import subprocess
import argparse
import shutil
import sys
import os

ezbench_dir = os.path.dirname(os.path.realpath(__file__))

sys.path.append(os.path.join(ezbench_dir, 'python-modules'))

from ezbench.smartezbench import *

def break_lists(input_list, sep=" "):
    res = []
    if input_list is None:
        return res
    for entry in input_list:
        res.extend(entry.split(sep))
    return res

# parse the options
attributes_val = dict()
def AttributeString(v):
    fields = v.split("=")
    if len(fields) != 2:
        raise argparse.ArgumentTypeError("Attributes need the format $name=(float)")
    else:
        attributes_val[fields[0]] = fields[1]
        return fields[0]

parser = argparse.ArgumentParser()
parser.add_argument("-b", dest='tests', help="<test regexp> include these tests to run",
                    action="append")
parser.add_argument("-B", dest='tests_exclude', help="<test regexp> exclude these benchmarks from running",
                    action="append")
parser.add_argument("-t", dest='testsets', help="Test sets to run", action="append")
parser.add_argument("-T", dest='list_testsets', help="List the available testsets", action="store_true")
parser.add_argument("-c", dest='commits', help="Commits to run the tests on",
                    action="append")
parser.add_argument("-r", dest='rounds', help="Number of execution rounds to add",
                    action="store", type=int, nargs='?')
parser.add_argument("-e", dest='ensure', help="Make sure that at least N rounds are executed",
                    action="store", type=int, nargs='?')
parser.add_argument("-p", dest='profile', help="Profile to be used by ezbench",
                    action="store")
parser.add_argument("-s", dest='add_conf_script', help="Add a configuration script for EzBench's runner",
                    action="append")
parser.add_argument("-S", dest='remove_conf_script', help="Remove a configuration script for EzBench's runner",
                    action="append")
parser.add_argument("-a", dest='attributes', help="Set an attribute",
                    type=AttributeString, action="append", choices=SmartEzbench.attributes())
parser.add_argument("-l", dest='list_tests', help="List the available benchmarks",
                    action="store_true")
parser.add_argument("report_name", nargs='?')
parser.add_argument("command", help="Command to execute", nargs='?',
                    choices=('start', 'run', 'pause', 'abort', 'status'))
args = parser.parse_args()

if args.list_tests:
    ezbench = Ezbench(ezbench_dir=ezbench_dir)
    tests = ezbench.available_tests()

    for test in sorted(tests):
        print(test)

    sys.exit(0)

if args.list_testsets:
    testsets = Testset.list(ezbench_dir)
    if len(testsets) > 0:
        print("Available test sets:")
        for testset in testsets:
            if testset.parse(None, silent=False):
                print(" * {}\n\t{}".format(testset.name, testset.description))
            else:
                print(" * {}: invalid because of one or more errors".format(testset.name))
    else:
        print("No test sets are available")
    sys.exit(0)

testsets_to_be_added = []
if args.testsets is not None:
     # remove duplicates in the lists
    testsets = list(set(break_lists(args.testsets)))

    ezbench = Ezbench(ezbench_dir=ezbench_dir)
    tests = ezbench.available_tests()

    # Check all the testsets
    for name in testsets:
        testset = Testset.open(ezbench_dir, name)
        if testset is None:
            print("Cannot find a test set named '{}'".format(name))
            sys.exit(1)
        if not testset.parse(tests):
            print("Invalid test set named {}, abort...".format(name))
            sys.exit(1)

        if args.report_name is None:
            print("The test set '{}' contains the following tests:".format(name))
            for test in sorted(testset.tests.keys()):
                print("\t{} --> {} rounds".format(test, testset.tests[test]))
            print("")
            sys.exit(0)

        testsets_to_be_added.append(testset)

if args.report_name is None:
    print("Error: The report name is missing")
    sys.exit(1)
sbench = SmartEzbench(ezbench_dir, args.report_name)

if sbench.profile() is None and args.profile is not None:
    sbench.set_profile(args.profile)

if args.add_conf_script is not None:
    for add in args.add_conf_script:
        sbench.add_conf_script(add)

if args.remove_conf_script is not None:
    for add in args.remove_conf_script:
        sbench.remove_conf_script(add)

if args.attributes is not None:
    for attr in set(args.attributes):
        sbench.set_attribute(attr, float(attributes_val[attr]))

# add commits and tests
if args.commits is not None and args.tests is not None:
    # remove duplicates in the lists
    commits = list(set(break_lists(args.commits)))
    tests = list(set(break_lists(args.tests)))
    tests_exclude = list(set(break_lists(args.tests_exclude)))

    # we cannot fetch the git sha1 without a profile/git repo
    if sbench.profile() is None:
        print("No profile is set, set one first with -p before adding test runs")
        sys.exit(1)

    # get the list of tests that actually need to be ran
    ezbench = Ezbench(ezbench_dir=ezbench_dir,
                  profile=sbench.profile(),
                  report_name="tmp")
    run_info = ezbench.run(commits, tests, tests_exclude, dry_run=True)
    if not run_info.success():
        sys.exit(1)

    # Default to 3 round if -r is not set
    if args.rounds is None:
        rounds = 3
    else:
        rounds = int(args.rounds)

    # Add all the commits and tests to commit
    for commit in run_info.commits:
        for test in run_info.tests:
            if args.ensure is None:
                total_rounds = sbench.add_test(commit, test, rounds)
                if rounds >= 0:
                    print("added {} runs to {} on {} --> {} runs".format(rounds,
                                                                        test, commit,
                                                                        total_rounds))
                else:
                    print("removed {} runs to {} on {} --> {} runs".format(-rounds,
                                                                        test, commit,
                                                                        total_rounds))
            else:
                added = sbench.force_test_rounds(commit, test, int(args.ensure))
                print("ensured {} runs of {} on {} --> added {} runs".format(int(args.ensure),
                                                                             test, commit,
                                                                             added))

if args.commits is not None and len(testsets_to_be_added) > 0:
    # remove duplicates in the lists
    commits = list(set(break_lists(args.commits)))

    # we cannot fetch the git sha1 without a profile/git repo
    if sbench.profile() is None:
        print("No profile is set, set one first with -p before adding test runs")
        sys.exit(1)

    # get the list of tests that actually need to be ran
    ezbench = Ezbench(ezbench_dir=ezbench_dir,
                  profile=sbench.profile(),
                  report_name="tmp")
    run_info = ezbench.run(commits, [], dry_run=True)
    if not run_info.success():
        sys.exit(1)

    # Ensure runs if set
    if args.ensure is None:
        # Default to 1 round if -r is not set
        if args.rounds is None:
            rounds = 1
        else:
            rounds = int(args.rounds)
        ensure = False
    else:
        rounds = int(args.ensure)
        ensure = True

    # Add the testsets specified
    for commit in run_info.commits:
        for testset in testsets_to_be_added:
            sbench.add_testset(commit, testset, rounds, ensure)

if args.command is not None:
    if args.command == "start":
        sbench.run()
    elif args.command == "run":
        sbench.set_running_mode(RunningMode.RUN)
    elif args.command == "pause":
        sbench.set_running_mode(RunningMode.PAUSE)
    elif args.command == "abort":
        sbench.set_running_mode(RunningMode.ABORT)
    elif args.command == "status":
        print("Report name:", sbench.report_name)
        print("Mode:", sbench.running_mode().name)

        print("\nAttributes:")
        for a in sbench.attributes():
            print(" - {}: {}".format(a, sbench.attribute(a)))

        print("\nRaw status:")
        pprint.pprint(sbench.state)
    else:
        print("Unknown command '{cmd}'".format(cmd=args.command))
