-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationDaemon.py
418 lines (358 loc) · 14.7 KB
/
NotificationDaemon.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Copyright (C) 2012-2020 MIPS Tech LLC
# Written by Matthew Fortune <[email protected]> and
# Daniel Sanders <[email protected]>
# This file is part of Overtest.
#
# Overtest is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Overtest is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with overtest; see the file COPYING. If not, write to the Free
# Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.
"""
Module for processing history and sending notifications to users
"""
import sys
from OvertestExceptions import formatExceptionInfo, ResultSubmissionException
from LogManager import LogManager
from TestManager import Testrun
from utils import Growl, Email
from socket import gaierror
# ------------------------------------------------------------------------------
# class NotificationDaemon
# ------------------------------------------------------------------------------
class NotificationDaemon:
"""
Sends notifications to users using e-mail or growl
"""
MAIL_HOST = "HHCAA01.hh.imgtec.org"
MAIL_PORT = 25
MAIL_POSTMASTER = "[email protected]"
MAIL_FROM_ADDR = "[email protected]"
def __init__(self, ovtDB):
self.ovtDB = ovtDB
self.log = LogManager("notification_daemon", True)
self.ovtDB.registerLog(self.log)
def run(self):
"""
Main function
"""
mailer = Email.Mailer(NotificationDaemon.MAIL_HOST,
NotificationDaemon.MAIL_PORT,
NotificationDaemon.MAIL_POSTMASTER)
# get history and notifyTypes
history = self.ovtDB.getHistory()
notifyTypeCache = self.ovtDB.getNotifyTypes() # to have id->name mapping
historyComplete = []
for historyEntry in history:
self.handleHistoryEntry(historyEntry, notifyTypeCache, mailer)
historyComplete.append(historyEntry['historyid'])
# cleanup
self.ovtDB.setHistorySent(historyComplete)
del mailer
def handleHistoryEntry(self, historyEntry, notifyTypeCache, mailer):
"""
Gets related addresses, creates and sends reports.
"""
# 1) Get addresses
growlAddresses = self.ovtDB.getGrowlsToBeNotified(historyEntry['historyid'])
emailAddresses = self.ovtDB.getEmailsToBeNotified(historyEntry['historyid'])
if (len(growlAddresses) == 0) and (len(emailAddresses) == 0):
return
# 2) Create relevant object for report generation
report = self.createReportObject(historyEntry, notifyTypeCache)
# 3) Generate and send reports!
self.sendGrowls(growlAddresses, notifyTypeCache, historyEntry, report)
self.sendEmails(emailAddresses, report, mailer)
def createReportObject(self, historyEntry, notifyTypeCache):
"""
Return appropriate object for historyEntry
"""
relatedEntities = self.ovtDB.getHistoryRelatedEntites(historyEntry['historyid'])
notifyTypeName = notifyTypeCache[historyEntry['notifytypeid']]['notifytypename']
if notifyTypeName in ("Testrun Status Change",
"Testrun Status Change Verbose",
"Testsuite Completed"):
return HistoryEntry(self.ovtDB, historyEntry,
notifyTypeCache, relatedEntities)
elif notifyTypeName == "Testrun Group Completed":
testrungroupid = relatedEntities['testrungroupid']
return TestgroupComplete(self.ovtDB, testrungroupid)
else:
raise NotImplementedError("Not implemented notifyTypeName = %s" % notifyTypeName)
def sendGrowls(self, growlAddresses, notifyTypeCache, historyEntry, report):
"""
Creates and sends growl notifications to all addresses
"""
if len(growlAddresses) > 0:
for growlAddr in growlAddresses:
notifyTypeName = notifyTypeCache[historyEntry['notifytypeid']]['notifytypename']
try:
grl = Growl.GrowlNotifier(applicationName = "Overtest",
notifications = [notifyTypeName],
defaultNotifications = [0],
hostname = growlAddr['growlhost'],
password = growlAddr['growlpassword'])
grl.register()
grl.notify(notifyTypeName, report.genGrowlTitle(), report.genGrowl())
except gaierror:
pass
def sendEmails(self, emailAddresses, report, mailer):
"""
Creates and sends emails containing reports to all addresses
"""
if len(emailAddresses) > 0:
for emailAddr in emailAddresses:
mailer.sendHtmlEmail(NotificationDaemon.MAIL_FROM_ADDR,
emailAddr['email'],
report.genGrowlTitle(),
report.genEmail(),
"This is an HTML email. No text version available")
# ------------------------------------------------------------------------------
# class GenerateReportInterface
# ------------------------------------------------------------------------------
class GenerateReportInterface:
def genEmail(self):
"""
Return generated email
"""
raise NotImplementedError("GenerateReportInterface::genEmail")
def genGrowlTitle(self):
"""
Return generated growl title
"""
raise NotImplementedError("GenerateReportInterface::genGrowlTitle")
def genGrowl(self):
"""
Return generated growl
"""
raise NotImplementedError("GenerateReportInterface::genGrowl")
# ------------------------------------------------------------------------------
# class TestgroupComplete(GenerateReportInterface)
# ------------------------------------------------------------------------------
class TestgroupComplete(GenerateReportInterface):
"""
Generates report for notifytypename = TestgroupComplete
"""
def __init__(self, ovtDB, testrungroupid):
"""
Body of report is created in __init__.
"""
self.ovtDB = ovtDB
# Below members are used to generate email or growl
self.testrungroupId = testrungroupid
self.testrungroupName = self.ovtDB.getTestrungroupDescription(testrungroupid)
self.testruns = self.ovtDB.getTestrunsFromTestgroup(testrungroupid)
self.errorReport = ""
self.reportHtml = "none"
# Collect results
actionObjList = self.getActionsWithResults()
if len(actionObjList) > 0: # Store all summary results in self.resultsList
summaryList = []
for action in actionObjList.values(): # Format each summary
summaryList.append(action.formatMultiResults())
# Combine summaries from all testsuites together into one report
self.reportHtml = ""
for summary in summaryList:
self.reportHtml += summary
self.reportHtml += "\n<hr>\n\n" # separator
# report is ready!
def getActionsWithResults(self):
"""
Construct all action objects and populate each with all result sets
"""
# Below dictionaries keep intermediate data (not formatted results)
actionObjList = {} # values = action object
for testrunid in self.testruns: # testruns from testrungroup
testsuites = self.ovtDB.getTestsuitesInTestrun(testrunid)
for testsuiteid in testsuites: # testsuites from testrun
actionid = testsuites[testsuiteid]
versionedactionid = self.ovtDB.getVersionedActionid(testrunid, actionid)
# Fetch results related to particular action
actionObject = self.getActionObject(actionid, testrunid, versionedactionid)
if actionObject is None:
continue
actionResults = self.fetchActionResults(actionObject)
if actionResults is None:
continue
# If this is a first encountered action then put it in the list
if not actionid in actionObjList:
actionObjList[actionid] = actionObject
actionObjList[actionid].addMultiResults(testrunid, actionResults)
return actionObjList
def getActionObject(self, actionid, testrunid, versionedactionid):
"""
Returns A<actionid> object
"""
task = None
testrun = Testrun(self.ovtDB, testrunid=testrunid, logDB=self.ovtDB.log)
try:
exec("from action.A%d import *" % actionid)
exec("task = A%d((testrun, versionedactionid, None, actionid))" % (actionid))
exec("del(sys.modules['action.A%d'])" % actionid)
except ImportError:
self.errorReport += "Failed to import action (A%d)\n%s\n" \
% (actionid, formatExceptionInfo())
task = None
except SyntaxError:
self.errorReport += "Failed to import action (A%d) because of syntax error\n%s\n" \
% (actionid, formatExceptionInfo())
task = None
except Exception:
self.errorReport += "Unknown exception when importing action (A%d)\n%s\n" \
% (actionid, formatExceptionInfo())
task = None
return task
def fetchActionResults(self, actionObject):
"""
Fetches results from an action without formatting.
Also uses extendActionResults() function to add some fields.
"""
actionResults = None
try:
actionResults = actionObject.actionQuery()
if actionResults is not None:
self.extendActionResults(actionResults, actionObject)
except ResultSubmissionException, e:
self.errorReport += "Failed to extract results for action (A%d)\n%s\n" \
% (actionObject.actionid, formatExceptionInfo())
return actionResults
def extendActionResults(self, actionResults, actionObject):
"""
This function adds "heading" field to results so formatted tables are
understandable for human.
Currently it adds core name as heading for Verify CSIM so user
will be able to distinguish table columns with results in the report.
TODO: this functionality should be in specialised version of actionQuery
"""
if actionObject.name == "Verify CSIM":
actionResults["heading"] = actionObject.config.getVariable("Target Board")
def genEmail(self):
email = "<html>\n"+\
"<head>\n"+\
("<title> %s </title>\n" % self.testrungroupName) +\
"</head>\n"+\
"<body>\n"
email += self.reportHtml
email += "</body>\n"+\
"</html>"
return email
def genGrowlTitle(self):
title = "Testrun Group Complete"
return title
def genGrowl(self):
text = "Testgroup name: %s\n" % self.testrungroupName
text += "Testgroup id: %d" % self.testrungroupId
return text
# ------------------------------------------------------------------------------
# class HistoryEntry(GenerateReportInterface)
# ------------------------------------------------------------------------------
class HistoryEntry(GenerateReportInterface):
"""
Stores information about a history entry and can generate various reports
for it
"""
def __init__(self, ovtDB, entry, notifytypecache, relatedEntities):
"""
Initialise a history entity
"""
self.entry = entry
self.notifytypeid = entry['notifytypeid']
self.ovtDB = ovtDB
self.notifytypecache = notifytypecache
self.relatedEntities = relatedEntities
self.growltitle = None
self.growl = None
self.email = None
def format(self, text):
"""
Format the reports filling out the special identifiers as below
%from% Fetches the old value as text
%to% Fetches the new value as text
%updateid% Fetches the most closely associated entityid
%date% Fetches the date of the history entry
%<fieldid>% Fetched the entityid
%[<fieldid>]% Fetches the textual representation of the entityid
$testsuite% Fetches results for the associated testsuiteid
"""
text = text.replace("%date%", self.entry['eventdate'])
text = text.replace("%from%", self.entry['fromvalue'])
text = text.replace("%to%", self.entry['tovalue'])
text = text.replace("%updateid%", str(self.entry['updateid']))
for field in self.relatedEntities:
entityid = self.relatedEntities[field]
text = text.replace("%%%s%%" % field, str(entityid))
description = self.ovtDB.getNotifyEntityDescription(field, entityid)
text = text.replace("%%[%s]%%" % field, description)
if "%testsuite%" in text:
results = self.results()
text = text.replace("%testsuite%", results)
return text
def results(self):
"""
Fetch the testsuite results as processed text
"""
task = None
actionid = \
self.ovtDB.getActionForTestsuite(self.relatedEntities['testsuiteid'])
if actionid == None:
return "Unknown actionid %d" % actionid
testrun = Testrun(self.ovtDB, testrunid=self.relatedEntities['testrunid'],
logDB=self.ovtDB.log)
try:
exec("from action.A%d import *" % actionid)
exec("task = A%d((testrun, None, None, actionid)) # %s" % (actionid, str(testrun)))
exec("del(sys.modules['action.A%d'])" % actionid)
except ImportError:
return "Failed to import action (A%d)\n%s" \
% (actionid, formatExceptionInfo())
except SyntaxError:
return "Failed to import action (A%d) because of syntax error\n%s" \
% (actionid, formatExceptionInfo())
except Exception:
return "Unknown exception when importing action (A%d)\n%s" \
% (actionid, formatExceptionInfo())
try:
formattedResult = task.formatResults()
except ResultSubmissionException, e:
formattedResult = "Testsuite formatting error:\n%s" % str(e)
return formattedResult
def genGrowlTitle(self):
"""
Generate the growl title
"""
if self.growltitle == None:
cache = self.notifytypecache[self.notifytypeid]
self.growltitle = self.format(cache['growltitletemplate'])
return self.growltitle
def genGrowl(self):
"""
Generate the growl report
"""
if self.growl == None:
cache = self.notifytypecache[self.notifytypeid]
self.growl = self.format(cache['growltemplate'])
return self.growl
def genEmail(self):
"""
Generate the email report
"""
if self.email == None:
cache = self.notifytypecache[self.notifytypeid]
self.email = "<html>\n" +\
"<head>\n" +\
("<title>%s</title>\n" % (self.format(cache['growltitletemplate']))) +\
"</head>\n" +\
"<body>\n"
self.email += self.format(cache['emailtemplate'])
self.email += "</body>\n" +\
"</html>"
return self.email