#!/usr/bin/python -u
#
# autotest <control file> - run the autotest control file specified.
#
from gevent import monkey; monkey.patch_all()

import os, sys
import common
from optparse import OptionParser
from autotest_lib.client.bin import job
from autotest_lib.client.common_lib import global_config


# Use the name of the binary to find the real installation directory
# aka $AUTODIR.  Update our path to include the $AUTODIR/bin/tests
# directory and ensure we have $AUTODIR in our environment.
autodirbin = os.path.dirname(os.path.realpath(sys.argv[0]))
autodir = os.path.dirname(autodirbin)

sys.path.insert(0, autodirbin)

os.environ['AUTODIR'] = autodir
os.environ['AUTODIRBIN'] = autodirbin
os.environ['PYTHONPATH'] = autodirbin

parser = OptionParser()

parser.add_option("-a", "--args", dest='args',
                        help="additional args to pass to control file")

parser.add_option("-c", "--continue", dest="cont", action="store_true",
                        default=False, help="continue previously started job")

parser.add_option("-t", "--tag", dest="tag", type="string", default="default",
                        help="set the job tag")

parser.add_option("-H", "--harness", dest="harness", type="string", default='',
                        help="set the harness type")

parser.add_option("-U", "--user", dest="user", type="string",
                        default='', help="set the job username")

parser.add_option("-l", "--external_logging", dest="log", action="store_true",
                        default=False, help="enable external logging")

parser.add_option('--verbose', dest='verbose', action='store_true',
                  help='Include DEBUG messages in console output')

parser.add_option('--quiet', dest='verbose', action='store_false',
                  help='Not include DEBUG messages in console output')

parser.add_option('--hostname', dest='hostname', type='string',
                  default=None, action='store',
                  help='Take this as the hostname of this machine '
                       '(given by autoserv)')

parser.add_option('--client_test_setup', dest='client_test_setup',
                  type='string', default=None, action='store',
                  help='a comma seperated list of client tests to prebuild on '
                       'the server. Use all to prebuild all of them.')

parser.add_option('--tap', dest='tap_report', action='store_true',
                  default=None, help='Output TAP (Test anything '
                  'protocol) reports')

def usage():
    parser.print_help()
    sys.exit(1)

options, args = parser.parse_args()

# Check for a control file if not in prebuild mode.
if len(args) != 1 and options.client_test_setup is None:
    usage()

drop_caches = global_config.global_config.get_config_value('CLIENT',
                                                           'drop_caches',
                                                           type=bool,
                                                           default=True)

if options.client_test_setup:
    from autotest_lib.client.bin import setup_job
    exit_code = 0
    try:
        setup_job.setup_tests(options)
    except:
        exit_code = 1
    sys.exit(exit_code)

# JOB: run the specified job control file.
job.runjob(os.path.realpath(args[0]), drop_caches, options)
