Skip to content

Commit b8408b0

Browse files
committed
Adress review comments
Rename variables Accept only strings not compiled regExps Split function in 2 for reuse
1 parent b81e352 commit b8408b0

2 files changed

Lines changed: 41 additions & 53 deletions

File tree

easybuild/tools/run.py

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -590,58 +590,55 @@ def parse_log_for_error(txt, regExp=None, stdout=True, msg=None):
590590

591591
return res
592592

593-
594-
def check_log_for_errors(logTxt, regExps):
593+
def extract_errors_from_log(log_txt, reg_exps):
595594
"""
596-
Check logTxt for messages matching regExps in order and do appropriate action
597-
:param logTxt: String containing the log, will be split into individual lines
598-
:param regExps: List of: regular expressions (as RE or string) to error on,
595+
Check log_txt for messages matching regExps and return warnings and errors
596+
:param log_txt: String containing the log, will be split into individual lines
597+
:param reg_exps: List of: regular expressions (as strings) to error on,
599598
or tuple of regular expression and action (any of [IGNORE, WARN, ERROR])
599+
:return (warnings, errors) as lists of lines containing a match
600600
"""
601-
global errors_found_in_log
602-
603-
def is_regexp_object(objToTest):
604-
try:
605-
objToTest.match('')
606-
return True
607-
except AttributeError:
608-
return False
609601

610602
# Avoid accidentally passing a single element
611-
assert isinstance(regExps, list), "regExps must be a list"
612-
regExpTuples = []
613-
for cur in regExps:
603+
assert isinstance(reg_exps, list), "reg_exps must be a list"
604+
re_tuples = []
605+
for cur in reg_exps:
614606
try:
615607
if isinstance(cur, str):
616-
regExpTuples.append((re.compile(cur), ERROR))
617-
elif is_regexp_object(cur):
618-
regExpTuples.append((cur, ERROR))
619-
elif len(cur) != 2:
620-
raise TypeError("Invalid tuple")
621-
elif not isinstance(cur[0], str) and not is_regexp_object(cur[0]):
622-
raise TypeError("Invalid RegExp in tuple")
623-
elif cur[1] not in (IGNORE, WARN, ERROR):
624-
raise TypeError("Invalid action in tuple")
608+
reg_exp, action = cur, ERROR
625609
else:
626-
regExpTuples.append((re.compile(cur[0]) if isinstance(cur[0], str) else cur[0], cur[1]))
627-
except TypeError:
628-
raise EasyBuildError("Invalid input: No RegExp or tuple of RegExp and action: %s" % str(cur))
610+
reg_exp, action = cur
611+
if not isinstance(reg_exp, str) or action not in (IGNORE, WARN, ERROR):
612+
raise TypeError("Invalid types")
613+
re_tuples.append((re.compile(reg_exp), action))
614+
except Exception as e:
615+
raise EasyBuildError("Invalid input: No RegExp or tuple of RegExp and action '%s' (%s)", str(cur), e)
629616
warnings = []
630617
errors = []
631-
for l in logTxt.split('\n'):
632-
for regExp, action in regExpTuples:
633-
m = regExp.search(l)
634-
if m:
618+
for line in log_txt.split('\n'):
619+
for reg_exp, action in re_tuples:
620+
if reg_exp.search(line):
635621
if action == ERROR:
636-
errors.append(l)
622+
errors.append(line)
637623
elif action == WARN:
638-
warnings.append(l)
624+
warnings.append(line)
639625
break
626+
return warnings, errors
627+
628+
def check_log_for_errors(log_txt, reg_exps):
629+
"""
630+
Check log_txt for messages matching regExps in order and do appropriate action
631+
:param log_txt: String containing the log, will be split into individual lines
632+
:param reg_exps: List of: regular expressions (as strings) to error on,
633+
or tuple of regular expression and action (any of [IGNORE, WARN, ERROR])
634+
"""
635+
global errors_found_in_log
636+
warnings, errors = extract_errors_from_log(log_txt, reg_exps)
637+
640638
errors_found_in_log += len(warnings) + len(errors)
641639
if warnings:
642-
_log.warning("Found %s potential errors in command output (output: %s)" %
643-
(len(warnings), ", ".join(warnings)))
640+
_log.warning("Found %s potential error(s) in command output (output: %s)",
641+
len(warnings), ", ".join(warnings))
644642
if errors:
645-
raise EasyBuildError("Found %s errors in command output (output: %s)" %
646-
(len(errors), ", ".join(errors)))
647-
643+
raise EasyBuildError("Found %s error(s) in command output (output: %s)",
644+
len(errors), ", ".join(errors))

test/framework/run.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,40 +526,31 @@ def test_check_log_for_errors(self):
526526
"enabling -Werror",
527527
"the process crashed with 0"
528528
])
529-
expected_error_msg = r"Found 2 errors in command output \(output: error found, the process crashed with 0\)"
529+
expected_error_msg = r"Found 2 error\(s\) in command output \(output: error found, the process crashed with 0\)"
530530

531531
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
532532
[r"\b(error|crashed)\b"])
533-
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
534-
[re.compile(r"\b(error|crashed)\b")])
535533
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
536534
[(r"\b(error|crashed)\b", ERROR)])
537-
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
538-
[(re.compile(r"\b(error|crashed)\b"), ERROR)])
539535

540-
expected_error_msg = "Found 2 potential errors in command output " \
536+
expected_error_msg = "Found 2 potential error(s) in command output " \
541537
"(output: error found, the process crashed with 0)"
542538
init_logging(logfile, silent=True)
543539
check_log_for_errors(input_text, [(r"\b(error|crashed)\b", WARN)])
544540
stop_logging(logfile)
545541
self.assertTrue(expected_error_msg in read_file(logfile))
546-
write_file(logfile, '')
547-
init_logging(logfile, silent=True)
548-
check_log_for_errors(input_text, [(re.compile(r"\b(error|crashed)\b"), WARN)])
549-
stop_logging(logfile)
550-
self.assertTrue(expected_error_msg in read_file(logfile))
551542

552-
expected_error_msg = r"Found 2 errors in command output \(output: error found, test failed\)"
543+
expected_error_msg = r"Found 2 error\(s\) in command output \(output: error found, test failed\)"
553544
write_file(logfile, '')
554545
init_logging(logfile, silent=True)
555546
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text, [
556547
r"\berror\b",
557548
(r"\ballowed-test failed\b", IGNORE),
558-
(re.compile(r"\bCRASHED\b", re.I), WARN),
549+
(r"(?i)\bCRASHED\b", WARN),
559550
"fail"
560551
])
561552
stop_logging(logfile)
562-
expected_error_msg = "Found 1 potential errors in command output (output: the process crashed with 0)"
553+
expected_error_msg = "Found 1 potential error(s) in command output (output: the process crashed with 0)"
563554
self.assertTrue(expected_error_msg in read_file(logfile))
564555

565556

0 commit comments

Comments
 (0)