#!/usr/bin/env python

"""
Ensure that all CIME python files are free of errors
and follow the PEP8 standard.
"""

from standard_script_setup import *

from CIME.code_checker import check_code, expect

import argparse, sys, os, doctest
from distutils.spawn import find_executable

logger = logging.getLogger(__name__)

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

\033[1mEXAMPLES:\033[0m
    \033[1;32m# Check code \033[0m
    > {0}

    \033[1;32m# Check code single file case.py \033[0m
    \033[1;32m# Note, you do NOT have to provide the path to this file, the tool will find it \033[0m
    > {0} case.py
""".format(os.path.basename(args[0])),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("-j", "--num-procs", type=int, default=10,
                        help="The number of files to check in parallel")

    parser.add_argument("files", nargs="*",
                        help="Restrict checking to specific files. Relative name is fine.")

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

    return args.num_procs, args.files

###############################################################################
def _main_func(description):
###############################################################################
    if ("--test" in sys.argv):
        test_results = doctest.testmod(verbose=True)
        sys.exit(1 if test_results.failed > 0 else 0)

    pylint = find_executable("pylint")
    expect(pylint is not None, "pylint not found")

    num_procs, files = parse_command_line(sys.argv, description)

    results = check_code(files, num_procs=num_procs, interactive=True)
    for result in results.itervalues():
        if result != "":
            sys.exit(1)

    sys.exit(0)

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

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