Skip to content

Commit

Permalink
Reintroduce verbose option into the refactored logging system
Browse files Browse the repository at this point in the history
Also, allow code_checker to check individual files
  • Loading branch information
jgfouca committed Jul 26, 2016
1 parent 8ff5f19 commit 9aa8683
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
23 changes: 19 additions & 4 deletions scripts/Tools/code_checker
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ formatter_class=argparse.ArgumentDefaultsHelpFormatter
parser.add_argument("-j", "--num-procs", type=int, default=8,
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 = parser.parse_args(args[1:])

CIME.utils.handle_standard_logging_options(args)

return args.dir, args.num_procs
return args.dir, args.num_procs, args.files

###############################################################################
def run_pylint(on_file):
Expand All @@ -62,7 +65,16 @@ def run_pylint(on_file):
return True

###############################################################################
def check_code(dir_to_check, num_procs):
def matches(file_path, file_ends):
###############################################################################
for file_end in file_ends:
if file_path.endswith(file_end):
return True

return False

###############################################################################
def check_code(dir_to_check, num_procs, files):
###############################################################################
"""
Check all python files in the given directory
Expand All @@ -77,6 +89,9 @@ def check_code(dir_to_check, num_procs):

# Get list of files to check
files_to_check = run_cmd_no_fail('find %s -name "*.py"' % dir_to_check).splitlines()
if files:
files_to_check = [item for item in files_to_check if matches(item, files)]

pool = ThreadPool(num_procs)
results = pool.map(run_pylint, files_to_check)

Expand All @@ -92,9 +107,9 @@ def _main_func(description):
pylint = find_executable("pylint")
expect(pylint is not None,"pylint not found")

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

sys.exit(0 if check_code(dir_to_check, num_procs) else 1)
sys.exit(0 if check_code(dir_to_check, num_procs, files) else 1)

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

Expand Down
17 changes: 13 additions & 4 deletions utils/python/CIME/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ def get_project(machobj=None):
def setup_standard_logging_options(parser):
parser.add_argument("-d", "--debug", action="store_true",
help="Print debug information (very verbose)")
parser.add_argument("-v", "--verbose", action="store_true",
help="Add additional context (time and file) to log messages")
parser.add_argument("-s", "--silent", action="store_true",
help="Print only warnings and error messages")

Expand All @@ -567,25 +569,32 @@ def handle_standard_logging_options(args):
"""
root_logger = logging.getLogger()

verbose_formatter = logging.Formatter(fmt='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')

# Change info to go to stdout. This handle applies to INFO exclusively
stdout_stream_handler = logging.StreamHandler(stream=sys.stdout)
stdout_stream_handler.setLevel(logging.INFO)
stdout_stream_handler.addFilter(_LessThanFilter(logging.WARNING))
root_logger.addHandler(stdout_stream_handler)

# Change warnings and above to go to stderr
stderr_stream_handler = logging.StreamHandler(stream=sys.stderr)
stderr_stream_handler.setLevel(logging.WARNING)

# --verbose adds to the message format but does not impact the log level
if args.verbose:
stdout_stream_handler.setFormatter(verbose_formatter)
stderr_stream_handler.setFormatter(verbose_formatter)

root_logger.addHandler(stdout_stream_handler)
root_logger.addHandler(stderr_stream_handler)

if args.debug:
# Set up log file to catch ALL logging records
log_file = "%s.log" % os.path.basename(sys.argv[0])

debug_log_handler = logging.FileHandler(log_file, mode='w')
debug_formatter = logging.Formatter(fmt='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
debug_log_handler.setFormatter(debug_formatter)
debug_log_handler.setFormatter(verbose_formatter)
debug_log_handler.setLevel(logging.DEBUG)
root_logger.addHandler(debug_log_handler)

Expand Down

0 comments on commit 9aa8683

Please sign in to comment.