|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Ensure that all CIME python files are free of errors |
| 5 | +and follow the PEP8 standard. |
| 6 | +""" |
| 7 | + |
| 8 | +from standard_script_setup import * |
| 9 | + |
| 10 | +from CIME.utils import run_cmd, run_cmd_no_fail, expect, get_python_libs_root |
| 11 | + |
| 12 | +import argparse, sys, os, doctest |
| 13 | +from multiprocessing.dummy import Pool as ThreadPool |
| 14 | + |
| 15 | +############################################################################### |
| 16 | +def parse_command_line(args, description): |
| 17 | +############################################################################### |
| 18 | + parser = argparse.ArgumentParser( |
| 19 | +usage="""\n%s [--verbose] |
| 20 | +OR |
| 21 | +%s --help |
| 22 | +OR |
| 23 | +%s --test |
| 24 | +
|
| 25 | +\033[1mEXAMPLES:\033[0m |
| 26 | + \033[1;32m# Check code \033[0m |
| 27 | + > %s |
| 28 | +""" % ((os.path.basename(args[0]), ) * 4), |
| 29 | + |
| 30 | +description=description, |
| 31 | + |
| 32 | +formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 33 | +) |
| 34 | + |
| 35 | + CIME.utils.setup_standard_logging_options(parser) |
| 36 | + |
| 37 | + parser.add_argument("--dir", default=get_python_libs_root(), |
| 38 | + help="The root directory containing python files to check.") |
| 39 | + |
| 40 | + parser.add_argument("-j", "--num-procs", type=int, default=8, |
| 41 | + help="The number of files to check in parallel") |
| 42 | + |
| 43 | + args = parser.parse_args(args[1:]) |
| 44 | + |
| 45 | + CIME.utils.handle_standard_logging_options(args) |
| 46 | + |
| 47 | + return args.dir, args.num_procs |
| 48 | + |
| 49 | +############################################################################### |
| 50 | +def run_pylint(on_file): |
| 51 | +############################################################################### |
| 52 | + cmd = "pylint --disable I,C,R,logging-not-lazy,wildcard-import,unused-wildcard-import,fixme,broad-except,bare-except,eval-used,exec-used,global-statement %s" % on_file |
| 53 | + stat = run_cmd(cmd)[0] |
| 54 | + if stat != 0: |
| 55 | + sys.stdout.write("File %s has pylint problems, please fix\n Use command: %s\n" % (on_file, cmd)) |
| 56 | + return False |
| 57 | + else: |
| 58 | + sys.stdout.write("File %s has no pylint problems\n" % on_file) |
| 59 | + return True |
| 60 | + |
| 61 | +############################################################################### |
| 62 | +def check_code(dir_to_check, num_procs): |
| 63 | +############################################################################### |
| 64 | + """ |
| 65 | + Check all python files in the given directory |
| 66 | +
|
| 67 | + Returns True if all files had no problems |
| 68 | + """ |
| 69 | + # Pylint won't work right if the imports within the checked files fails |
| 70 | + if "PYTHONPATH" in os.environ: |
| 71 | + os.environ["PYTHONPATH"] += dir_to_check |
| 72 | + else: |
| 73 | + os.environ["PYTHONPATH"] = dir_to_check |
| 74 | + |
| 75 | + # Get list of files to check |
| 76 | + files_to_check = run_cmd_no_fail('find %s -name "*.py"' % dir_to_check).splitlines() |
| 77 | + pool = ThreadPool(num_procs) |
| 78 | + results = pool.map(run_pylint, files_to_check) |
| 79 | + |
| 80 | + return False not in results |
| 81 | + |
| 82 | +############################################################################### |
| 83 | +def _main_func(description): |
| 84 | +############################################################################### |
| 85 | + if ("--test" in sys.argv): |
| 86 | + test_results = doctest.testmod(verbose=True) |
| 87 | + sys.exit(1 if test_results.failed > 0 else 0) |
| 88 | + |
| 89 | + dir_to_check, num_procs = parse_command_line(sys.argv, description) |
| 90 | + |
| 91 | + sys.exit(0 if check_code(dir_to_check, num_procs) else 1) |
| 92 | + |
| 93 | +############################################################################### |
| 94 | + |
| 95 | +if (__name__ == "__main__"): |
| 96 | + _main_func(__doc__) |
0 commit comments