Skip to content

Commit a85a51a

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

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
@@ -538,40 +538,31 @@ def test_check_log_for_errors(self):
538538
"enabling -Werror",
539539
"the process crashed with 0"
540540
])
541-
expected_error_msg = r"Found 2 errors in command output \(output: error found, the process crashed with 0\)"
541+
expected_error_msg = r"Found 2 error\(s\) in command output \(output: error found, the process crashed with 0\)"
542542

543543
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
544544
[r"\b(error|crashed)\b"])
545-
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
546-
[re.compile(r"\b(error|crashed)\b")])
547545
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
548546
[(r"\b(error|crashed)\b", ERROR)])
549-
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text,
550-
[(re.compile(r"\b(error|crashed)\b"), ERROR)])
551547

552-
expected_error_msg = "Found 2 potential errors in command output " \
548+
expected_error_msg = "Found 2 potential error(s) in command output " \
553549
"(output: error found, the process crashed with 0)"
554550
init_logging(logfile, silent=True)
555551
check_log_for_errors(input_text, [(r"\b(error|crashed)\b", WARN)])
556552
stop_logging(logfile)
557553
self.assertTrue(expected_error_msg in read_file(logfile))
558-
write_file(logfile, '')
559-
init_logging(logfile, silent=True)
560-
check_log_for_errors(input_text, [(re.compile(r"\b(error|crashed)\b"), WARN)])
561-
stop_logging(logfile)
562-
self.assertTrue(expected_error_msg in read_file(logfile))
563554

564-
expected_error_msg = r"Found 2 errors in command output \(output: error found, test failed\)"
555+
expected_error_msg = r"Found 2 error\(s\) in command output \(output: error found, test failed\)"
565556
write_file(logfile, '')
566557
init_logging(logfile, silent=True)
567558
self.assertErrorRegex(EasyBuildError, expected_error_msg, check_log_for_errors, input_text, [
568559
r"\berror\b",
569560
(r"\ballowed-test failed\b", IGNORE),
570-
(re.compile(r"\bCRASHED\b", re.I), WARN),
561+
(r"(?i)\bCRASHED\b", WARN),
571562
"fail"
572563
])
573564
stop_logging(logfile)
574-
expected_error_msg = "Found 1 potential errors in command output (output: the process crashed with 0)"
565+
expected_error_msg = "Found 1 potential error(s) in command output (output: the process crashed with 0)"
575566
self.assertTrue(expected_error_msg in read_file(logfile))
576567

577568

0 commit comments

Comments
 (0)