-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrt_log.py
More file actions
103 lines (87 loc) · 3.9 KB
/
crt_log.py
File metadata and controls
103 lines (87 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import re
from .messenger import Messenger
def next_sub(sub_iter):
return next(sub_iter)
class CRTLog(object):
"""Identify components of CRT run based on log results"""
def __init__(self, log_file_handle, institute=None, config=None):
"""
:param log_file_handle: File hindle for the log
:type log_file_handle: file_like
"""
if type(log_file_handle) is str:
log_file_handle = open(log_file_handle)
self.log_contents = log_file_handle.read()
self.config = config
self.institute = institute
@property
def successfully_completed(self):
"""True if the log file indicates that the run completed successfully"""
if re.search('Collab builder has successfully completed', self.log_contents) is not None:
return True
else:
return False
def send_warnings_message(self):
"""
Send message based on warnings in the log
.. note:: Only sends message when there is at least one warning to send.
.. note:: Expects there to be a crt_warning message in config
.. note:: Expects the body of the message to have the text WARNINGS_LIST
"""
warning_list = self.warnings_in_log()
if len(warning_list) > 0:
m = Messenger(config=self.config)
config = self.config
joined_warnings = "\n".join(warning_list)
full_body = re.sub('WARNINGS_LIST', joined_warnings,
config.email_text('crt_warning', 'body'))
m.send_email(from_email=config.from_email,
from_name=config.email_from_name,
to_name=config.info_by_institute(
self.institute, 'ddm_name'),
to_email=config.info_by_institute(
self.institute, 'ddm_email'),
subject=config.email_text('crt_warning', 'subject'),
body=full_body)
def warnings_in_log(self):
"""
List all warnings present in log
:return: list of strings of user friendly warnings
.. note:: source of warnings is from the config object. All \
warnings are wrapped in an <li> tag for html formatted emails
"""
warning_list = list()
config = self.config
for warning in config.crt_warnings:
new_warnings = self._user_friendly_warning_from_log(warning)
if len(new_warnings) > 0:
new_formatted_warnings = [ "<li>" + w + "</li>" for w in new_warnings]
warning_list = warning_list + new_formatted_warnings
return warning_list
def _user_friendly_warning_from_log(self, warning):
config = self.config
warning_re = re.compile(warning)
if re.search(warning_re, self.log_contents) is not None:
# If warning has groups, then substitute in the user friendly
# string
if warning_re.groups > 1:
warn_pat = config.user_friendly_warning(warning)
groups = warning_re.findall(self.log_contents)
uf_warnings = list()
for group in groups:
iter_groups = iter(group)
uf_warnings.append(re.sub(
r"\b(X)\b", lambda p: next_sub(iter_groups), warn_pat))
return uf_warnings
if warning_re.groups > 0:
groups = warning_re.findall(self.log_contents)
uf_warnings = list()
for group in groups:
uf_warnings.append(re.sub(
r"\b(X)\b", group, config.user_friendly_warning(warning)))
return uf_warnings
else:
return [config.user_friendly_warning(warning)]
else:
# Return empty list if no matches found
return list()