Skip to content

Commit 52c3a6a

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

2 files changed

Lines changed: 42 additions & 52 deletions

File tree

easybuild/tools/run.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -591,57 +591,56 @@ def parse_log_for_error(txt, regExp=None, stdout=True, msg=None):
591591
return res
592592

593593

594-
def check_log_for_errors(logTxt, regExps):
594+
def extract_errors_from_log(log_txt, reg_exps):
595595
"""
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,
596+
Check log_txt for messages matching regExps and return warnings and errors
597+
:param log_txt: String containing the log, will be split into individual lines
598+
:param reg_exps: List of: regular expressions (as strings) to error on,
599599
or tuple of regular expression and action (any of [IGNORE, WARN, ERROR])
600+
:return (warnings, errors) as lists of lines containing a match
600601
"""
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
609602

610603
# Avoid accidentally passing a single element
611-
assert isinstance(regExps, list), "regExps must be a list"
612-
regExpTuples = []
613-
for cur in regExps:
604+
assert isinstance(reg_exps, list), "reg_exps must be a list"
605+
re_tuples = []
606+
for cur in reg_exps:
614607
try:
615608
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")
609+
reg_exp, action = cur, ERROR
625610
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))
611+
reg_exp, action = cur
612+
if not isinstance(reg_exp, str) or action not in (IGNORE, WARN, ERROR):
613+
raise TypeError("Invalid types")
614+
re_tuples.append((re.compile(reg_exp), action))
615+
except Exception as e:
616+
raise EasyBuildError("Invalid input: No RegExp or tuple of RegExp and action '%s' (%s)", str(cur), e)
629617
warnings = []
630618
errors = []
631-
for l in logTxt.split('\n'):
632-
for regExp, action in regExpTuples:
633-
m = regExp.search(l)
634-
if m:
619+
for line in log_txt.split('\n'):
620+
for reg_exp, action in re_tuples:
621+
if reg_exp.search(line):
635622
if action == ERROR:
636-
errors.append(l)
623+
errors.append(line)
637624
elif action == WARN:
638-
warnings.append(l)
625+
warnings.append(line)
639626
break
627+
return warnings, errors
628+
629+
630+
def check_log_for_errors(log_txt, reg_exps):
631+
"""
632+
Check log_txt for messages matching regExps in order and do appropriate action
633+
:param log_txt: String containing the log, will be split into individual lines
634+
:param reg_exps: List of: regular expressions (as strings) to error on,
635+
or tuple of regular expression and action (any of [IGNORE, WARN, ERROR])
636+
"""
637+
global errors_found_in_log
638+
warnings, errors = extract_errors_from_log(log_txt, reg_exps)
639+
640640
errors_found_in_log += len(warnings) + len(errors)
641641
if warnings:
642-
_log.warning("Found %s potential errors in command output (output: %s)" %
643-
(len(warnings), ", ".join(warnings)))
642+
_log.warning("Found %s potential error(s) in command output (output: %s)",
643+
len(warnings), ", ".join(warnings))
644644
if errors:
645-
raise EasyBuildError("Found %s errors in command output (output: %s)" %
646-
(len(errors), ", ".join(errors)))
647-
645+
raise EasyBuildError("Found %s error(s) in command output (output: %s)",
646+
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)