Debian Codegraph @

cmake/tribits/ci_support/CDashQueryAnalyzeReport.py

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 try:
11 # Python 2
12 from urllib2 import urlopen
13 from urllib2 import quote as urlquote
14 except ImportError:
15 # Python 3
16 from urllib.request import urlopen
17 from urllib.parse import quote as urlquote
18
19 import sys
20 import hashlib
21 import json
22 import datetime
23 import copy
24 import pprint
25 import csv
26
27 from FindGeneralScriptSupport import *
28 from GeneralScriptSupport import *
29 from Python2and3 import u, csvReaderNext
30
31 import cdash_build_testing_date as CBTD
32
33
34 # Accept the --date input option with values 'today', 'yesterday', or some
35 # 'YYYY-MM-DD' value.
36 #
37 def convertInputDateArgToYYYYMMDD(cdashProjectTestingDayStartTime, dateText,
38 currentDateTimeStr=None, # Used for unit testing only
39 ):
40 if dateText == "yesterday" or dateText == "today":
41 if dateText == "yesterday": dayIncr = -1
42 else: dayIncr = 0
43 dateTime = CBTD.getRelativeCDashBuildStartTimeFromCmndLineArgs(
44 currentDateTimeStr, cdashProjectTestingDayStartTime, dayIncr)
45 rtnDate = CBTD.getDateOnlyFromDateTime(dateTime)
46 else:
47 rtnDate = validateAndConvertYYYYMMDD(dateText)
48 return rtnDate
49
50
51 # Validate a date YYYY-MM-DD string and return a date object for the
52 # 'datetime' module.
53 #
54 def validateAndConvertYYYYMMDD(dateText):
55 try:
56 return datetime.datetime.strptime(dateText, '%Y-%m-%d')
57 except ValueError:
58 raise ValueError("Incorrect data format for '"+dateText+"', should be YYYY-MM-DD")
59
60
61 # Get a file name string from a general text string.
62 #
63 # This replaces non-alphanumeric chars with '_'.
64 #
65 def getFileNameStrFromText(inputStr):
66 fileNameStr = ""
67 for char in inputStr:
68 if char.isalnum():
69 fileNameStr += char
70 else:
71 fileNameStr += "_"
72 return fileNameStr
73
74
75 # Check if the key/value pairs for two dicts are the same and if so, return an
76 # error message explaining how they are different.
77 #
78 # Returns tuple (hasSameKeyValuePairs, errMsg). If
79 # hasSameKeyValuePairs==True, then errMsg==None. Otherwise, if
80 # hasSameKeyValuePairs==False, then errMsg gives a string that explains how
81 # they are different.
82 #
83 # This improves on a simple check dict_1 == dict_2 in that shows exactly why
84 # the dicts are different for a single key/value pair.
85 #
86 def checkDictsAreSame(dict_1, dict_1_name, dict_2, dict_2_name):
87 # Assume all passing unless we find a difference
88 hasSameKeyValuePairs = True
89 errMsg = None
90 # Start with the fast internal Python check
91 if dict_1 == dict_2:
92 return (True, None)
93 # Check if they have the same number of keys
94 if hasSameKeyValuePairs and (len(dict_1.keys()) != len(dict_2.keys())):
95 hasSameKeyValuePairs = False
96 errMsg = "len("+dict_1_name+".keys())="+str(len(dict_1.keys()))+\
97 " != len("+dict_2_name+".keys())="+str(len(dict_2.keys()))
98 # Check that they have the same key/value pairs
99 if hasSameKeyValuePairs:
100 for key_1 in dict_1.keys():
101 if not key_1 in dict_2.keys():
102 hasSameKeyValuePairs = False
103 errMsg = dict_1_name+"['"+key_1+"'] does not exist in "+dict_2_name
104 break
105 keyVal_1 = dict_1[key_1]
106 keyVal_2 = dict_2[key_1]
107 if keyVal_1 != keyVal_2:
108 hasSameKeyValuePairs = False
109 errMsg = dict_1_name+"['"+key_1+"'] = '"+str(keyVal_1)+"' != "+\
110 dict_2_name+"['"+key_1+"'] = '"+str(keyVal_2)+"'"
111 break
112 #end for
113 #end if
114 # Return the final result
115 return (hasSameKeyValuePairs, errMsg)
116
117
118 # Compress a long file name to avoid open() error
119 #
120 # If the full file name must be shorted and if prefix!="", then it is added to
121 # the beginning of the shortened filename. Also, if ext!="", then "."+ext is
122 # added to the end of the shortened filename. Otherwise, if inputFileName is
123 # not too long, then it is returned without modification (i.e. 'prefix' and
124 # 'ext' are ignored). NOTE: If 'prefix' and 'ext' are too long, then the
125 # returned shortened filename may also be too long.
126 #
127 # This function should return a shorter unique file name that is platform
128 # independent.
129 #
130 def getCompressedFileNameIfTooLong(inputFileName, prefix="", ext=""):
131 maxFileNameLength = 255 # ToDo: Figure out for this system?
132 if len(inputFileName) > maxFileNameLength:
133 hashObject = hashlib.sha1(str(inputFileName).encode('utf-8'))
134 hashStr = hashObject.hexdigest()
135 newFileName = prefix+hashObject.hexdigest()
136 if ext: newFileName += "." + ext
137 return newFileName
138 return inputFileName
139
140
141 # Filter and input list and return a list with elements where
142 # matchFunctor(inputList[i])==True.
143 #
144 def getFilteredList(inputList, matchFunctor):
145 filteredList = []
146 for ele in inputList:
147 if matchFunctor(ele): filteredList.append(ele)
148 return filteredList
149
150
151 # Filter an input list returning a two lists (matchList, nomatchList) where
152 # the first list has elements where matchFunctor(inputList[i])==True and the
153 # second list has elements where matchFunctor(inputList[i])==False.
154 #
155 def splitListOnMatch(inputList, matchFunctor):
156 #print("\nsplitListOnMatch(): matchFunctor = "+str(matchFunctor))
157 matchList = []
158 nomatchList = []
159 for ele in inputList:
160 if matchFunctor(ele): matchList.append(ele)
161 else: nomatchList.append(ele)
162 return (matchList, nomatchList)
163
164
165 # DECORATOR match functor class that negates the match of a stored functor.
166 #
167 class NotMatchFunctor(object):
168
169 # Construct with another functor to negate
170 def __init__(self, matchFunctor):
171 self.__matchFunctor = matchFunctor
172
173 # Convert to string rep for debugging/etc.
174 def __str__(self):
175 myStr = "NotMatchFunctor{"+str(self.__matchFunctor)+"}"
176 return myStr
177
178 # Negate the matchFunctor
179 def __call__(self, item):
180 return (self.__matchFunctor(item) == False)
181
182
183 # Apply a functor to transform every element in a list
184 #
185 # The object transformFunctor is applied as:
186 #
187 # list_inout[i] = transformFunctor(list_inout[i])
188 #
189 # If the elements are small value-type objects, then the assignment is needed.
190 # However, if the list elements are handled with reference semantics like a
191 # list [] or a dict {} then really the object is being modified in place and
192 # the assignment is not needed but it cheap and harmess in that case.
193 #
194 # This returns the input list transformed but the return object can be ignored
195 # because it modifies the input list object's elements in place.
196 #
197 def foreachTransform(list_inout, transformFunctor):
198 for i in range(len(list_inout)):
199 list_inout[i] = transformFunctor(list_inout[i])
200 return list_inout
201
202
203 # Remove elements from a list given a list of indexes
204 #
205 # This modifies the original list inplace but also returns it. Therefore, if
206 # you want to keep the original list, you better create a copy of the base
207 # list object before passing it in.
208 #
209 def removeElementsFromListGivenIndexes(list_inout, indexesToRemoveList_in):
210 indexesToRemoveList = copy.copy(indexesToRemoveList_in)
211 indexesToRemoveList.sort()
212 numRemoved = 0
213 for index in indexesToRemoveList:
214 del list_inout[index-numRemoved]
215 numRemoved += 1
216 return list_inout
217
218
219 # Class CsvFileStructure
220 #
221 class CsvFileStructure(object):
222
223 def __init__(self, headersList, rowsList):
224 self.headersList = headersList
225 self.rowsList = rowsList
226
227
228 # Write a CsvFileStructure data to a string
229 #
230 def writeCsvFileStructureToStr(csvFileStruct):
231 csvFileStr = ", ".join(csvFileStruct.headersList)+"\n"
232 for rowFieldsList in csvFileStruct.rowsList:
233 csvFileStr += ", ".join(rowFieldsList)+"\n"
234 return csvFileStr
235
236
237 ########################################
238 # CDash Specific stuff
239 ########################################
240
241
242 #
243 # Reporting policy, data, and defaults
244 #
245
246
247 # Collection of data used to create the final HTML CDash report that is
248 # updated and queried by various functions.
249 #
250 # NOTE: This is put into a class object so that these vars can be updated in
251 # place when passed to a function.
252 #
253 class CDashReportData(object):
254 def __init__(self):
255 # Gives the final result (assume passing by default)
256 self.globalPass = True
257 # This is the top of the HTML body
258 self.htmlEmailBodyTop = ""
259 # This is the bottom of the email body
260 self.htmlEmailBodyBottom = ""
261 # This var will store the list of data numbers for the summary line
262 self.summaryLineDataNumbersList = []
263 def reset(self):
264 self.globalPass = True
265 self.htmlEmailBodyTop = ""
266 self.htmlEmailBodyBottom = ""
267 self.summaryLineDataNumbersList = []
268
269
270 # Define standard CDash colors
271 def cdashColorPassed(): return 'green'
272 def cdashColorFailed(): return 'red'
273 def cdashColorNotRun(): return 'orange'
274 def cdashColorMissing(): return 'gray'
275 # ToDo: Make the above return different colors for a color-blind palette
276
277
278 def getStandardTestsetAcroList():
279 return ['twoif', 'twoinr', 'twip', 'twim', 'twif', 'twinr']
280
281
282 # Aggregate info about a test set used for generating the summary and table
283 # and to determine global pass/fail.
284 #
285 # Members:
286 #
287 # * testsetAcro: e.g. 'twoif'
288 # * testsetDescr: e.g. "Tests without issue trackers Failed"
289 # * testsetTableType: Values: 'nopass', 'pass', 'missing'
290 # * testsetColor: e.g. 'red', 'green' (whatever is accepted by function
291 # colorHtmlText())
292 # * existanceTriggersGlobalFail: If 'True' and any of tests fall into this
293 # test-set category, then it shoulid trigger a global 'False'
294 class TestsetTypeInfo(object):
295
296 def __init__(self, testsetAcro, testsetDescr, testsetTableType, testsetColor,
297 existanceTriggersGlobalFail=True,
298 ):
299 self.testsetAcro = testsetAcro
300 self.testsetDescr = testsetDescr
301 self.testsetTableType = testsetTableType
302 self.testsetColor = testsetColor
303 self.existanceTriggersGlobalFail = existanceTriggersGlobalFail
304
305
306 # Return the TestsetTypeInfo object for the standard types of test sets that get
307 # their own tables.
308 #
309 # testsetArco [in] Acronym for the standard test set (e.g. 'twoif')
310 #
311 # testsetColor [in] Gives the color to use for the summary line and the table
312 # header. If 'None' is passed in (the default), then a standard color is
313 # used. If the empty string is passed in '', then no color will be applied.
314 #
315 def getStandardTestsetTypeInfo(testsetAcro, testsetColor=None):
316 if testsetAcro == "twoif":
317 tsti = TestsetTypeInfo(testsetAcro, "Tests without issue trackers Failed", 'nopass',
318 cdashColorFailed())
319 elif testsetAcro == "twoinr":
320 tsti = TestsetTypeInfo(testsetAcro, "Tests without issue trackers Not Run", 'nopass',
321 cdashColorNotRun())
322 elif testsetAcro == "twip":
323 tsti = TestsetTypeInfo(testsetAcro, "Tests with issue trackers Passed", 'pass',
324 cdashColorPassed(), existanceTriggersGlobalFail=False)
325 elif testsetAcro == "twim":
326 tsti = TestsetTypeInfo(testsetAcro, "Tests with issue trackers Missing", 'missing',
327 cdashColorMissing(), existanceTriggersGlobalFail=False)
328 elif testsetAcro == "twif":
329 tsti = TestsetTypeInfo(testsetAcro, "Tests with issue trackers Failed", 'nopass',
330 cdashColorFailed())
331 elif testsetAcro == "twinr":
332 tsti = TestsetTypeInfo(testsetAcro, "Tests with issue trackers Not Run", 'nopass',
333 cdashColorNotRun())
334 else:
335 raise Exception("Error, testsetAcro = '"+str(testsetAcro)+"' is not supported!")
336
337 if testsetColor != None:
338 tsti.testsetColor = testsetColor
339
340 return tsti
341
342
343 # Return the 'status' field from a test dict
344 #
345 # Return 'Not Run' if the 'status' field is missing. (This happens with one
346 # customer's tests apparently, see SESW-383.)
347 #
348 def getTestDictStatusField(testDict):
349 return testDict.get('status', 'Not Run')
350
351
352 # Get the Test-set acronym from the fields of a test dict
353 #
354 def getTestsetAcroFromTestDict(testDict):
355 issueTracker = testDict.get('issue_tracker', None)
356 if isTestFailed(testDict) and issueTracker == None:
357 return 'twoif'
358 if isTestNotRun(testDict) and issueTracker == None:
359 return 'twoinr'
360 if isTestPassed(testDict) and issueTracker != None:
361 return 'twip'
362 if isTestMissing(testDict) and issueTracker != None:
363 return 'twim'
364 if isTestFailed(testDict) and issueTracker != None:
365 return 'twif'
366 if isTestNotRun(testDict) and issueTracker != None:
367 return 'twinr'
368 raise Exception(
369 "Error, testDict = '"+str(testDict)+"' with fields"+\
370 " status = '"+str(testDict.get('status', None))+"' and"+\
371 " issue_tracker = '"+str(testDict.get('issue_tracker', None))+"'"+\
372 " is not a supported test-set type!")
373
374
375 # Returns True if a test has 'status' 'Passed'
376 def isTestPassed(testDict):
377 return (testDict.get('status', None) == 'Passed')
378
379
380 # Returns True if a test has 'status' 'Failed'
381 def isTestFailed(testDict):
382 return (testDict.get('status', None) == 'Failed')
383
384
385 # Returns True if a test has 'status' 'Not Run'
386 def isTestNotRun(testDict):
387 return (testDict.get('status', None) == 'Not Run')
388
389
390 # Return True if a test is missing
391 def isTestMissing(testDict):
392 status = testDict.get('status', None)
393 if status == 'Missing': return True
394 if status == 'Missing / Failed': return True
395 return False
396
397
398 # Define default test dicts sort order in tables
399 def getDefaultTestDictsSortKeyList() : return ['testname', 'buildName', 'site']
400
401
402 #
403 # Implementation functions
404 #
405
406
407 # Given a CDash query URL PHP page that returns JSON data, return the JSON
408 # data converged to a Python data-structure.
409 #
410 # The returned Python object will be a simple nested set of Python dicts and
411 # lists.
412 #
413 # NOTE: This function can't really be unit tested because it actually gets
414 # data from CDash. Therefore, the code below will be structured such that it
415 # we can avoid getting call it in any automated tests.
416 #
417 def extractCDashApiQueryData(cdashApiQueryUrl):
418 if sys.version_info < (2,7,5):
419 raise Exception("Error: Must be using Python 2.7.5 or newer")
420 # NOTE: If we use Python 2.6.6. then the urllib2 function crashes!
421 response = urlopen(cdashApiQueryUrl)
422 return json.load(response)
423
424
425 # Read a CSV file into a list of dictionaries for each row where the rows of
426 # the output list are dicts with the column names as keys.
427 #
428 # For example, for the CSV file:
429 #
430 # col_0, col_1, col_2
431 # val_00, val_01, val_02
432 # val_10, val_11, val_12
433 #
434 # the returned list of dicts will be:
435 #
436 # [
437 # { 'col_0':'val_00', 'col_1':'val_01', 'col_2':'val_02' },
438 # { 'col_0':'val_10', 'col_1':'val_11', 'col_2':'val_12' },
439 # ]
440 #
441 # This function can also allow the user to assert that the included columns
442 # match a set of required and optional headers. For example, that above CSV
443 # file would match:
444 #
445 # requiredColumnHeadersList = [ 'col_0', 'col_1', 'col_2' ]
446 #
447 # or:
448 #
449 # requiredColumnHeadersList = [ 'col_0', 'col_1' ]
450 # optionalColumnHeadersList = [ 'col_2', 'col_3', ]
451 #
452 # The requiredColumnHeadersList and optionalColumnHeadersList argument lists
453 # are optional.
454 #
455 # Also, the columns can be appear in any order as long as they match all of
456 # the required headers and don't contain any headers not in the list of
457 # expected headers.
458 #
459 def readCsvFileIntoListOfDicts(csvFileName, requiredColumnHeadersList=[],
460 optionalColumnHeadersList=[],
461 ):
462 listOfDicts = []
463 with open(csvFileName, 'r') as csvFile:
464 csvReader = csv.reader(csvFile)
465 columnHeadersList = getColumnHeadersFromCsvFileReader(csvFileName, csvReader)
466 assertExpectedColumnHeadersFromCsvFile(csvFileName, requiredColumnHeadersList,
467 optionalColumnHeadersList, columnHeadersList)
468 # Read the rows of the CSV file into dicts
469 dataRow = 0
470 for lineList in csvReader:
471 if not lineList: continue # Ignore blank line
472 stripWhiltespaceFromStrList(lineList)
473 assertExpectedNumColsFromCsvFile(csvFileName, dataRow, lineList,
474 columnHeadersList)
475 # Read the row entries into a new dict
476 rowDict = {}
477 for j in range(len(columnHeadersList)):
478 rowDict.update( { columnHeadersList[j] : lineList[j] } )
479 listOfDicts.append(rowDict)
480 # Update for next row
481 dataRow += 1
482 # Return the constructed object
483 return listOfDicts
484
485
486 def getColumnHeadersFromCsvFileReader(csvFileName, csvReader):
487 try:
488 columnHeadersList = csvReaderNext(csvReader)
489 stripWhiltespaceFromStrList(columnHeadersList)
490 return columnHeadersList
491 except StopIteration:
492 raise Exception(
493 "Error, CSV file '"+csvFileName+"' is empty which is not allowed!"
494 )
495
496
497 def csvReaderNext(csvReader):
498 if sys.version_info < (3,):
499 return csvReader.next()
500 else:
501 return next(csvReader)
502
503
504 def assertExpectedColumnHeadersFromCsvFile(csvFileName, requiredColumnHeadersList,
505 optionalColumnHeadersList, columnHeadersList,
506 ):
507
508 if not requiredColumnHeadersList and not optionalColumnHeadersList:
509 return # No expected column headers to assert against!
510
511 requiredAndOptionalHeadersSet = set(requiredColumnHeadersList)
512 requiredAndOptionalHeadersSet.update(optionalColumnHeadersList)
513 columnHeadersSet = set(columnHeadersList)
514
515 # Assert that each column header is expected
516 for colHeader in columnHeadersList:
517 if not colHeader in requiredAndOptionalHeadersSet:
518 raise Exception(
519 "Error, for CSV file '"+csvFileName+"' the"+\
520 " column header '"+str(colHeader)+"' is not in the set"+\
521 " of required column headers '"+str(requiredColumnHeadersList)+"'"+\
522 " or optional column headers '"+str(optionalColumnHeadersList)+"'!"+\
523 ""
524 )
525
526 # Assert that all of the required headers are present
527 for requiredHeader in requiredColumnHeadersList:
528 if not requiredHeader in columnHeadersSet:
529 raise Exception(
530 "Error, for CSV file '"+csvFileName+"' the"+\
531 " required header '"+str(requiredHeader)+"' is missing from the"+\
532 " set of included column headers '"+str(columnHeadersList)+"'!"+\
533 ""
534 )
535
536
537 def assertExpectedNumColsFromCsvFile(csvFileName, dataRow, lineList,
538 columnHeadersList,
539 ):
540 if len(lineList) != len(columnHeadersList):
541 raise Exception(
542 "Error, for CSV file '"+csvFileName+"' the data row"+\
543 " "+str(dataRow)+" "+str(lineList)+" has"+\
544 " "+str(len(lineList))+" entries which does not match"+\
545 " the number of column headers "+str(len(columnHeadersList))+"!")
546
547
548 def stripWhiltespaceFromStrList(strListInOut):
549 for i in range(len(strListInOut)): strListInOut[i] = strListInOut[i].strip()
550
551
552 g_expectedBuildsCsvFileHeadersRequired = \
553 ('group', 'site', 'buildname')
554
555
556 def getExpectedBuildsListOfDictsfromCsvFile(expectedBuildsFileName):
557 return readCsvFileIntoListOfDicts(expectedBuildsFileName,
558 g_expectedBuildsCsvFileHeadersRequired)
559
560
561 def getExpectedBuildsListOfDictsFromCsvFileArg(expectedBuildsFileArg):
562 expectedBuildsLOD = []
563 if expectedBuildsFileArg:
564 expectedBuildsFilenameList = expectedBuildsFileArg.split(",")
565 for expectedBuildsFilename in expectedBuildsFilenameList:
566 expectedBuildsLOD.extend(
567 getExpectedBuildsListOfDictsfromCsvFile(expectedBuildsFilename))
568 return expectedBuildsLOD
569
570
571 # Write list of builds from a builds LOD to a CSV file structure meant to
572 # match the expected builds CSV file.
573 #
574 def expectedBuildsListOfDictsToCsvFileStructure(buildsLOD):
575 csvFileHeadersList = copy.deepcopy(g_expectedBuildsCsvFileHeadersRequired)
576 csvFileRowsList = []
577 for buildDict in buildsLOD:
578 csvFileRow = (
579 buildDict['group'],
580 buildDict['site'],
581 buildDict['buildname'],
582 )
583 csvFileRowsList.append(csvFileRow)
584 return CsvFileStructure(csvFileHeadersList, csvFileRowsList)
585
586
587 # Write list of builds from a builds LOD to a CSV file meant to match the
588 # expected builds CSV file.
589 #
590 def writeExpectedBuildsListOfDictsToCsvFile(buildsLOD, csvFileName):
591 csvFileStruct = expectedBuildsListOfDictsToCsvFileStructure(buildsLOD)
592 with open(csvFileName, 'w') as csvFile:
593 csvFile.write(writeCsvFileStructureToStr(csvFileStruct))
594
595
596 g_testsWithIssueTrackersCsvFileHeadersRequired = \
597 ('site', 'buildName', 'testname', 'issue_tracker_url', 'issue_tracker')
598
599
600 def getTestsWtihIssueTrackersListFromCsvFile(testsWithIssueTrackersFile):
601 return readCsvFileIntoListOfDicts(testsWithIssueTrackersFile,
602 g_testsWithIssueTrackersCsvFileHeadersRequired)
603
604
605 # Write list of tests from a Tests LOD to a CSV file structure meant to match
606 # tests with issue trackers CSV file.
607 #
608 def writeTestsListOfDictsToCsvFileStructure(testsLOD,
609 issueTrackerUrl="", issueTracker="",
610 ):
611 csvFileHeadersList = copy.deepcopy(g_testsWithIssueTrackersCsvFileHeadersRequired)
612 csvFileRowsList = []
613 for testDict in testsLOD:
614 csvFileRow = (
615 testDict['site'],
616 testDict['buildName'],
617 testDict['testname'],
618 issueTrackerUrl, # issue_tracker_url
619 issueTracker, # issue_tracker
620 )
621 csvFileRowsList.append(csvFileRow)
622 return CsvFileStructure(csvFileHeadersList, csvFileRowsList)
623
624
625 # Write list of tests from a Tests LOD to a CSV file meant to match tests with
626 # issue trackers CSV file.
627 #
628 def writeTestsListOfDictsToCsvFile(testsLOD, csvFileName):
629 csvFileStruct = writeTestsListOfDictsToCsvFileStructure(testsLOD)
630 with open(csvFileName, 'w') as csvFile:
631 csvFile.write(writeCsvFileStructureToStr(csvFileStruct))
632
633
634 # Pretty print a nested Python data-structure to a file
635 #
636 # ToDo: Reimplement this to create a better looking set of indented that that
637 # involves less right-drift and the expense of more vertical space.
638 #
639 def pprintPythonDataToFile(pythonData, filePath):
640 with open(filePath,'w') as fileObj:
641 pp = pprint.PrettyPrinter(stream=fileObj, indent=2)
642 pp.pprint(pythonData)
643
644
645 # Get data off CDash and cache it or read from previously cached data
646 #
647 # If useCachedCDashData == True, then the file cdashQueryDataCacheFile must
648 # exist and will be used to get the data instead of calling CDash
649 #
650 # If alwaysUseCacheFileIfExists==True and the file cdashQueryDataCacheFile
651 # already exists, then the file cdashQueryDataCacheFile will be used to get
652 # the dta instead of calling CDash.
653 #
654 # Otherwise, CDash will be called at cdashQueryUrl to get the data and then
655 # the data will be written to the the file cdashQueryDataCacheFile if
656 # cdashQueryDataCacheFile != None.
657 #
658 # This function can be used to get data off of CDash using any page on CDash
659 # including cdash/api/v1/index.php, cdash/api/v1/queryTests.php and anything
660 # other PHP page that returns a JSON data structure (which is all of the
661 # cdash/api/v1/XXX.php pages).
662 #
663 def getAndCacheCDashQueryDataOrReadFromCache(
664 cdashQueryUrl,
665 cdashQueryDataCacheFile, # File name
666 useCachedCDashData, # If 'True', then cdasyQueryDataCacheFile must be non-null
667 alwaysUseCacheFileIfExists = False,
668 verbose = False,
669 extractCDashApiQueryData_in=extractCDashApiQueryData,
670 ):
671 if (
672 alwaysUseCacheFileIfExists \
673 and cdashQueryDataCacheFile \
674 and os.path.exists(cdashQueryDataCacheFile) \
675 ):
676 if verbose:
677 print(" Since the file exists, using cached data from file:\n"+\
678 " "+cdashQueryDataCacheFile )
679 with open(cdashQueryDataCacheFile, 'r') as cacheFile:
680 cdashQueryData=eval(cacheFile.read())
681 elif useCachedCDashData:
682 if verbose:
683 print(" Using cached data from file:\n "+cdashQueryUrl )
684 with open(cdashQueryDataCacheFile, 'r') as cacheFile:
685 cdashQueryData=eval(cacheFile.read())
686 else:
687 if verbose:
688 print(" Downloading CDash data from:\n "+cdashQueryUrl )
689 cdashQueryData = extractCDashApiQueryData_in(cdashQueryUrl)
690 if cdashQueryDataCacheFile:
691 if verbose:
692 print(" Caching data downloaded from CDash to file:\n "+\
693 cdashQueryDataCacheFile)
694 pprintPythonDataToFile(cdashQueryData, cdashQueryDataCacheFile)
695 return cdashQueryData
696
697
698 def normalizeUrlStrings(*args):
699 return [urlquote(x) for x in args]
700
701
702 # Construct full cdash/api/v1/index.php query URL to pull data down given the
703 # pieces
704 def getCDashIndexQueryUrl(cdashUrl, projectName, date, filterFields):
705 # for legacy reasons, this function assumes we normalized projectName
706 projectName, = normalizeUrlStrings(projectName,)
707 if date: dateArg = "&date="+date
708 else: dateArg = ""
709 return cdashUrl+"/api/v1/index.php?project="+projectName+dateArg \
710 + "&"+filterFields
711
712
713 # Construct full cdash/index.php browser URL given the pieces
714 def getCDashIndexBrowserUrl(cdashUrl, projectName, date, filterFields):
715 # for legacy reasons, this function assumes we normalized projectName
716 projectName, = normalizeUrlStrings(projectName,)
717 if date: dateArg = "&date="+date
718 else: dateArg = ""
719 return cdashUrl+"/index.php?project="+projectName+dateArg \
720 + "&"+filterFields
721
722
723 # Construct full cdash/api/v1/queryTests.php query URL given the pieces
724 def getCDashQueryTestsQueryUrl(cdashUrl, projectName, date, filterFields):
725 # for legacy reasons, this function assumes we normalized projectName
726 projectName, = normalizeUrlStrings(projectName,)
727 if date: dateArg = "&date="+date
728 else: dateArg = ""
729 cdashTestUrl = cdashUrl+"/api/v1/queryTests.php?project="+projectName+dateArg+"&"+filterFields
730 return cdashTestUrl
731
732
733 # Construct full cdash/queryTests.php browser URL given the pieces
734 def getCDashQueryTestsBrowserUrl(cdashUrl, projectName, date, filterFields):
735 # for legacy reasons, this function assumes we normalized projectName
736 projectName, = normalizeUrlStrings(projectName,)
737 if date: dateArg = "&date="+date
738 else: dateArg = ""
739 return cdashUrl+"/queryTests.php?project="+projectName+dateArg+"&"+filterFields
740
741
742 # Construct full cdash/api/v1/buildSummary.php query URL given the buildId
743 def getCDashBuildSummaryQueryUrl(cdashUrl, buildId):
744 return cdashUrl+"/api/v1/buildSummary.php?buildid="+buildId
745
746
747 # Construct full cdash/build browser URL given the buildId
748 def getCDashBuildSummaryBrowserUrl(cdashUrl, buildId):
749 return cdashUrl+"/build/"+buildId
750
751
752 # Copy a key/value pair from one dict to another if it eixsts
753 def copyKeyDictIfExists(sourceDict_in, keyName_in, dict_inout):
754 value = sourceDict_in.get(keyName_in, None)
755 if value:
756 dict_inout.update( { keyName_in : value } )
757
758
759 # Extend the set of fields for a CDash index.phpb build dict
760 #
761 # buildDict_in [in]: The build dict gotten from cdash/index.php. This will be
762 # modified in place.
763 #
764 # Returns the modified build dict.
765 #
766 # Change this to get all of the fields and add the 'group' field as well.
767 #
768 def extendCDashIndexBuildDict(buildDict_in, groupName):
769 buildDict = buildDict_in
770 buildDict[u'group'] = groupName
771 return buildDict
772
773
774 # Given the full Python JSON data-structure returned from the page
775 # cdash/api/v1/index.php query from extractCDashApiQueryData(), return a
776 # flattened-out data-structure that is easier to manipulate.
777 #
778 # This function takes in the JSON data-structure (as a nested set of Python
779 # dicts and listed) directly returned from a query gotten from the page
780 # cdash/api/v1/index.php with some filters.
781 #
782 # The input full CDash index.php JSON data-structure has the following
783 # structure and fields of interest:
784 #
785 # fullCDashIndexBuildsJson =
786 # {
787 # 'all_buildgroups': [ {'id':1,'name:"Nightly"}, ...],
788 # 'buildgroups': [
789 # {
790 # 'name':"???", # group name, e.g. Nightly
791 # 'builds":[
792 # {
793 # 'site':"???"
794 # 'buildname':"???",
795 # 'update': {'errors':???, ...},
796 # 'configure':{'error': ???, ...},
797 # 'compilation':{'error': ???, ...},
798 # 'test': {'fail':???, 'notrun':???, 'pass':???, ...},
799 # ...
800 # },
801 # ...
802 # ]
803 # },
804 # ...
805 # ...
806 # ]
807 # },
808 # ...
809 # }
810 #
811 # This function gets the data from *all* of the collapsed builds and returns
812 # the flatten-out list of dicts for each build with the 'group' field added in
813 # as:
814 #
815 # [
816 # {
817 # 'group':"???",
818 # 'site':"???",
819 # 'buildname':"???",
820 # 'update': {'errors':???, ...},
821 # 'configure':{'error': ???, ...},
822 # 'compilation':{'error': ???, ...},
823 # 'test': {'fail':???, 'notrun':???, 'pass':???, ...},
824 # ...
825 # },
826 # ...
827 # ]
828 #
829 # This collects *all* of the builds from all of the build groups provided by
830 # that data-structure, not just the 'Nighlty' build group. Therefore, if you
831 # want to only consider one set of build groups, you need to add that to the
832 # CDash query URL (e.g. group='Nighlty').
833 #
834 def flattenCDashIndexBuildsToListOfDicts(fullCDashIndexBuildsJson):
835 summaryCDashIndexBuilds = []
836 for buildgroup in fullCDashIndexBuildsJson["buildgroups"]:
837 groupName = buildgroup["name"]
838 for build in buildgroup["builds"]:
839 summaryBuild = extendCDashIndexBuildDict(build, groupName)
840 summaryCDashIndexBuilds.append(summaryBuild)
841 return summaryCDashIndexBuilds
842
843
844 # Given the full JSON data-structure returned from the page
845 # cdash/api/v1/queryTests.php query from extractCDashApiQueryData(), return a
846 # flattened-out data-structure that is easier to manipulate.
847 #
848 # This function takes in the JSON data-structure (as a nested set of Python
849 # dicts and listed) directly returned from a query gotten from the page
850 # cdash/api/v1/queryTests.php with some filters.
851 #
852 # The input full CDash queryTests.php JSON data-structure has the following
853 # structure and fields of interest:
854 #
855 # fullCDashQueryTestsJson =
856 # {
857 # 'version':???,
858 # 'feed_enabled':???,
859 # ...
860 # 'builds': [
861 # {
862 # 'buildName': 'Trilinos-atdm-mutrino-intel-opt-openmp-HSW',
863 # 'buildSummaryLink': 'buildSummary.php?buildid=4109735',
864 # 'buildstarttime': '2018-10-29T05:54:03 UTC',
865 # 'details': 'Completed (Failed)\n',
866 # 'nprocs': 4,
867 # 'prettyProcTime': '40s 400ms',
868 # 'prettyTime': '10s 100ms',
869 # 'procTime': 40.4,
870 # 'site': 'mutrino',
871 # 'siteLink': 'viewSite.php?siteid=223',
872 # 'status': 'Failed',
873 # 'statusclass': 'error',
874 # 'testDetailsLink': 'testDetails.php?test=57925465&build=4109735',
875 # 'testname': 'Anasazi_Epetra_BKS_norestart_test_MPI_4',
876 # 'time': 10.1
877 # },
878 # ...
879 # ],
880 # ...
881 # }
882 #
883 # This function gets the data from *all* of the tests and returns the
884 # flatten-out list of dicts with some additional fields for each test of the
885 # form:
886 #
887 # [
888 # {
889 # 'buildName': 'Trilinos-atdm-mutrino-intel-opt-openmp-HSW',
890 # 'buildSummaryLink': 'buildSummary.php?buildid=4109735',
891 # 'buildstarttime': '2018-10-29T05:54:03 UTC',
892 # 'details': 'Completed (Failed)\n',
893 # 'nprocs': 4,
894 # 'prettyProcTime': '40s 400ms',
895 # 'prettyTime': '10s 100ms',
896 # 'procTime': 40.4,
897 # 'site': 'mutrino',
898 # 'siteLink': 'viewSite.php?siteid=223',
899 # 'status': 'Failed',
900 # 'statusclass': 'error',
901 # 'testDetailsLink': 'testDetails.php?test=57925465&build=4109735',
902 # 'testname': 'Anasazi_Epetra_BKS_norestart_test_MPI_4',
903 # 'time': 10.1,
904 # },
905 # ...
906 # ]
907 #
908 # NOTE: This does a shallow copy so any modifications to the returned list and
909 # dicts will modify the original data-structure fullCDashQueryTestsJson. If
910 # that is a problem, then make sure and do a deep copy before passing in
911 # fullCDashQueryTestsJson.
912 #
913 # This collects *all* of the tests from all of the "build" list provided by
914 # the CDash JSON data-structure. Therefore, if you want to only consider one
915 # set of build groups, you need to add that to the CDash query URL
916 # (e.g. buildName='<build-name>').
917 #
918 def flattenCDashQueryTestsToListOfDicts(fullCDashQueryTestsJson):
919 testsListOfDicts = []
920 for testDict in fullCDashQueryTestsJson['builds']:
921 testsListOfDicts.append(testDict)
922 return testsListOfDicts
923
924
925 # Create a lookup dict for a list of dicts
926 #
927 # listOfDicts [in/out]: List of dict objects that have keys that one will want
928 # to lookup the dict based on their values. May have 100% duplicate elements
929 # removed from the list.
930 #
931 # listOfKeys [in]: List of the names of keys in these dicts that are used to
932 # build a search dict data-structure which is returned from this function.
933 #
934 # removeExactDuplicateElements [in]: If True, then dict elements that are 100%
935 # duplicates and have the exact same key/value pairs will be removed from
936 # listOfDicts. (default False)
937 #
938 # checkDictsAreSame_in [in]: Allows specialization of the check for exact dict
939 # matches and reporting the differences. The default value is the function
940 # checkDictsAreSame(). Any Python object that has the __call__() operator
941 # function defined that takes those same arguments and returns the same
942 # outputs as the function checkDictsAreSame() can be passed in.
943 #
944 # If listOfDicts has any elements that are 100% complete duplicates with the
945 # same exact key/value pairs, then the later elements will be removed from the
946 # list. But if just the key/value pairs listed in listOfKeys are duplicated
947 # but one or more of the other key/value pairs is different, then then an
948 # exception is thrown.
949 #
950 # NOTE: This is an implementation function that is used in the class
951 # SearchableListOfDicts. Please use that class instead of this raw function.
952 #
953 def createLookupDictForListOfDicts(listOfDicts, listOfKeys,
954 removeExactDuplicateElements=False, checkDictsAreSame_in=checkDictsAreSame,
955 ):
956 # Build the lookup dict data-structure. Also, optionally mark any 100%
957 # duplicate elements if asked to remove 100% duplicate elements.
958 lookupDict = {} ; idx = 0 ; numRemoved = 0 ; duplicateIndexesToRemoveFromList = []
959 for dictEle in listOfDicts:
960 # Create the structure of recursive dicts for the keys in order
961 currentLookupDictRef = lookupDict
962 lastLookupDictRef = None
963 for key in listOfKeys:
964 keyValue = dictEle[key]
965 lastLookupDictRef = currentLookupDictRef
966 nextLookupDictRef = currentLookupDictRef.setdefault(keyValue, {})
967 currentLookupDictRef = nextLookupDictRef
968 addEle = True
969 # Check to see if this dict has already been added
970 if currentLookupDictRef:
971 lookedUpDict = currentLookupDictRef.get('dict', None)
972 lookedUpIdx = currentLookupDictRef.get('idx', None)
973 (hasSameKeyValuePairs, dictDiffErrorMsg) = checkDictsAreSame_in(
974 dictEle, "listOfDicts["+str(idx)+"]",
975 lookedUpDict, "listOfDicts["+str(lookedUpIdx)+"]" )
976 if hasSameKeyValuePairs and removeExactDuplicateElements:
977 # This is a 100% duplicate element to one previously added.
978 # Therefore, mark this duplicate element to be removed.
979 duplicateIndexesToRemoveFromList.append(idx)
980 addEle = False
981 else:
982 raiseDuplicateDictEleException(idx, dictEle, listOfKeys, lookedUpIdx,
983 lookedUpDict, dictDiffErrorMsg)
984 # Need to go back and reset the dict on the last dict in the
985 # data-structure so that modifications to the dicts that are looked up
986 # will modify the original list.
987 if addEle:
988 currentLookupDictRef.update({'dict':dictEle, 'idx':idx-numRemoved})
989 else:
990 numRemoved += 1
991 idx += 1
992 # Remove 100% duplicate elements marked above
993 removeElementsFromListGivenIndexes(listOfDicts, duplicateIndexesToRemoveFromList)
994 return lookupDict
995
996
997 def raiseDuplicateDictEleException(idx, dictEle, listOfKeys,
998 lookedUpIdx, lookedUpDict, dictDiffErrorMsg,
999 ):
1000 raise Exception(
1001 "Error, The element\n\n"+\
1002 " listOfDicts["+str(idx)+"] =\n\n"+\
1003 " "+sorted_dict_str(dictEle)+"\n\n"+\
1004 " has duplicate values for the list of keys\n\n"+\
1005 " "+str(listOfKeys)+"\n\n"+\
1006 " with the element already added\n\n"+\
1007 " listOfDicts["+str(lookedUpIdx)+"] =\n\n"+\
1008 " "+sorted_dict_str(lookedUpDict)+"\n\n"+\
1009 " and differs by at least the key/value pair\n\n"+\
1010 " "+str(dictDiffErrorMsg))
1011
1012
1013 # Lookup a dict (and optionally also its index location) in a list of dicts
1014 # given a lookup dict returned from createLookupDictForListOfDicts() where the
1015 # key/value pairs match
1016 #
1017 # lookupDict [in]: A dict created by createLookupDictForListOfDicts() given
1018 # the same listOfKeys used in that function.
1019 #
1020 # listOfKeys [in]: List of keys that was used used to create lookupDict.
1021 #
1022 # listOfValues [in]: A list of values for the given list of keys in
1023 # listOfKeys.
1024 #
1025 # alsoReturnIdx [in]: If True, then the index of the located dict in the
1026 # original listOfDicts will be returned as well. (default False)
1027 #
1028 # If the matching dict is found, then it will be returned as:
1029 #
1030 # matchingDict = lookupDictGivenLookupDict(...)
1031 #
1032 # If alsoReturnIdx==True, then also the index will be returned as:
1033 #
1034 # (matchingDict, idx) = lookupDictGivenLookupDict(...)
1035 #
1036 # If the matching dict is not found, then None will be returned or the tuple
1037 # (None, None) if alsoReturnIdx==True.
1038 #
1039 # NOTE: This is an implementation function that is used in the class
1040 # SearchableListOfDicts. Please use that class instead of this raw function.
1041 #
1042 def lookupDictGivenLookupDict(lookupDict, listOfKeys, listOfValues,
1043 alsoReturnIdx=False,
1044 ):
1045 #print("\nlookupDict = "+str(lookupDict))
1046 #print("\nlistOfKeys = "+str(listOfKeys))
1047 #print("\ndictToFind = "+str(dictToFind))
1048 if len(listOfKeys) != len(listOfValues):
1049 raise Exception("Error, len(listOfKeys)="+str(len(listOfKeys))+\
1050 " != len(listOfValues)="+str(len(listOfValues))+" where"+\
1051 " listOfKeys="+str(listOfKeys)+\
1052 " and listOfValues="+str(listOfValues)+"!")
1053 currentSubLookupDict = lookupDict
1054 idx = 0
1055 for idx in range(len(listOfValues)):
1056 key = listOfKeys[idx]
1057 #print("\nkey = '"+key+"'")
1058 keyValueToFind = listOfValues[idx]
1059 #print("keyValueToFind = '"+str(keyValueToFind)+"'")
1060 #print("currentSubLookupDict = "+str(currentSubLookupDict))
1061 keyValueLookedUp = currentSubLookupDict.get(keyValueToFind, None)
1062 #print("keyValueLookedUp = "+str(keyValueLookedUp))
1063 if not keyValueLookedUp:
1064 if alsoReturnIdx: return (None, None)
1065 return None
1066 currentSubLookupDict = keyValueLookedUp
1067 if keyValueLookedUp:
1068 if alsoReturnIdx:
1069 return (keyValueLookedUp.get('dict'), keyValueLookedUp.get('idx'))
1070 return keyValueLookedUp.get('dict')
1071 return None
1072
1073
1074 # Class that encapsulates a list of dicts and an efficient lookup of a dict
1075 # given a list key/value pairs to match
1076 #
1077 # Once created, this object acts like a list of dicts in most cases but also
1078 # contains functions to search for specific dicts given a set of key/value
1079 # pairs.
1080 #
1081 # Any modifications to the dicts looked up with this object will edit the
1082 # dicts in the underlying list of dicts. This therefore makes this class act
1083 # as a type of multi-key lookup dict using the member function
1084 # lookupDictGivenKeyValuesList(['keyval0', 'keyval1', ...]). This provides a
1085 # handy way to access and edit the underlying dicts that require
1086 # multi-key/value pairs to find them.
1087 #
1088 # NOTE: The key values for the list of keys given in listOfKeys must be
1089 # unique! If it is not, then an exception will be thrown.
1090 #
1091 class SearchableListOfDicts(object):
1092
1093 # Constructor
1094 #
1095 # listOfDicts [stored, may be modified]: List of dicts that a search
1096 # data-structure will be created for.
1097 #
1098 # listOfKeys [stored, will not be modified]: List of the names of keys in
1099 # the dicts of listOfDicts that a search data-structure will be created for
1100 # and defines the set of key/value pairs used to look up up dicts in
1101 # listOfDicts.
1102 #
1103 # removeExactDuplicateElements [in]: If True, then exact duplicate dicts
1104 # based on the key/value pairs in listOfKeys will be removed from
1105 # listOfDicts (which is modified in place, not a copy). (default False)
1106 #
1107 # keyMapList [in]: Optional list of key names in the input
1108 # keyValueDictToFind to pull out and used the match the key/value pairs in
1109 # the listOfKeys. This allows a mapping from int input dict key names to
1110 # the output dict key names. (default None)
1111 #
1112 # checkDictsAreSame_in [in]: Allows specialization of the check for exact
1113 # dict matches and reporting the differences. The default value is the
1114 # function checkDictsAreSame(). Any Python object that has the __call__()
1115 # operator function defined that takes those same arguments and returns the
1116 # same outputs as the function checkDictsAreSame() can be passed in.
1117 #
1118 def __init__(self, listOfDicts, listOfKeys,
1119 removeExactDuplicateElements=False, keyMapList=None,
1120 checkDictsAreSame_in=checkDictsAreSame,
1121 ):
1122 if keyMapList:
1123 if len(listOfKeys) != len(keyMapList):
1124 raise Exception("Error, listOfKeys="+str(listOfKeys)+\
1125 " keyMapList="+str(listOfKeys)+" have different lengths!" )
1126 self.__listOfDicts = listOfDicts
1127 self.__listOfKeys = listOfKeys
1128 self.__keyMapList = keyMapList
1129 self.__checkDictsAreSame = checkDictsAreSame_in
1130 self.__lookupDict = createLookupDictForListOfDicts(
1131 self.__listOfDicts, self.__listOfKeys,
1132 removeExactDuplicateElements=removeExactDuplicateElements,
1133 checkDictsAreSame_in=checkDictsAreSame_in)
1134
1135 # Convert to string rep
1136 def __str__(self):
1137 myStr = "SearchableListOfDicts{listOfDicts="+str(self.__listOfDicts)+\
1138 ", listOfKeys="+str(self.__listOfKeys)+", lookupDict="+str(self.__lookupDict)+"}"
1139 return myStr
1140
1141 # Return listOfDicts passed into Constructor
1142 def getListOfDicts(self):
1143 return self.__listOfDicts
1144
1145 # Return listOfKeys passed to Constructor
1146 def getListOfKeys(self):
1147 return self.__listOfKeys
1148
1149 # Return keyMapList passed to Constructor
1150 def getKeyMapList(self):
1151 return self.__keyMapList
1152
1153 # Lookup a dict given a dict with same key/value pairs for keys listed in
1154 # listOfKeys.
1155 def lookupDictGivenKeyValueDict(self, keyValueDictToFind, alsoReturnIdx=False):
1156 if self.__keyMapList:
1157 keyListToUse = self.__keyMapList
1158 else:
1159 keyListToUse = self.__listOfKeys
1160 keyValuesListToFind = []
1161 for idx in range(len(keyListToUse)):
1162 keyValuesListToFind.append(keyValueDictToFind.get(keyListToUse[idx]))
1163 lookupRtn = self.lookupDictGivenKeyValuesList(keyValuesListToFind, alsoReturnIdx)
1164 return lookupRtn
1165
1166 # Lookup a dict given a flat list of values for the keys
1167 #
1168 # Must be in same order self.getListOfKeys().
1169 #
1170 def lookupDictGivenKeyValuesList(self, keyValuesListToFind, alsoReturnIdx=False):
1171 lookupRtn = lookupDictGivenLookupDict(self.__lookupDict, self.__listOfKeys,
1172 keyValuesListToFind, alsoReturnIdx)
1173 return lookupRtn
1174
1175 # Functions to allow this to act like a list
1176 def __len__(self):
1177 return len(self.__listOfDicts)
1178 def __getitem__(self, index_in):
1179 return self.__listOfDicts[index_in]
1180
1181
1182 # Create a SearchableListOfDicts object for a list of builds dicts that allows
1183 # lookups of builds given the keys "group" => "site" => "buildname" :
1184 # build_dict.
1185 def createSearchableListOfBuilds(buildsListOfDicts):
1186 return SearchableListOfDicts(buildsListOfDicts, ['group', 'site', 'buildname'])
1187
1188
1189 # Create a SearchableListOfDicts object for a list of tests with issue
1190 # trackers that allows lookups of tests given the keys "site" => "buildName"
1191 # => "testname" : test_dict.
1192 def createSearchableListOfTests( testsListOfDicts,
1193 removeExactDuplicateElements=False,
1194 checkDictsAreSame_in=checkDictsAreSame,
1195 ):
1196 return SearchableListOfDicts(testsListOfDicts, ['site', 'buildName', 'testname'],
1197 removeExactDuplicateElements=removeExactDuplicateElements,
1198 checkDictsAreSame_in=checkDictsAreSame_in )
1199
1200
1201 # Create a SearchableListOfDicts object for a list of build dicts allows
1202 # lookups that match the 'site' and 'buildname' fields but uses input for the
1203 # search that are test dicts that have the fields 'site' and 'buildName'.
1204 def createTestToBuildSearchableListOfDicts(buildsLOD,
1205 removeExactDuplicateElements=False,
1206 ):
1207 return SearchableListOfDicts( buildsLOD, ('site', 'buildname'),
1208 removeExactDuplicateElements=removeExactDuplicateElements,
1209 keyMapList=('site', 'buildName') )
1210 # NOTE: The extra keyMapList is needed because CDash used the key name
1211 # 'buildname' for the build name returned form the cdash/index.php page
1212 # while it gave the build name the key name 'buildName' for the data
1213 # returned from cdash/queryTests.php.
1214
1215
1216 # Match functor that returns true if the input dict has key/values that
1217 # matches one of the dicts in the input SearchableListOfDicts.
1218 class MatchDictKeysValuesFunctor(object):
1219
1220 # Construct with a SearchableListOfDicts object
1221 def __init__(self, searchableListOfDict):
1222 self.__searchableListOfDict = searchableListOfDict
1223
1224 # Convert to string rep for debugging/etc.
1225 def __str__(self):
1226 myStr = "MatchDictKeysValuesFunctor{"+str(self.__searchableListOfDict)+"}"
1227 return myStr
1228
1229 # Return 'true' if the key/value pairs in dict_in match the key/value pairs
1230 # in one of the dicts in the searchableListOfDict object.
1231 def __call__(self, dict_in):
1232 matchingDict = self.__searchableListOfDict.lookupDictGivenKeyValueDict(dict_in)
1233 if matchingDict:
1234 return True
1235 return False
1236
1237
1238 # Transform functor that adds issue tracker info and URL to an existing test
1239 # dict.
1240 #
1241 # This functor looks up the test based on 'site', 'buildName', and 'testname'
1242 # keys to find the entry in the list of known issues with issue trackers and
1243 # then it copies the issue issue tracker fields to the input/output test dict.
1244 class AddIssueTrackerInfoToTestDictFunctor(object):
1245
1246 # Construct with a SearchableListOfDicts object that has issue tracker info.
1247 # This object testsWithIssueTrackersSLOD must have been constructed using
1248 # the function createSearchableListOfTests() so it will allow lookups based
1249 # on the 'site', 'buildName', and 'testname' keys.
1250 def __init__(self, testsWithIssueTrackersSLOD, addEmptyOnNoMatch=True):
1251 self.__testsWithIssueTrackersSLOD = testsWithIssueTrackersSLOD
1252 self.__addEmptyOnNoMatch = addEmptyOnNoMatch
1253
1254 # Lookup the issue tracker info and add it as new key/value pairs to
1255 # testDict_inout.
1256 def __call__(self, testDict_inout):
1257 # Look up the entry for the test tracker info based on the 'site',
1258 # 'buildName', and 'testname' key/value pairs in testDict_inout.
1259 matchingDict = \
1260 self.__testsWithIssueTrackersSLOD.lookupDictGivenKeyValueDict(testDict_inout)
1261 if matchingDict:
1262 issue_tracker = matchingDict['issue_tracker']
1263 issue_tracker_url = matchingDict['issue_tracker_url']
1264 else:
1265 if self.__addEmptyOnNoMatch:
1266 issue_tracker = ""
1267 issue_tracker_url = ""
1268 else:
1269 raise Exception(
1270 "Error, testDict_inout="+str(testDict_inout)+\
1271 " does not have an assigned issue tracker!")
1272 testDict_inout[u'issue_tracker'] = issue_tracker
1273 testDict_inout[u'issue_tracker_url'] = issue_tracker_url
1274 return testDict_inout
1275
1276
1277 # Split a list of Test Dicts based on if they match the set of expected builds
1278 # or not.
1279 #
1280 # testsWithIssueTrackersLOD [in]: List of dicts of tests with issue trackers.
1281 # Here, only the fields 'site', 'buildName', and 'testname' are significant.
1282 #
1283 # expectedBuildsLOD [in]: List of dicts of expected builds with fields 'site'
1284 # and 'buildname'. Here, the key/value pairs 'site' and 'buildname' must be
1285 # unique. The 'group' field is ignored (because cdash/queryTests.php does
1286 # not give the 'group' of each test).
1287 #
1288 # Returns the tuple (testsWithIssueTrackersMatchingExpectedBuildsLOD,
1289 # testsWithIssueTrackersNotMatchingExpectedBuildsLOD).
1290 #
1291 def splitTestsOnMatchExpectedBuilds( testsWithIssueTrackersLOD,
1292 testToExpectedBuildsSLOD,
1293 ):
1294 return splitListOnMatch(testsWithIssueTrackersLOD,
1295 MatchDictKeysValuesFunctor(testToExpectedBuildsSLOD) )
1296
1297
1298 # Check if the list of tests with issue trackers matches the expected builds.
1299 #
1300 # testsWithIssueTrackersLOD [in]: List of dicts of tests with issue trackers.
1301 # Here, only the fields 'site', 'buildName', and 'testname' are significant.
1302 #
1303 # expectedBuildsLOD [in]: List of dicts of expected builds with fields 'site'
1304 # and 'buildname'. Here, the key/value pairs 'site' and 'buildname' must be
1305 # unique. The 'group' field is ignored (because cdash/queryTests.php does
1306 # not give the 'group' of each test).
1307 #
1308 # This returns a tuple (matches, errMsg). If all of the tests match, then
1309 # 'matches' will be 'True' and errMsg=="". If one or more of the tests don't
1310 # match the expected builds then 'matches' will be 'False' and 'errMsg' will
1311 # give a message about which tests are missing.
1312 #
1313 def doTestsWithIssueTrackersMatchExpectedBuilds( testsWithIssueTrackersLOD,
1314 testToExpectedBuildsSLOD,
1315 ):
1316 # Get list of tests matching and non-matching expected builds (ignoring
1317 # matching tests list)
1318 (_, nomatchingTestsLOD) = \
1319 splitTestsOnMatchExpectedBuilds(testsWithIssueTrackersLOD,
1320 testToExpectedBuildsSLOD)
1321 # Gather up all of the tests that don't match one of the expected builds
1322 nonmatchingTestsWithIssueTrackersLOD = []
1323 for testDict in nomatchingTestsLOD:
1324 nonmatchingTestsWithIssueTrackersLOD.append(
1325 {'site':testDict['site'], 'buildName':testDict['buildName'],
1326 'testname':testDict['testname']} )
1327 # If all tests matched, return True
1328 if len(nonmatchingTestsWithIssueTrackersLOD) == 0:
1329 return (True, "")
1330 # One or more tests did not match so build an error message and return False
1331 errMsg = \
1332 "Error: The following tests with issue trackers did not match 'site' and"+\
1333 " 'buildName' in one of the expected builds:\n"
1334 for testDict in nonmatchingTestsWithIssueTrackersLOD:
1335 errMsg += \
1336 " {'site'='"+testDict['site']+"'"+\
1337 ", 'buildName'="+testDict['buildName']+"'"+\
1338 ", 'testname'="+testDict['testname']+"'}\n"
1339 return (False, errMsg)
1340
1341
1342 # Extract just the date from the testDict['buildstartdate'] field
1343 def dateFromBuildStartTime(buildStartTime):
1344 return buildStartTime.split('T')[0]
1345
1346
1347 # Sort list of test history dicts and get statistics
1348 #
1349 # Inputs:
1350 #
1351 # testHistoryLOD [in]: List of test dicts for the same test. Neither this
1352 # list nor its elements are modified in this call. The base list object is
1353 # shallow copied before it is sorted and returned.
1354 #
1355 # currentTestDate [in]: The current testing day (as a string "YYYY-MM-DD").
1356 # This is needed to define a frame of reference for interpreting if the test
1357 # is currently 'Passed', 'Failed', 'Not Run', or is 'Missing' (i.e. does not
1358 # have any test results for current testing date).
1359 #
1360 # testingDayStartTimeUtc [in]: The CDash project testing day start time
1361 # "<hh>:<mm>" in UTC. For example, if the CDash project testing day start
1362 # time is 6 PM MDT (18:00 MDT), then the testing day start time is "02:00"
1363 # (UTC) (which is the next calendar day).
1364 #
1365 # daysOfHistory [in]: Number of days of history that were requested.
1366 #
1367 # Note that len(testHistoryLOD) may be less than daysOfHistory which is
1368 # allowed and handled in function. Any days in that range missing contribute
1369 # to testHistoryStats['missing_last_x_days'].
1370 #
1371 # Also note that this function will remove any duplicate tests (which seem to
1372 # occur sometimes due to a defect in CDash).
1373 #
1374 # Returns:
1375 #
1376 # (sortedTestHistoryLOD, testHistoryStats, testStatus)
1377 #
1378 # where:
1379 #
1380 # sortedTestHistoryLOD: The sorted list of test dicts with most recent dict
1381 # at the top. New list object with references to the same test dict
1382 # elements. Therefore, if the list elements themselves are modified after
1383 # the function returns, then the elements in the original list testHistoryLOD
1384 # will be modified as well.
1385 #
1386 # testHistoryStats: Dict that gives statistics for the test with fields:
1387 # - 'pass_last_x_days': Number of times test 'Passed'
1388 # - 'nopass_last_x_days': Number of times the not 'Passed'
1389 # - 'missing_last_x_days': Number of days there was no test data
1390 # - 'consec_pass_days': Number of times the test consecutively passed
1391 # - 'consec_nopass_days': Number of times the test consecutively did not pass
1392 # - 'consec_missing_days': Number of days test is missing
1393 # - 'previous_nopass_date': Before current date, the previous nopass date in UTC
1394 #
1395 # testStatus: The status of the test for the current testing day with values:
1396 # - 'Passed': Most recent test 'Passed' had date matching curentTestDate
1397 # - 'Failed': Most recent test 'Failed' had date matching curentTestDate
1398 # - 'Not Run': Most recent test 'Not Run' had date matching curentTestDate
1399 # - 'Missing': Most recent test has date before matching curentTestDate
1400 #
1401 def sortTestHistoryGetStatistics(testHistoryLOD,
1402 currentTestDate, testingDayStartTimeUtc,
1403 daysOfHistory,
1404 ):
1405
1406 # Helper functions
1407 def incr(testDict, key): testDict[key] = testDict[key] + 1
1408 def decr(testDict, key): testDict[key] = testDict[key] - 1
1409
1410 # Initialize outputs assuming no history (i.e. test is missing for
1411 # alldaysOfHistory of history)
1412 sortedTestHistoryLOD = []
1413 testHistoryStats = {
1414 'pass_last_x_days': 0,
1415 'nopass_last_x_days': 0,
1416 'missing_last_x_days': daysOfHistory,
1417 'consec_pass_days': 0,
1418 'consec_nopass_days': 0,
1419 'consec_missing_days': 0,
1420 'previous_nopass_date': 'None'
1421 }
1422 testStatus = "Missing"
1423
1424 # Return if there is no test history
1425 if len(testHistoryLOD) == 0:
1426 testHistoryStats['consec_missing_days'] = daysOfHistory
1427 return (sortedTestHistoryLOD, testHistoryStats, testStatus)
1428
1429 # Sort the test history by the buildstarttime (most current date at top)
1430 sortedTestHistoryLOD = copy.copy(testHistoryLOD)
1431 sortedTestHistoryLOD.sort(reverse=True, key=DictSortFunctor(['buildstarttime']))
1432
1433 # Remove duplicate tests from list of dicts
1434 sortedTestHistoryLOD = getUniqueSortedTestsHistoryListOfDicts(sortedTestHistoryLOD)
1435
1436 # Get testing day/time helper object
1437 testingDayTimeObj = CBTD.CDashProjectTestingDay(currentTestDate, testingDayStartTimeUtc)
1438 currentTestDateDT = testingDayTimeObj.getCurrentTestingDayDateDT()
1439 #print("currentTestDateDT = "+str(currentTestDateDT))
1440
1441 # Top (most recent) test history data
1442 topTestDict = sortedTestHistoryLOD[0]
1443 #print("topTestDict['buildstarttime'] = "+topTestDict['buildstarttime'])
1444
1445 # Get the CDash testing date of the most recent test
1446 topTestDictTestingDayDT = testingDayTimeObj.getTestingDayDateFromBuildStartTimeDT(
1447 topTestDict['buildstarttime'] )
1448 #print("topTestDictTestingDayDT ="+str(topTestDictTestingDayDT))
1449
1450 # testStatus (for this test based on history)
1451 if topTestDictTestingDayDT == currentTestDateDT:
1452 testStatus = getTestDictStatusField(topTestDict)
1453 else:
1454 testStatus = "Missing"
1455 #print("testStatus = '"+testStatus+"'")
1456
1457 # testHistoryStats
1458
1459 # Set up for counting num of consecutive pass, nopass, or missing
1460
1461 if testStatus == "Missing":
1462 # The test is missing so see how many consecutive days that it is missing
1463 testHistoryStats['consec_missing_days'] = \
1464 (currentTestDateDT - topTestDictTestingDayDT).days
1465 # There are no initial consecutive passing or nopassing days
1466 initialTestStatusHasChanged = True
1467 else:
1468 # Count number of consecutive days that test is either passing or
1469 # nopasssing
1470 initialTestStatusHasChanged = False
1471
1472 if testStatus == 'Passed': previousTestStatusPassed = True
1473 else: previousTestStatusPassed = False
1474
1475 previousNopassDate = None
1476
1477 # Loop over test history for each of the kth entries and update quantities
1478 for pastTestDict_k in sortedTestHistoryLOD:
1479 pastTestStatus_k = getTestDictStatusField(pastTestDict_k)
1480 pastTestDateUtc_k = testingDayTimeObj.getTestingDayDateFromBuildStartTimeStr(
1481 pastTestDict_k['buildstarttime'])
1482 # Count the initial consecutive streaks for passing and nonpassing
1483 if (
1484 (pastTestStatus_k=='Passed') == previousTestStatusPassed \
1485 and not initialTestStatusHasChanged \
1486 ):
1487 # The initial consecutive streak for passing or nonpassing continues!
1488 if pastTestStatus_k == 'Passed':
1489 incr(testHistoryStats, 'consec_pass_days')
1490 else:
1491 incr(testHistoryStats, 'consec_nopass_days')
1492 else:
1493 # The initial consecutive streak has been broken
1494 initialTestStatusHasChanged = True
1495 # Count total pass/nopass/missing tests
1496 decr(testHistoryStats, 'missing_last_x_days') # Test not missing this day!
1497 if pastTestStatus_k == 'Passed':
1498 incr(testHistoryStats, 'pass_last_x_days')
1499 else:
1500 incr(testHistoryStats, 'nopass_last_x_days')
1501 # Find most recent previous nopass test date
1502 if (
1503 previousNopassDate == None \
1504 and pastTestDateUtc_k != currentTestDate \
1505 and pastTestStatus_k != 'Passed' \
1506 ):
1507 previousNopassDate = pastTestDateUtc_k
1508 testHistoryStats['previous_nopass_date'] = previousNopassDate
1509
1510 # Return the computed stuff
1511 return (sortedTestHistoryLOD, testHistoryStats, testStatus)
1512
1513
1514 # Get a new list with unique entries from an input sorted list of test dicts.
1515 #
1516 # The returned list is new and does not modify any of the entries in the input
1517 # sorted inputSortedTestHistoryLOD object.
1518 #
1519 def getUniqueSortedTestsHistoryListOfDicts(inputSortedTestHistoryLOD):
1520
1521 if len(inputSortedTestHistoryLOD) == 0:
1522 return inputSortedTestHistoryLOD
1523
1524 uniqueSortedTestHistoryLOD = []
1525
1526 lastUniqueTestDict = inputSortedTestHistoryLOD[0]
1527 uniqueSortedTestHistoryLOD.append(lastUniqueTestDict)
1528
1529 idx = 1
1530 while idx < len(inputSortedTestHistoryLOD):
1531 candidateTestDict = inputSortedTestHistoryLOD[idx]
1532
1533 if not checkCDashTestDictsAreSame(candidateTestDict, "a", lastUniqueTestDict, "b")[0]:
1534 uniqueSortedTestHistoryLOD.append(candidateTestDict)
1535 lastUniqueTestDict = candidateTestDict
1536 # Else, this is duplicate test entry so skip
1537 idx += 1
1538
1539 return uniqueSortedTestHistoryLOD
1540
1541
1542 # Extract testid and buildid from 'testDetailsLink' CDash test dict
1543 # field.
1544 def extractTestIdAndBuildIdFromTestDetailsLink(testDetailsLink):
1545 testDetailsLinkList = testDetailsLink.split('?')
1546 if (len(testDetailsLinkList) > 1):
1547 # Older CDash implementations have ?testid=<testid>&buildid=<buildid>
1548 phpArgsList = testDetailsLinkList[1].split('&')
1549 testidArgList = phpArgsList[0].split("=")
1550 buildidArgList = phpArgsList[1].split("=")
1551 testId = testidArgList[1]
1552 buildId = buildidArgList[1]
1553 else:
1554 # Newer CDash implementations have
1555 testDetailsLinkList = testDetailsLink.split('/')
1556 testId = testDetailsLinkList[1]
1557 buildId = ""
1558 return (testId, buildId)
1559
1560
1561 # Check if two test dicts returned from CDash are the same, accounting for
1562 # possible CDash defects allowing duplicate tests except for different test
1563 # IDs and small changes in 'time' (strange defects in CDash).
1564 #
1565 # Has the same calling conventions and return value as the function
1566 # checkDictsAreSame().
1567 #
1568 # Returns tuple (hasSameKeyValuePairs, errMsg). If
1569 # hasSameKeyValuePairs==True, then errMsg==None. Otherwise, if
1570 # hasSameKeyValuePairs==False, then errMsg gives a string that explains how
1571 # they are different.
1572 #
1573 # This improves on a simple check dict_1 == dict_2 in that shows exactly why
1574 # the dicts are different for a single key/value pair.
1575 #
1576 def checkCDashTestDictsAreSame(testDict_1, testDict_1_name,
1577 testDict_2, testDict_2_name,
1578 ):
1579 # Check the easy case where they are exactly the same
1580 if testDict_1 == testDict_2:
1581 return (True, None)
1582 # Check to see if 'testDetailsLink' is there in both and then check contents
1583 sameBuildIdDifferentTestIds = False
1584 if (
1585 ('testDetailsLink' in testDict_1.keys()) \
1586 and \
1587 ('testDetailsLink' in testDict_2.keys()) \
1588 ):
1589 (test1d_1, buildid_1) = \
1590 extractTestIdAndBuildIdFromTestDetailsLink(testDict_1['testDetailsLink'])
1591 (test1d_2, buildid_2) = \
1592 extractTestIdAndBuildIdFromTestDetailsLink(testDict_2['testDetailsLink'])
1593 if (buildid_1 == buildid_2) and (test1d_1 != test1d_2):
1594 # This is the special case that we are writing this function for!
1595 sameBuildIdDifferentTestIds = True
1596 # Set up copy to allow dropping out fields for comparison
1597 testDict_1_copy = copy.deepcopy(testDict_1)
1598 testDict_2_copy = copy.deepcopy(testDict_2)
1599 # If buildIds are the same but the testIds are different, then check the
1600 # rest of the key/value pairs to determine if they are the same:
1601 if sameBuildIdDifferentTestIds:
1602 testDict_1_copy.pop('testDetailsLink', None)
1603 testDict_2_copy.pop('testDetailsLink', None)
1604 # Don't require the same test times
1605 testDict_1_copy.pop('time', None)
1606 testDict_2_copy.pop('time', None)
1607 testDict_1_copy.pop('prettyTime', None)
1608 testDict_2_copy.pop('prettyTime', None)
1609 testDict_1_copy.pop('procTime', None)
1610 testDict_2_copy.pop('procTime', None)
1611 testDict_1_copy.pop('prettyProcTime', None)
1612 testDict_2_copy.pop('prettyProcTime', None)
1613 # Don't require identical matching output
1614 testDict_1_copy.pop('matchingoutput', None)
1615 testDict_2_copy.pop('matchingoutput', None)
1616 # Compare what ever fields are left that may be different and just use the
1617 # standard comparison that will give a good error message for differences.
1618 return checkDictsAreSame(testDict_1_copy, testDict_1_name,
1619 testDict_2_copy, testDict_2_name )
1620
1621
1622 # Get the test history CDash cache filename
1623 #
1624 # Note: this takes care of things like having '/' in the test name
1625 #
1626 def getTestHistoryCacheFileName(date, site, buildName, testname, daysOfHistory):
1627 testHistoryFileName = \
1628 date+"-"+site+"-"+buildName+"-"+testname+"-HIST-"+str(daysOfHistory)+".json"
1629 return testHistoryFileName.replace('/', '_')
1630
1631
1632 # Transform functor that computes and add detailed test history to an existing
1633 # test dict so that it can be printed in the table
1634 # createCDashTestHtmlTableStr().
1635 #
1636 # ToDo: Document the fields set by this functor
1637 #
1638 class AddTestHistoryToTestDictFunctor(object):
1639
1640 # Constructor which takes additional data needed to get the test history and
1641 # other stuff.
1642 #
1643 # By default, this will always read the data from the cache file if that file
1644 # already exists.
1645 #
1646 def __init__(self, cdashUrl, projectName, date, testingDayStartTimeUtc, daysOfHistory,
1647 testCacheDir, useCachedCDashData=True, alwaysUseCacheFileIfExists=True,
1648 verbose=False, printDetails=False, requireMatchTestTopTestHistory=True,
1649 extractCDashApiQueryData_in=extractCDashApiQueryData, # For unit testing
1650 ):
1651 self.__cdashUrl = cdashUrl
1652 self.__projectName = projectName
1653 self.__date = date
1654 self.__testingDayStartTimeUtc = testingDayStartTimeUtc
1655 self.__daysOfHistory = daysOfHistory
1656 self.__testCacheDir = testCacheDir
1657 self.__useCachedCDashData = useCachedCDashData
1658 self.__alwaysUseCacheFileIfExists = alwaysUseCacheFileIfExists
1659 self.__verbose = verbose
1660 self.__printDetails = printDetails
1661 self.__requireMatchTestTopTestHistory = requireMatchTestTopTestHistory
1662 self.__extractCDashApiQueryData_in = extractCDashApiQueryData_in
1663
1664 # Get test history off CDash and add test history info and URL to info we
1665 # find out from that test history
1666 #
1667 def __call__(self, testDict):
1668
1669 #pp = pprint.PrettyPrinter(indent=2)
1670
1671 #print("\ntestDict:\n")
1672 #pp.pprint(testDict)
1673
1674 # Get short names for data inside of this functor
1675 cdashUrl = self.__cdashUrl
1676 projectName = self.__projectName
1677 testDayDate = validateAndConvertYYYYMMDD(self.__date)
1678 daysOfHistory = self.__daysOfHistory
1679
1680 # Get basic info about the test from the from the testDict
1681 site = testDict["site"]
1682 buildName = testDict["buildName"]
1683 testname = testDict["testname"]
1684
1685 # Determine if this test has data from CDash or if it does not
1686 if testDict.get('buildstarttime', None):
1687 testAlreadyHasCDashData = True
1688 else:
1689 testAlreadyHasCDashData = False
1690
1691 # Get the date range for CDash queries
1692 dateRangeBeginDT = testDayDate - datetime.timedelta(days=(daysOfHistory-1))
1693 dateRangeBeginDateStr = CBTD.getDateStrFromDateTime(dateRangeBeginDT)
1694 dateRangeEndDateStr = self.__date
1695 beginEndUrlFields = "begin="+dateRangeBeginDateStr+"&end="+dateRangeEndDateStr
1696
1697 # normalize names for query
1698 projectName_url, buildName_url, testname_url, site_url = normalizeUrlStrings(
1699 projectName, buildName, testname, site)
1700
1701 # Define queryTests.php query filters for test history
1702 testHistoryQueryFilters = \
1703 beginEndUrlFields+"&"+\
1704 "filtercombine=and&filtercombine=&filtercount=3&showfilters=1&filtercombine=and"+\
1705 "&field1=buildname&compare1=61&value1="+buildName_url+\
1706 "&field2=testname&compare2=61&value2="+testname_url+\
1707 "&field3=site&compare3=61&value3="+site_url
1708
1709 # URL used to get the history of the test in JSON form
1710 testHistoryQueryUrl = \
1711 getCDashQueryTestsQueryUrl(cdashUrl, projectName_url, None, testHistoryQueryFilters)
1712
1713 # URL to embed in email to show the history of the test to humans
1714 testHistoryBrowserUrl = \
1715 getCDashQueryTestsBrowserUrl(cdashUrl, projectName_url, None, testHistoryQueryFilters)
1716
1717 # URL for to the build summary on index.php page
1718 buildHistoryEmailUrl = getCDashIndexBrowserUrl(
1719 cdashUrl, projectName_url, None,
1720 beginEndUrlFields+"&"+\
1721 "filtercombine=and&filtercombine=&filtercount=2&showfilters=1&filtercombine=and"+\
1722 "&field1=buildname&compare1=61&value1="+buildName_url+\
1723 "&field2=site&compare2=61&value2="+site_url
1724 )
1725 # ToDo: Replace this with the the URL to just this one build the index.php
1726 # page. To do that, get the build stamp from the list of builds on CDash
1727 # and then create a URL link for this one build given 'site', 'buildName',
1728 # and 'buildStamp'. (NOTE: We can't use 'buildstarttime' without
1729 # replacing ':' with '%' or the URL will not work with CDash.)
1730
1731 # Set the names of the cached files so we can check if they exists and
1732 # write them out otherwise
1733 testHistoryCacheFileFullName = \
1734 getTestHistoryCacheFileName(self.__date,site,buildName,testname,daysOfHistory)
1735 # Possibly compress the file name if it is too long
1736 testHistoryCacheFilePath = \
1737 self.__testCacheDir+"/"+\
1738 getCompressedFileNameIfTooLong(testHistoryCacheFileFullName,self.__date+"-","json")
1739
1740 if self.__verbose:
1741 gettingTestHistoryMsg = \
1742 "Getting "+str(daysOfHistory)+" days of history for "+testname+\
1743 " in the build "+buildName+" on "+site
1744 if os.path.exists(testHistoryCacheFilePath):
1745 gettingTestHistoryMsg += " from cache file"
1746 else:
1747 gettingTestHistoryMsg += " from CDash"
1748 print(gettingTestHistoryMsg)
1749
1750 # Get the test history off of CDash (or from reading the cache file)
1751 testHistoryLOD = downloadTestsOffCDashQueryTestsAndFlatten(
1752 testHistoryQueryUrl, testHistoryCacheFilePath,
1753 useCachedCDashData=self.__useCachedCDashData,
1754 alwaysUseCacheFileIfExists=self.__alwaysUseCacheFileIfExists,
1755 verbose=self.__printDetails,
1756 extractCDashApiQueryData_in=self.__extractCDashApiQueryData_in
1757 )
1758
1759 # Sort and get test history stats and update core testDict fields
1760
1761 (testHistoryLOD, testHistoryStats, testStatus) = sortTestHistoryGetStatistics(
1762 testHistoryLOD, self.__date, self.__testingDayStartTimeUtc, daysOfHistory)
1763
1764 # Assert and update the status
1765
1766 #print("\ntestStatus = "+str(testStatus))
1767 #print("\ntestHistoryLOD[0] = "+str(testHistoryLOD[0]))
1768
1769 if testStatus == "Missing":
1770 testDict = setTestDictAsMissing(testDict)
1771 elif testStatus == "Passed":
1772 testDict.update(testHistoryLOD[0])
1773 testDict['status_color'] = cdashColorPassed()
1774 else:
1775 # If we get here, there should be at least one test dict in
1776 # testHistoryLOD and this should be a Failed or Not Run test
1777 # testHistoryLOD[0] should be an exact duplicate of testDict. The below
1778 # check confirms that to make sure that CDash is giving us consistent
1779 # data.
1780 if self.__requireMatchTestTopTestHistory:
1781 if testDict.get('status', None) != testStatus:
1782 raise Exception(
1783 "Error, test testDict['status'] = '"+str(testDict.get('status',None))+"'"+\
1784 " != "+\
1785 "top test history testStatus = '"+testStatus+"'"+\
1786 " where:\n\n"+\
1787 " testDict = "+sorted_dict_str(testDict)+"\n\n"+\
1788 " top test history dict = "+sorted_dict_str(testHistoryLOD[0])+"\n\n" )
1789 if testDict.get('buildstarttime', None) != testHistoryLOD[0]['buildstarttime']:
1790 raise Exception(
1791 "Error, testDict['buildstarttime'] = '"+\
1792 str(testDict.get('buildstarttime',None))+"'"+\
1793 " != "+\
1794 "top test history 'buildstarttime' = "+\
1795 "'"+testHistoryLOD[0]['buildstarttime']+"'"+\
1796 " where:\n\n"+\
1797 " testDict = "+sorted_dict_str(testDict)+"\n\n"+\
1798 " top test history dict = "+sorted_dict_str(testHistoryLOD[0])+"\n\n" )
1799 if testDict.get('status', None) == None and testStatus == "Failed":
1800 # This is a test missing in the outer list of nonpassing tests but is
1801 # shown to be failing for the current testing day when looking at the
1802 # test history. This can happen when the outer CDash query filters
1803 # out random system failures (see documentation for option
1804 # --require-test-history-match-nonpassing-tests).
1805 testDict = setTestDictAsMissing(testDict)
1806 testDict.update(testHistoryLOD[0]) # Overwrites 'status' = "Failed"
1807 testDict['status'] = "Missing / Failed" # Show this special status!
1808 elif testStatus == "Failed":
1809 testDict['status_color'] = cdashColorFailed()
1810 elif testStatus == "Not Run":
1811 testDict['status_color'] = cdashColorNotRun()
1812
1813 # ToDo: Lookup the matching build info so that we can get the buildstamp
1814 # in order to build a good link to the build on CDash?
1815
1816 # Get the link to the test details if it exists
1817 testDetailsLink = testDict.get('testDetailsLink', None)
1818 if testDetailsLink:
1819 fullTestDetailsLink = cdashUrl+"/"+testDetailsLink
1820 else:
1821 fullTestDetailsLink = None
1822
1823 # Assign all of the new test dict fields that need to be added
1824 testDict["site_url"] = ""
1825 testDict['buildName_url'] = buildHistoryEmailUrl # ToDo: Change to one build
1826 if fullTestDetailsLink:
1827 testDict['testname_url'] = fullTestDetailsLink
1828 testDict['status_url'] = fullTestDetailsLink
1829 testDict['test_history_num_days'] = daysOfHistory
1830 testDict['test_history_query_url'] = testHistoryQueryUrl
1831 testDict['test_history_browser_url'] = testHistoryBrowserUrl
1832 testDict['test_history_list'] = testHistoryLOD
1833 testDict.update(testHistoryStats)
1834 testDict['pass_last_x_days_color'] = cdashColorPassed()
1835 testDict['pass_last_x_days_url'] = testHistoryBrowserUrl
1836 testDict['nopass_last_x_days_color'] = cdashColorFailed()
1837 testDict['nopass_last_x_days_url'] = testHistoryBrowserUrl
1838 testDict['missing_last_x_days_color'] = cdashColorMissing()
1839 testDict['missing_last_x_days_url'] = testHistoryBrowserUrl
1840 testDict['consec_pass_days_color'] = cdashColorPassed()
1841 testDict['consec_pass_days_url'] = testHistoryBrowserUrl
1842 testDict['consec_nopass_days_color'] = cdashColorFailed()
1843 testDict['consec_nopass_days_url'] = testHistoryBrowserUrl
1844 testDict['consec_missing_days_color'] = cdashColorMissing()
1845 testDict['consec_missing_days_url'] = testHistoryBrowserUrl
1846
1847 if testDict.get('status', None) == None:
1848 print("\ntestStatus = "+testStatus)
1849 print("\ntestDict:")
1850 pp = pprint.PrettyPrinter(indent=2)
1851 pp.pprint(testDict)
1852 raise Exception("Error, testDict['status']==None for testDict="+str(testDict))
1853
1854 # Return the updated test dict with the new fields
1855 return testDict
1856
1857
1858 def setTestDictAsMissing(testDict):
1859 testDict['status'] = "Missing"
1860 testDict['status_color'] = cdashColorMissing()
1861 testDict['details'] = "Missing"
1862 return testDict
1863
1864
1865 # Transform functor that sets the 'cdash_testing_day' field in a test dict.
1866 #
1867 class AddCDashTestingDayFunctor(object):
1868
1869 def __init__(self, cdash_testing_day):
1870 self.cdash_testing_day = cdash_testing_day
1871
1872 def __call__(self, testDict):
1873 testDict[u('cdash_testing_day')] = u(self.cdash_testing_day)
1874 return testDict
1875
1876
1877 # Gather up a list of the missing builds
1878 #
1879 # Inputs:
1880 #
1881 # buildLookupDict [in]: Lookup dict of build summary dicts gotten off CDash
1882 #
1883 # expectedBuildsList [in]: List of expected builds dict with fields 'group',
1884 # 'site', and 'buildname'.
1885 #
1886 # Returns an array of dicts of missing expected builds with list elements:
1887 #
1888 # {'group':"???", 'site':"???", 'buildname':"???", 'status':"???", ...}
1889 #
1890 # where the '...' will be the rest of the fields for builds that exist on CDash
1891 # but don't have full results.
1892 #
1893 # The field 'status' will either be given either:
1894 #
1895 # "Missing ALL"
1896 #
1897 # or will be:
1898 #
1899 # "Missing [update], [configure], [build], [tests]"
1900 #
1901 def getMissingExpectedBuildsList(buildsSearchableListOfDicts, expectedBuildsList):
1902 missingExpectedBuildsList = []
1903 for expectedBuildDict in expectedBuildsList:
1904 #print("\nexpectedBuildDict = "+str(expectedBuildDict))
1905 buildSummaryDict = \
1906 buildsSearchableListOfDicts.lookupDictGivenKeyValueDict(expectedBuildDict)
1907 #print("\nbuildSummaryDict = "+str(buildSummaryDict))
1908 if not buildSummaryDict:
1909 # No part of the expected build is found!
1910 missingExpectedBuildDict = copy.deepcopy(expectedBuildDict)
1911 missingExpectedBuildDict.update({'status':"Missing ALL"})
1912 #print("missingExpectedBuildDict = "+str(missingExpectedBuildDict))
1913 missingExpectedBuildsList.append(missingExpectedBuildDict)
1914 else:
1915 # This build has some build results so see if all of the results are
1916 # there or not
1917 missingPartsList = []
1918 #if not buildSummaryDict.get('update', None):
1919 # missingPartsList.append("update")
1920 if not buildSummaryDict.get('configure', None):
1921 missingPartsList.append("configure")
1922 if not buildSummaryDict.get('compilation', None):
1923 missingPartsList.append("build")
1924 if not buildSummaryDict.get('test', None):
1925 compilationDict = buildSummaryDict.get('compilation', None)
1926 if compilationDict and compilationDict['time'] == '0s':
1927 missingPartsList.append("build") # See NOTE below for explanation!
1928 missingPartsList.append("tests")
1929 # See if any parts are missing and report if there are as a missing
1930 # build
1931 if len(missingPartsList) > 0:
1932 missingBuildStr = "Missing "+", ".join(missingPartsList)
1933 missingExpectedBuildDict = copy.deepcopy(expectedBuildDict)
1934 missingExpectedBuildDict.update({'status':missingBuildStr})
1935 #print("missingExpectedBuildDict = "+str(missingExpectedBuildDict))
1936 missingExpectedBuildsList.append(missingExpectedBuildDict)
1937 else:
1938 # All parts of the expected build exists so don't report this as an
1939 # expected build.
1940 None
1941 # Return the list of missing expected builds and status
1942 return missingExpectedBuildsList
1943
1944 # NOTE: Above uses a heuristic for reporting missing build/compilation data
1945 # that if the 'compilation' 'time' is '0s' and there is no test data, then
1946 # we will assume that no build data was submitted to the dashboard.
1947 # Otherwise, we can't detect when build results are missing because if just
1948 # 'configure' data is updated to CDash, then CDash will automatically create
1949 # a 'compilation' subdict and will set the initial time to '0s'. So we
1950 # can't directly tell if build has missing build/compilation data or if it
1951 # just has configure data. (I consider this to be a defect in CDash and we
1952 # may ask Kitware to fix this but for now we can just use this heuristic.)
1953 # But we don't want to assume that a time of '0s' means that no
1954 # build/compilation data was submitted to CDash since a rebuild with no
1955 # targets getting built can take under 0s. However, it is unlikely that the
1956 # build/compilation time is '0s' and just happens to be missing test
1957 # results. In that case, it is likely that the job that was doing the build
1958 # died and never submitted build results in the first place. Therefore, we
1959 # will never report missing build results unless there are also missing test
1960 # results. And if there are missing test results, then the build will be
1961 # listed and missing anyway. And I don't see a lot of harm in failing to
1962 # report missing build results if there are test results and all of the
1963 # tests pass. The only gap is this logic, therefore, are expected builds
1964 # that have zero tests defined and submitted 0 tests to CDash (so the 'test'
1965 # subdict exists). So this is not perfect but I think this is the best we
1966 # can do until Kitware fixes CDash to not create a 'compilation' subdict
1967 # unless build data is actually submitted to CDash.
1968
1969 # NOTE: Above, we are skipping checking for missing 'update' results for now
1970 # because we want to allow some builds to not have update results. In the
1971 # future, it may be better to allow an expected build to say that it does
1972 # not have expected 'update' results.
1973
1974
1975 # Download set of builds from CDash builds and return flattened list of dicts
1976 #
1977 # The cdash/api/v1/index.php query selecting the set of builds is provided by
1978 # cdashIndexBuildsQueryUrl.
1979 #
1980 # If cdashIndexBuildsQueryCacheFile != None, then the raw JSON data-structure
1981 # downloaded from CDash will be written to the file
1982 # cdashIndexBuildsQueryCacheFile or read from that file if
1983 # useCachedCDashData==True.
1984 #
1985 # If alwaysUseCacheFileIfExists==True, then if the file
1986 # cdashIndexBuildsQueryCacheFile already exists, it will always be read to get
1987 # data instead of communicating with CDash even if useCachedCDashData==False.
1988 #
1989 # The list of builds pulled off of CDash is flattended and extracted using the
1990 # function flattenCDashIndexBuildsToListOfDicts().
1991 #
1992 # NOTE: The optional argument extractCDashApiQueryData_in is used in unit
1993 # testing to avoid calling CDash.
1994 #
1995 def downloadBuildsOffCDashAndFlatten(
1996 cdashIndexBuildsQueryUrl,
1997 fullCDashIndexBuildsJsonCacheFile=None,
1998 useCachedCDashData=False,
1999 alwaysUseCacheFileIfExists = False,
2000 verbose=True,
2001 extractCDashApiQueryData_in=extractCDashApiQueryData,
2002 ):
2003 # Get the query data
2004 fullCDashIndexBuildsJson = getAndCacheCDashQueryDataOrReadFromCache(
2005 cdashIndexBuildsQueryUrl, fullCDashIndexBuildsJsonCacheFile, useCachedCDashData,
2006 alwaysUseCacheFileIfExists, verbose=verbose,
2007 extractCDashApiQueryData_in=extractCDashApiQueryData_in )
2008 # Get trimmed down set of builds
2009 buildsListOfDicts = \
2010 flattenCDashIndexBuildsToListOfDicts(fullCDashIndexBuildsJson)
2011 return buildsListOfDicts
2012
2013
2014 # Download set of tests from cdash/api/v1/ctest/queryTests.php and return
2015 # flattened list of dicts
2016 #
2017 # cdashQueryTestsUrl [in]: String URL for cdash/api/v1/ctest/queryTests.php
2018 # with filters.
2019 #
2020 # If verbose==True, the the CDash query URL will be printed to STDOUT.
2021 # Otherwise, this function is silent and will not return any output to STDOUT.
2022 #
2023 # If fullCDashQueryTestsJsonCacheFile != None, then the raw JSON
2024 # data-structure will be written to that file.
2025 #
2026 # If useCachedCDashData==True, then data will not be pulled off of CDash and
2027 # instead the list of builds will be read from the file cdashQueryCacheFile
2028 # which must already exist from a prior call to this function (mostly for
2029 # debugging and unit testing purposes).
2030 #
2031 # If alwaysUseCacheFileIfExists==True, then if the file
2032 # cdashIndexBuildsQueryCacheFile already exists, it will always be read to get
2033 # data instead of communicating with CDash even if useCachedCDashData==False.
2034 #
2035 # The list of tests pulled off CDash is flattended and returned by the
2036 # function flattenCDashQueryTestsToListOfDicts().
2037 #
2038 # NOTE: The optional argument extractCDashApiQueryData_in is used in unit
2039 # testing to avoid calling CDash.
2040 #
2041 def downloadTestsOffCDashQueryTestsAndFlatten(
2042 cdashQueryTestsUrl,
2043 fullCDashQueryTestsJsonCacheFile=None,
2044 useCachedCDashData=False,
2045 alwaysUseCacheFileIfExists = False,
2046 verbose=True,
2047 extractCDashApiQueryData_in=extractCDashApiQueryData,
2048 ):
2049 # Get the query data
2050 fullCDashQueryTestsJson = getAndCacheCDashQueryDataOrReadFromCache(
2051 cdashQueryTestsUrl, fullCDashQueryTestsJsonCacheFile, useCachedCDashData,
2052 alwaysUseCacheFileIfExists, verbose=verbose,
2053 extractCDashApiQueryData_in=extractCDashApiQueryData_in )
2054 # Get flattened set of tests
2055 testsListOfDicts = \
2056 flattenCDashQueryTestsToListOfDicts(fullCDashQueryTestsJson)
2057 return testsListOfDicts
2058
2059
2060 # Returns True if a build has configure failures
2061 def buildHasConfigureFailures(buildDict):
2062 configureDict = buildDict.get('configure', None)
2063 if configureDict and configureDict['error'] > 0:
2064 return True
2065 return False
2066
2067
2068 # Returns True if a build has compilation/build failures
2069 def buildHasBuildFailures(buildDict):
2070 compilationDict = buildDict.get('compilation', None)
2071 if compilationDict and compilationDict['error'] > 0:
2072 return True
2073 return False
2074
2075
2076 # Functor class to sort a row of dicts by multiple columns of string data.
2077 class DictSortFunctor(object):
2078 def __init__(self, sortKeyList):
2079 self.sortKeyList = sortKeyList
2080 def __call__(self, dict_in):
2081 sortKeyStr=""
2082 for key in self.sortKeyList:
2083 keyData = dict_in.get(key)
2084 if sortKeyStr:
2085 sortKeyStr += "-"+str(keyData)
2086 else:
2087 sortKeyStr = keyData
2088 return sortKeyStr
2089
2090
2091 # Sort and limit a list of dicts
2092 #
2093 # Arguments:
2094 #
2095 # listOfDicts [in]: List of dicts that will be sorted according to keys.
2096 #
2097 # sortKeyList [in]: List of dict keys that define the sort order for the data
2098 # in the list. The default is None which means that no sort is performed.
2099 #
2100 # limitRowsToDisplay [in]: The max number of rows to display. The default is
2101 # None which will result in no limit to the number of rows displayed. The top
2102 # limitRowsToDisplay items will be displayed after the list is sorted.
2103 #
2104 def sortAndLimitListOfDicts(listOfDicts, sortKeyList = None,
2105 limitRowsToDisplay = None\
2106 ):
2107 # Sort the list
2108 if sortKeyList:
2109 listOfDictsOrdered = copy.copy(listOfDicts) # Shallow copy
2110 listOfDictsOrdered.sort(key=DictSortFunctor(sortKeyList))
2111 else:
2112 listOfDictsOrdered = listOfDicts # No sort being done
2113 # Limit rows
2114 if limitRowsToDisplay == None:
2115 listOfDictsLimited = listOfDictsOrdered
2116 else:
2117 listOfDictsLimited = listOfDictsOrdered[0:limitRowsToDisplay]
2118 # Return the final sorted limited list
2119 return listOfDictsLimited
2120
2121
2122 # Create a final summary line of global passfail
2123 #
2124 # cdashReportData [in]: Report data of type CDashReportData
2125 #
2126 # buildsetName [in]: The name of the set of builds to report on
2127 #
2128 # date [in]: Date string in format "YYYY-MM-DD"
2129 #
2130 def getOverallCDashReportSummaryLine(cdashReportData, buildsetName, date):
2131 if cdashReportData.globalPass:
2132 summaryLine = "PASSED"
2133 else:
2134 summaryLine = "FAILED"
2135
2136 if cdashReportData.summaryLineDataNumbersList:
2137 summaryLine += " (" + ", ".join(cdashReportData.summaryLineDataNumbersList) + ")"
2138
2139 summaryLine += ": "+buildsetName+" on "+date
2140
2141 return summaryLine
2142
2143
2144 ################################################################################
2145 # HTML Support Code
2146 ################################################################################
2147
2148
2149 def getFullCDashHtmlReportPageStr(cdashReportData, pageTitle="", pageStyle="",
2150 detailsBlockSummary=None,
2151 ):
2152
2153 htmlPage = \
2154 "<html>\n\n"
2155
2156 if pageStyle:
2157 htmlPage += \
2158 "<head>\n"+\
2159 pageStyle+\
2160 "</head>\n\n"
2161
2162 htmlPage += \
2163 "<body>\n\n"
2164
2165 if pageTitle:
2166 htmlPage += \
2167 "<h2>"+pageTitle+"</h2>\n\n"
2168
2169 htmlPage += \
2170 cdashReportData.htmlEmailBodyTop+\
2171 "\n"
2172
2173 if detailsBlockSummary:
2174 htmlPage += \
2175 "<details>\n\n"+\
2176 "<summary><b>"+detailsBlockSummary+":</b> (click to expand)</b></summary>\n\n"
2177 htmlPage += \
2178 cdashReportData.htmlEmailBodyBottom+\
2179 "\n"
2180
2181 if detailsBlockSummary:
2182 htmlPage += \
2183 "</details>\n\n"
2184
2185 htmlPage += \
2186 "</body>\n\n"+\
2187 "</html>\n"
2188
2189 return htmlPage
2190
2191
2192 def getDefaultHtmlPageStyleStr():
2193 return \
2194 "<style>\n"+\
2195 "h1 {\n"+\
2196 " font-size: 40px;\n"+\
2197 "}\n"+\
2198 "h2 {\n"+\
2199 " font-size: 30px;\n"+\
2200 "}\n"+\
2201 "h3 {\n"+\
2202 " font-size: 24px;\n"+\
2203 "}\n"+\
2204 "p {\n"+\
2205 " font-size: 18px;\n"+\
2206 "}\n"+\
2207 "</style>\n"
2208
2209
2210 # Class to store dict key and table header
2211 class TableColumnData(object):
2212
2213 # Class data
2214 validColAlignList=["left","right","center","justify","char"]
2215
2216 # Constructor
2217 def __init__(self, colHeader, dictKey, colAlign="left"):
2218 self.colHeader = colHeader
2219 self.dictKey = dictKey
2220 if not colAlign in self.validColAlignList:
2221 raise Exception(
2222 "Error, colAlign="+colAlign+" not valid. Please choose from"+\
2223 " the list ['" + "', '".join(validColAlignList) + "']!" )
2224 self.colAlign = colAlign
2225
2226
2227 # Color HTML text supported color
2228 def colorHtmlText(htmlText, color_in):
2229 if color_in == None or color_in == "":
2230 return htmlText
2231 elif color_in == "red":
2232 None # Okay!
2233 elif color_in == "green":
2234 None # Okay!
2235 elif color_in == "gray":
2236 None # Okay!
2237 elif color_in == "orange":
2238 None # Okay!
2239 else:
2240 raise Exception("Error, color='"+color_in+"' is invalid."+\
2241 " Only 'red', 'green', 'gray' and 'orange' are supported!")
2242 return("<font color=\""+color_in+"\">"+htmlText+"</font>")
2243
2244
2245 # Add soft word breaks for '_' chars and at other places to allow word wrap
2246 def addHtmlSoftWordBreaks(text_in):
2247 text_out = text_in.replace('_', '_&shy;')
2248 return text_out
2249
2250
2251 # Create an HTML table string from a list of dicts and column headers
2252 #
2253 # Arguments:
2254 #
2255 # tableTitle [in]: String for the name of the table included at the top of the
2256 # table.
2257 #
2258 # colDataList [in]: List of TableColumnData objects where
2259 # colDataList[j].dictKey gives the name of the key for that column of data,
2260 # colDataList[j].colHeader is the text name for the column header and
2261 # colDataList[j].colAlign gives the HTML alignment. That columns in the
2262 # table will listed in the order given in this list.
2263 #
2264 # rowDataList [in]: List of dicts that provide the data from the table. The
2265 # dict in each row must have the keys specified by colData[j].dictKey. In
2266 # addition, if (key_url=rowDataList[i].get(colData[j].dictKey+"_url",#
2267 # None))!=None, then the table entry will be an HTML link <a
2268 # href="dataRowList[i].get(key_url)">dataRowList[i].get(key)</a>.
2269 #
2270 # htmlStyle [in]: The HTML style data (between <style></style>. If None is
2271 # passed in then a default style is provided internally. NOTE: The default
2272 # table style uses CSS formatting for boarders but also sets the <table>
2273 # 'boarder' property since some email clients like Gmail ignore the CSS style
2274 # sections. To not set a style at all, pass in the empty string "" (not
2275 # None).
2276 #
2277 # htmlTableStyle [in]: The style for the HTML table used in <table
2278 # style=htmlTableStyle>. If set to None, then a default style is used. To
2279 # not set a style, pass in the empty string "" (not None).
2280 #
2281 # This will also put in soft work breaks for chars like '_' to allow for
2282 # compressing the produced tables.
2283 #
2284 def createHtmlTableStr(tableTitle, colDataList, rowDataList,
2285 htmlStyle=None, htmlTableStyle=None \
2286 ):
2287
2288 htmlStr = ""
2289
2290 # style options for the table
2291 defaultHtmlStyle=\
2292 "<style>table, th, td {\n"+\
2293 " padding: 5px;\n"+\
2294 " border: 1px solid black;\n"+\
2295 " border-collapse: collapse;\n"+\
2296 "}\n"+\
2297 "tr:nth-child(even) {background-color: #eee;}\n"+\
2298 "tr:nth-child(odd) {background-color: #fff;}\n"+\
2299 "</style>"
2300 if htmlStyle == "": htmlStyleUsed = ""
2301 elif htmlStyle != None: htmlStyleUsed = htmlStyle
2302 else: htmlStyleUsed = defaultHtmlStyle
2303 if htmlStyleUsed:
2304 htmlStr+=htmlStyleUsed+"\n"
2305
2306 # Table title and <table style=...>
2307 htmlStr+="<h3>"+tableTitle+"</h3>\n"
2308 if htmlTableStyle != None: htmlTableStyleUsed = htmlTableStyle
2309 else: htmlTableStyleUsed = "style=\"width:100%\" boarder=\"1\""
2310 htmlStr+="<table "+htmlTableStyleUsed+">\n\n"
2311
2312 # Column headings:
2313 htmlStr+="<tr>\n"
2314 for colData in colDataList:
2315 htmlStr+="<th>"+colData.colHeader+"</th>\n"
2316 htmlStr+="</tr>\n\n"
2317
2318 # Rows for the table
2319 row_i = 0
2320 for rowData in rowDataList:
2321 htmlStr+="<tr>\n"
2322 col_j = 0
2323 for colData in colDataList:
2324 dictKey = colData.dictKey
2325 # Get the raw entry for this column
2326 entry = rowData.get(dictKey, None)
2327 if entry == None:
2328 raise Exception(
2329 "Error, column "+str(col_j)+" dict key='"+colData.dictKey+"'"+\
2330 " row "+str(row_i)+" entry is 'None' which is not allowed!\n\n"+\
2331 "Row dict = "+str(rowData))
2332 # Add soft word breaks to allow line breaks for table compression
2333 entry = addHtmlSoftWordBreaks(str(entry).strip())
2334 # Add color if defined for this field
2335 entryColor = rowData.get(dictKey+"_color", None)
2336 if entryColor:
2337 entry = colorHtmlText(entry, entryColor)
2338 # See if the _url key also exists
2339 entry_url = rowData.get(dictKey+"_url", None)
2340 # Set the text for this row/column entry with or without the hyperlink
2341 if entry_url:
2342 entryStr = "<a href=\""+entry_url+"\">"+str(entry)+"</a>"
2343 else:
2344 entryStr = entry
2345 # Set the row entry in the HTML table
2346 htmlStr+=\
2347 "<td align=\""+colData.colAlign+"\">"+entryStr+"</td>\n"
2348 col_j += 1
2349 htmlStr+="</tr>\n\n"
2350 row_i += 1
2351
2352 # End of table
2353 htmlStr+="</table>\n\n" # Use two newlines makes for good formatting!
2354 return(htmlStr)
2355
2356
2357 # Get string for table title for CDash data to display
2358 #
2359 # Arguments:
2360 #
2361 # Arguments:
2362 #
2363 # dataTitle [in]: Name of the data category.
2364 #
2365 # dataCountAcronym [in]: Acronym for the type of data being displayed
2366 # (e.g. 'twoi' for "Tests With Out issue trackers"). This is printed in the
2367 # table title in the form dataCoutAcronym=len(rowDataList).
2368 #
2369 # numItems [in]: The number of items of data
2370 #
2371 def getCDashDataSummaryHtmlTableTitleStr(dataTitle, dataCountAcronym, numItems,
2372 limitRowsToDisplay=None,
2373 ):
2374 tableTitle = dataTitle
2375 if limitRowsToDisplay:
2376 tableTitle += " (limited to "+str(limitRowsToDisplay)+")"
2377 tableTitle += ": "+dataCountAcronym+"="+str(numItems)
2378 return tableTitle
2379
2380
2381 # Create an html table string for CDash summary data.
2382 #
2383 # Arguments:
2384 #
2385 # dataTitle [in]: Name of the data that we be included in the table title.
2386 #
2387 # dataCountAcronym [in]: Acronym for the type of data being displayed
2388 # (e.g. 'twoi' for "Tests With Out issue trackers"). This is printed in the
2389 # table title in the form dataCoutAcronym=len(rowDataList).
2390 #
2391 # colDataList [in]: List of TableColumnData objects where
2392 # colDataList[j].dictKey gives the name of the key for that column of data,
2393 # colDataList[j].colHeader is the text name for the column header, and
2394 # colDataList[j].colAlign gives the HTML alignment.
2395 # The columns in the table will listed in the order given in this list.
2396 #
2397 # rowDataList [in]: List of dicts that provide the data from the table. The
2398 # dict in each row must have the keys specified by colData[j].dictKey.
2399 #
2400 # sortKeyList [in]: List of dict keys that define the sort order for the data
2401 # in the list. The default is None which means that no sort is performed.
2402 #
2403 # limitRowsToDisplay [in]: The max number of rows to display. The default is
2404 # None which will result in no limit to the number of rows displayed. The top
2405 # limitRowsToDisplay items will be displayed after the list is sorted.
2406 #
2407 # htmlStyle [in]: The HTML style data (between <style></style>. If None is
2408 # passed in then a default style is provided internally (see
2409 # createHtmlTableStr().
2410 #
2411 # htmlTableStyle [in]: The style for the HTML table used in <table
2412 # style=htmlTableStyle>. The default is None in which case a default is
2413 # picked by createHtmlTableStr(().
2414 #
2415 # NOTE: If len(rowDataList) == 0, then the empty string "" is returned.
2416 #
2417 def createCDashDataSummaryHtmlTableStr( dataTitle, dataCountAcronym,
2418 colDataList, rowDataList, sortKeyList=None, limitRowsToDisplay=None,
2419 htmlStyle=None, htmlTableStyle=None, titleColor=None,
2420 ):
2421 # If no rows, don't create a table
2422 if len(rowDataList) == 0:
2423 return ""
2424 # Sort the list and limit the list
2425 rowDataListDisplayed = sortAndLimitListOfDicts(
2426 rowDataList, sortKeyList, limitRowsToDisplay)
2427 # Table title
2428 tableTitle = colorHtmlText(
2429 getCDashDataSummaryHtmlTableTitleStr(
2430 dataTitle, dataCountAcronym, len(rowDataList), limitRowsToDisplay ),
2431 titleColor )
2432 # Create and return the table
2433 return createHtmlTableStr( tableTitle,
2434 colDataList, rowDataListDisplayed, htmlStyle, htmlTableStyle )
2435
2436
2437 # Create a tests HTML table string
2438 #
2439 # testsetTypeInfo [in]: Information about the testset of type TestsetTypeInfo
2440 #
2441 # testTypeCountNum [in]: Number of total items for the test type, before
2442 # limiting (e.g. 25)
2443 #
2444 # testsLOD [in]: List of dicts of the test data typically first first
2445 # downloaded from CDash. Each dict in this list must also have been operated
2446 # on by the functors AddIssueTrackerInfoToTestDictFunctor and
2447 # AddTestHistoryToTestDictFunctor in order to have all of the data needed to
2448 # print in this table.
2449 #
2450 # daysOfHistory [in]: Number of days of test history being displayed. This is
2451 # needed for one of the table column headers. (ToDo: Remove this and get this
2452 # from the data).
2453 #
2454 # limitRowsToDisplay [in]: Limit of the number of rows to display. If this
2455 # limited then this argument is needed in order to print "(limited it ???)" in
2456 # the table title. Should be 'None' if this listing is not limited. (default
2457 # None)
2458 #
2459 # htmlStyle [in]: HTML style for the entire table (see createHtmlTableStr())
2460 # (default None)
2461 #
2462 # htmlTableStyle [in]: Style inside of <table ... > (see createHtmlTableStr())
2463 # (default None)
2464 #
2465 def createCDashTestHtmlTableStr(testsetTypeInfo, testTypeCountNum, testsLOD,
2466 limitRowsToDisplay=None, htmlStyle=None, htmlTableStyle=None,
2467 ):
2468 # Return empty string if no tests
2469 if len(testsLOD) == 0:
2470 return ""
2471 # Table title
2472 tableTitle = colorHtmlText(
2473 getCDashDataSummaryHtmlTableTitleStr(
2474 testsetTypeInfo.testsetDescr, testsetTypeInfo.testsetAcro,
2475 testTypeCountNum, limitRowsToDisplay ),
2476 testsetTypeInfo.testsetColor )
2477 # Consecutive nopass/pass/missing column
2478 consecCol = getCDashTestHtmlTableConsecColData(testsetTypeInfo.testsetTableType)
2479 # Get daysOfHistory out of the data
2480 daysOfHistory = testsLOD[0]['test_history_num_days']
2481 # Create column headers
2482 tcd = TableColumnData
2483 testsColDataList = [
2484 tcd("Site", "site"),
2485 tcd("Build Name", "buildName"),
2486 tcd("Test Name", "testname"),
2487 tcd("Status", "status"),
2488 tcd("Details", "details"),
2489 consecCol,
2490 tcd("Non-pass Last "+str(daysOfHistory)+" Days", 'nopass_last_x_days', "right"),
2491 tcd("Pass Last "+str(daysOfHistory)+" Days", 'pass_last_x_days', "right"),
2492 tcd("Issue Tracker", "issue_tracker", "right"),
2493 ]
2494 # Return the HTML table
2495 return createHtmlTableStr( tableTitle,
2496 testsColDataList, testsLOD,
2497 htmlStyle=htmlStyle, htmlTableStyle=htmlTableStyle )
2498
2499
2500 def getCDashTestHtmlTableConsecColData(testsetTableType):
2501 tcd = TableColumnData
2502 if testsetTableType == 'nopass':
2503 consecCol = tcd("Consec&shy;utive Non-pass Days", 'consec_nopass_days', 'right')
2504 elif testsetTableType == 'pass':
2505 consecCol = tcd("Consec&shy;utive Pass Days", 'consec_pass_days', 'right')
2506 elif testsetTableType == 'missing':
2507 consecCol = tcd("Consec&shy;utive Missing Days", 'consec_missing_days', 'right')
2508 else:
2509 raise Exception("Error, invalid testsetTableType="+str(testsetTableType))
2510 return consecCol
2511
2512
2513 # Class to generate the data for an HTML report for all test-sets for a given
2514 # issue tracker.
2515 #
2516 # This class can be reused for multiple issue issue trackers.
2517 #
2518 class IssueTrackerTestsStatusReporter(object):
2519
2520 # Constructor
2521 #
2522 # cdashReportData [persisting]: Data used to create the final report (of type
2523 # CDashReportData).
2524 #
2525 def __init__(self, verbose=True):
2526 self.cdashReportData = CDashReportData()
2527 self.testsetsReporter = TestsetsReporter(self.cdashReportData, htmlStyle="",
2528 verbose=verbose )
2529 self.issueTracker = None
2530 self.cdashTestingDay = None
2531
2532
2533 # Generate a report about the status of all of the tests for one issue
2534 # tracker
2535 #
2536 # Inputs:
2537 #
2538 # testsLOD [in]: The list of test dicts for one issue tracker. (The isuse
2539 # tracker info will be extracted from the test dicts field 'issue_tracker'
2540 # and all of the test dicts must have the same value for 'issue_tracker'
2541 # this will throw.)
2542 #
2543 # Return 'True' if all of the tests match an (internally defined) passing
2544 # criteria such that the issue tracker could be closed. Otherwise, returns
2545 # 'False' which means that the tests have not yet mt the passing criteria.
2546 # If len(testsLOD) == 0, then 'True' will be returned (which assumes that
2547 # there are no tests remaining related to the issue tracker).
2548 #
2549 # Postconditions:
2550 #
2551 # * A report about the status of the tests is returned in the function
2552 # self.getIssueTrackerTestsStatusReport().
2553 #
2554 def reportIssueTrackerTestsStatus(self, testsLOD):
2555 self.cdashReportData.reset()
2556 if (len(testsLOD) == 0):
2557 self.issueTracker = None
2558 return True
2559 (self.issueTracker, _) = getIssueTrackerFieldsAndAssertAllSame(testsLOD)
2560 self.cdashTestingDay = testsLOD[0]['cdash_testing_day']
2561 self.testsetsReporter.reportTestsets(testsLOD)
2562 # Return the final status
2563 return False # ToDo: Add logic to verify if issue can be closed!
2564
2565
2566 # Generate a pass/fail report HTML string for the last call to
2567 # reportIssueTrackerTestsStatus()
2568 def getIssueTrackerTestsStatusReport(self):
2569 if self.issueTracker == None:
2570 return None
2571 testsSummaryTitle = \
2572 "Test results for issue "+self.issueTracker+" as of "+self.cdashTestingDay
2573 return self.testsetsReporter.getTestsHtmlReportStr(testsSummaryTitle,
2574 detailsBlockSummary="Detailed test results")
2575
2576
2577 # Get the issue tracker fields from a list of test dicts and assert they are
2578 # all the same.
2579 #
2580 # Formal Parameters:
2581 #
2582 # testsLOD [in]: List of test dicts the must have the 'issue_tracker' and
2583 # the 'issuer_tracker_url' fields and they must all be identical to each
2584 # other.
2585 #
2586 # Returns:
2587 #
2588 # (issue_tracker, issue_tracker_url)
2589 #
2590 # Throws:
2591 #
2592 # IssueTrackerFieldError: If the 'issue_tracker' or the 'issuer_tracker_url'
2593 # fields are missing from a test dict or if they don't match each other.
2594 #
2595 def getIssueTrackerFieldsAndAssertAllSame(testsLOD):
2596 if len(testsLOD) == 0:
2597 return None
2598 issue_tracker = None
2599 issue_tracker_url = None
2600 i = 0
2601 for testDict in testsLOD:
2602 (issue_tracker_i, issue_tracker_url_i) = \
2603 getIssueTrackerFieldsFromTestDict(testDict, i)
2604 if issue_tracker == None and issue_tracker_url == None:
2605 issue_tracker = issue_tracker_i
2606 issue_tracker_url = issue_tracker_url_i
2607 else:
2608 assertSameIssueTracker(issue_tracker_i, issue_tracker, testDict, i)
2609 assertSameIssueTrackerUrl(issue_tracker_url_i, issue_tracker_url,
2610 testDict, i)
2611 i += 1
2612 return (issue_tracker, issue_tracker_url)
2613
2614
2615 def getIssueTrackerFieldsFromTestDict(testDict, idx):
2616 issue_tracker = testDict.get('issue_tracker', None)
2617 issue_tracker_url = testDict.get('issue_tracker_url', None)
2618 if issue_tracker == None:
2619 raise IssueTrackerFieldError(
2620 "Error, the test dict "+sorted_dict_str(testDict)+" at index "+str(idx)+\
2621 " is missing the 'issue_tracker' field!" )
2622 if issue_tracker_url == None:
2623 raise IssueTrackerFieldError(
2624 "Error, the test dict "+sorted_dict_str(testDict)+" at index "+str(idx)+\
2625 " is missing the 'issue_tracker_url' field!" )
2626 return (issue_tracker, issue_tracker_url)
2627
2628
2629 def assertSameIssueTracker(issue_tracker, issue_tracker_expected, testDict, idx):
2630 if issue_tracker != issue_tracker_expected:
2631 raise IssueTrackerFieldError(
2632 "Error, the test dict "+sorted_dict_str(testDict)+" at index "+str(idx)+\
2633 " has a different 'issue_tracker' field '"+str(issue_tracker)+"' than the"+\
2634 " expected value of '"+str(issue_tracker_expected)+"'!" )
2635
2636
2637 def assertSameIssueTrackerUrl(issue_tracker_url, issue_tracker_url_expected,
2638 testDict, idx,
2639 ):
2640 if issue_tracker_url != issue_tracker_url_expected:
2641 raise IssueTrackerFieldError(
2642 "Error, the test dict "+sorted_dict_str(testDict)+" at index "+str(idx)+\
2643 " has a different 'issue_tracker_url' field '"+str(issue_tracker_url)+"' than the"+\
2644 " expected value of '"+str(issue_tracker_url_expected)+"'!" )
2645
2646
2647 class IssueTrackerFieldError(Exception):
2648 pass
2649
2650
2651 # Class to report a single build-set.
2652 #
2653 # NOTE: The reason this is a class is that the cdashReportData and
2654 # addTestHistoryStrategy objects are set once and are used for multiple calls
2655 # to reportSingleBuildset().
2656 #
2657 class SingleBuildsetReporter(object):
2658
2659 # Constructor
2660 #
2661 def __init__(self, cdashReportData,
2662 htmlStyle=None, htmlTableStyle=None,
2663 verbose=True,
2664 ):
2665 self.cdashReportData = cdashReportData
2666 self.htmlStyle = htmlStyle
2667 self.htmlTableStyle = htmlTableStyle
2668 self.verbose = verbose
2669 self.groupSiteBuildNameSortOrder = ['group', 'site', 'buildname']
2670
2671 # Report on a given build-set and write info to self.cdashReportData
2672 #
2673 # Input arguments:
2674 #
2675 # buildsetDescr [in] Description for the set of builds
2676 #
2677 # buildsetAcro [in] Short acronym for the build set
2678 #
2679 # buildsetLOD [in] List of builds in this build set
2680 #
2681 # buildsetGlobalPass [in] If set to False and len(buildsetLOD) > 0, then
2682 # global pass is set to False (see below).
2683 #
2684 # buildsetColor [in] Color used for the text (see cdashColorXXX() values).
2685 #
2686 # buildsetColDataList [in] List of TableColumnData entries for each of the
2687 # columns to include in the table. Default is 'None' which gives a
2688 # default set of entries.
2689 #
2690 # verbose [in] If set to True then some more verbose info is printed to
2691 # STDOUT. If False, then nothing is printed to STDOUT (which is useful
2692 # for unit testing). The default is True.
2693 #
2694 # On output, self.cdashReportData data will be updated with the summary and
2695 # table of this given build-set. In particular, the following
2696 # cdashReportData fields will be written to:
2697 #
2698 # cdashReportData.summaryLineDataNumbersList: List will be appended with
2699 # lines for each build-set in order called.
2700 #
2701 # cdashReportData.htmlEmailBodyTop: The name of the table 'buildsetDescr',
2702 # the acronym 'buildsetAcro' and the size will be written on one line
2703 # ending with ``<br>\n``.
2704 #
2705 # cdashReportData.htmlEmailBodyBottom: Summary HTML table (with title)
2706 # will be written, along with formatting.
2707 #
2708 # cdashReportData.globalPass: Set to False if buildsetGlobalPass==True and
2709 # len(buildsetLOD) > 0.
2710 #
2711 def reportSingleBuildset(self, buildsetDescr, buildsetAcro, buildsetLOD,
2712 buildsetGlobalPass, buildsetColor, buildsetColDataList=None, verbose=True,
2713 ):
2714
2715 buildsetNum = len(buildsetLOD)
2716
2717 buildsetSummaryStr = \
2718 getCDashDataSummaryHtmlTableTitleStr(buildsetDescr, buildsetAcro,
2719 buildsetNum)
2720
2721 if self.verbose:
2722 print(buildsetSummaryStr)
2723
2724 if buildsetNum > 0:
2725
2726 if not buildsetGlobalPass:
2727 self.cdashReportData.globalPass = False
2728
2729 self.cdashReportData.summaryLineDataNumbersList.append(
2730 buildsetAcro+"="+str(buildsetNum))
2731
2732 self.cdashReportData.htmlEmailBodyTop += \
2733 colorHtmlText(buildsetSummaryStr,buildsetColor)+"<br>\n"
2734
2735 if not buildsetColDataList:
2736 tcd = TableColumnData
2737 buildsetColDataList = [
2738 tcd("Group", 'group'),
2739 tcd("Site", 'site'),
2740 tcd("Build Name", 'buildname'),
2741 ]
2742
2743 self.cdashReportData.htmlEmailBodyBottom += \
2744 createCDashDataSummaryHtmlTableStr(
2745 buildsetDescr, buildsetAcro, buildsetColDataList, buildsetLOD,
2746 sortKeyList=self.groupSiteBuildNameSortOrder,
2747 titleColor=buildsetColor)
2748
2749
2750 # Class to optionally get test history and then analyze and report a single
2751 # test-set.
2752 #
2753 # NOTE: The reason this is a class is that the cdashReportData and
2754 # addTestHistoryStrategy objects are set once and are used for multiple calls
2755 # to reportSingleTestset().
2756 #
2757 class SingleTestsetReporter(object):
2758
2759 # Constructor
2760 #
2761 # cdashReportData [persisting]: Data used to create the final report (of type
2762 # CDashReportData).
2763 #
2764 # testDictsSortKeyList [persisting]: The sort order array tests dicts (input
2765 # to DictSortFunctor()). (Default getDefaultTestDictsSortKeyList()).
2766 #
2767 # addTestHistoryStrategy [persisting]: Strategy object that can set the test
2768 # history on a list of dicts. Must have member function
2769 # getTestHistory(testsLOD). (Default 'None')
2770 #
2771 def __init__(self, cdashReportData,
2772 testDictsSortKeyList=getDefaultTestDictsSortKeyList(),
2773 addTestHistoryStrategy=None,
2774 htmlStyle=None, htmlTableStyle=None,
2775 verbose=True,
2776 ):
2777 self.cdashReportData = cdashReportData
2778 self.testDictsSortKeyList = testDictsSortKeyList
2779 self.addTestHistoryStrategy = addTestHistoryStrategy
2780 self.htmlStyle = htmlStyle
2781 self.htmlTableStyle = htmlTableStyle
2782 self.verbose = verbose
2783
2784 # Report on a given test-set and write info to self.cdashReportData
2785 #
2786 # On output, self.cdashReportData data will be updated with the summary and
2787 # table of this given testsetTypeInfo data. In particular, the following
2788 # cdashReportData fields will be written to:
2789 #
2790 # cdashReportData.summaryLineDataNumbersList: List will be appended with
2791 # the entry ``testsetTypeInfo.testsetAcro+"="+testsetTotalSize``.
2792 #
2793 # cdashReportData.htmlEmailBodyTop: The name of the table from
2794 # testsetTypeInfo.testsetDescr, the acronym testsetTypeInfo.testsetAcro and the
2795 # size will be written on one line ending with ``<br>\n``.
2796 #
2797 # cdashReportData.htmlEmailBodyBottom: Summary HTML table (with title)
2798 # will be written, along with formatting.
2799 #
2800 def reportSingleTestset(self, testsetTypeInfo, testsetTotalSize, testsetLOD,
2801 sortTests=True,
2802 limitTableRows=None, # Change to 'int' > 0 to limit table rows
2803 getTestHistory=False,
2804 ):
2805
2806 testsetSummaryStr = \
2807 getCDashDataSummaryHtmlTableTitleStr(testsetTypeInfo.testsetDescr,
2808 testsetTypeInfo.testsetAcro, testsetTotalSize)
2809
2810 if self.verbose:
2811 print("")
2812 print(testsetSummaryStr)
2813
2814 if testsetTotalSize > 0:
2815
2816 if testsetTypeInfo.existanceTriggersGlobalFail:
2817 self.cdashReportData.globalPass = False
2818
2819 self.cdashReportData.summaryLineDataNumbersList.append(
2820 testsetTypeInfo.testsetAcro+"="+str(testsetTotalSize))
2821
2822 self.cdashReportData.htmlEmailBodyTop += \
2823 colorHtmlText(testsetSummaryStr, testsetTypeInfo.testsetColor)+"<br>\n"
2824
2825 if sortTests or limitTableRows:
2826 testsetSortedLimitedLOD = sortAndLimitListOfDicts(
2827 testsetLOD, self.testDictsSortKeyList, limitTableRows )
2828 else:
2829 testsetSortedLimitedLOD = testsetLOD
2830
2831 if getTestHistory and self.addTestHistoryStrategy:
2832 self.addTestHistoryStrategy.getTestHistory(testsetSortedLimitedLOD)
2833
2834 self.cdashReportData.htmlEmailBodyBottom += createCDashTestHtmlTableStr(
2835 testsetTypeInfo, testsetTotalSize, testsetSortedLimitedLOD,
2836 limitRowsToDisplay=limitTableRows,
2837 htmlStyle=self.htmlStyle, htmlTableStyle=self.htmlTableStyle )
2838
2839
2840 # Class to generate the data for an HTML report for all test-sets represented
2841 # in a list of test dicts.
2842 #
2843 class TestsetsReporter(object):
2844
2845 # Constructor
2846 #
2847 # cdashReportData [persisting]: Data used to create the final report (of type
2848 # CDashReportData).
2849 #
2850 def __init__(self, cdashReportData, testsetAcroList=getStandardTestsetAcroList(),
2851 htmlStyle=None, htmlTableStyle=None, verbose=True,
2852 ):
2853 self.cdashReportData = cdashReportData
2854 self.testsetAcroList = testsetAcroList
2855 self.singleTestsetReporter = SingleTestsetReporter(cdashReportData,
2856 htmlStyle=htmlStyle, htmlTableStyle=htmlTableStyle, verbose=verbose)
2857
2858
2859 # Separate out and report on all of the test-sets in the input list of test
2860 # dicts.
2861 #
2862 def reportTestsets(self, testsLOD):
2863 testDictsByTestsetAcro = binTestDictsByTestsetAcro(testsLOD)
2864 for testsetAcro in self.testsetAcroList:
2865 testsetLOD = testDictsByTestsetAcro.get(testsetAcro, None)
2866 if testsetLOD:
2867 testsetTypeInfo = getStandardTestsetTypeInfo(testsetAcro)
2868 self.singleTestsetReporter.reportSingleTestset(testsetTypeInfo,
2869 len(testsetLOD), testsetLOD)
2870 # ToDo: Modify the above to assert that there are no unexpected test-set
2871 # types!
2872
2873
2874 # Generate a pass/fail report for all of the testset analyzed by
2875 # reportTestsets().
2876 def getTestsHtmlReportStr(self, testsSummaryTitle, detailsBlockSummary=None):
2877 cdashReportData = self.cdashReportData
2878 cdashReportData.htmlEmailBodyTop = \
2879 "<p>\n" + cdashReportData.htmlEmailBodyTop + "</p>\n"
2880 return getFullCDashHtmlReportPageStr(
2881 cdashReportData, pageTitle=testsSummaryTitle, pageStyle="",
2882 detailsBlockSummary=detailsBlockSummary)
2883 # NOTE: Above, cdashReportData is a shallow copy of some fields like lists
2884 # and other objects but the string fields are deep copied. Therefore,
2885 # this function does not change the state of 'self' at all.!
2886
2887
2888 # Bin a list of test dicts by issue tracker
2889 #
2890 # Input arguments:
2891 #
2892 # testsLOD [in]: Tests list of dicts
2893 #
2894 # Returns (testDictsByIssueTracker, testsWithoutIssueTrackersLOD)
2895 #
2896 # testDictsByIssueTracker [out]: A dict where the keys are the issue tracker
2897 # strings (e.g. '#1234') and the values are the sublist test dicts that have
2898 # that issue tracker.
2899 #
2900 # testsWithoutIssueTrackersLOD [out]: The remaining list of test dicts that
2901 # don't have an issue tracker.
2902 #
2903 # For example, the input list of dicts:
2904 #
2905 # testsLOD = [
2906 # { 'testname':'test1' ... 'issue_tracker':'#1234' },
2907 # { 'testname':'test2' ... },
2908 # { 'testname':'test3' ... 'issue_tracker':'#1235' },
2909 # { 'testname':'test4' ... 'issue_tracker':'#1234' },
2910 # { 'testname':'test5' ... },
2911 # { 'testname':'test6' ... 'issue_tracker':'#1235' },
2912 # { 'testname':'test7' ... 'issue_tracker':'#1236' },
2913 #
2914 # would yield:
2915 #
2916 # testDictsByIssueTracker = {
2917 # '#1234' : [
2918 # { 'testname':'test1' ... 'issue_tracker':'#1234' },
2919 # { 'testname':'test4' ... 'issue_tracker':'#1234' },
2920 # ],
2921 # '#1235' : [
2922 # { 'testname':'test3' ... 'issue_tracker':'#1235' },
2923 # { 'testname':'test6' ... 'issue_tracker':'#1235' },
2924 # ],
2925 # '#1236' : [
2926 # { 'testname':'test7' ... 'issue_tracker':'#1236' },
2927 # ],
2928 # ]
2929 #
2930 # testsWithoutIssueTrackersLOD = [
2931 # { 'testname':'test2' ... },
2932 # { 'testname':'test5' ... },
2933 # ]
2934 #
2935 def binTestDictsByIssueTracker(testsLOD):
2936 testDictsByIssueTracker = {}
2937 testsWithoutIssueTrackersLOD = []
2938 for testDict in testsLOD:
2939 issueTracker = testDict.get('issue_tracker', None)
2940 if issueTracker:
2941 issueTrackerBinTestsLOD = testDictsByIssueTracker.setdefault(issueTracker, [])
2942 issueTrackerBinTestsLOD.append(testDict)
2943 else:
2944 testsWithoutIssueTrackersLOD.append(testDict)
2945 return (testDictsByIssueTracker, testsWithoutIssueTrackersLOD)
2946
2947
2948 # Bin a list of test dicts based on their test-set acronym
2949 #
2950 # Input arguments:
2951 #
2952 # testsLOD [in]: Tests list of dicts
2953 #
2954 # Returns testDictsByTestsetAcro
2955 #
2956 # testDictsByTestsetAcro [out]: A dict where the keys are the standard
2957 # test-set acronyms ('twoif', 'twoinr', 'twip', etc) and the value for each
2958 # is the list of test dicts that below to that test-set category.
2959 #
2960 # For example, the input list of dicts:
2961 #
2962 # testsLOD = [
2963 # { 'testname':'test1', 'status':'Failed' ... 'issue_tracker':'#1234' },
2964 # { 'testname':'test2', 'status':'Failed' ... },
2965 # { 'testname':'test3', 'status':'Passed' ... 'issue_tracker':'#1235' },
2966 # { 'testname':'test4', 'status':'Failed' ... 'issue_tracker':'#1234' },
2967 # { 'testname':'test5', 'status':'Not Run' ... },
2968 # { 'testname':'test6', 'status':'Missing' ... 'issue_tracker':'#1235' },
2969 # { 'testname':'test7', 'status':'Not Run' ... 'issue_tracker':'#1236' },
2970 #
2971 # would yield:
2972 #
2973 # testDictsByTestsetAcro = {
2974 # 'twoif' : [
2975 # { 'testname':'test2', 'status':'Failed' ... },
2976 # ],
2977 # 'twoinr' : [
2978 # { 'testname':'test5', 'status':'Not Run' ... },
2979 # ],
2980 # 'twip' : [
2981 # { 'testname':'test3', 'status':'Passed' ... 'issue_tracker':'#1235' },
2982 # ],
2983 # 'twim' : [
2984 # { 'testname':'test6', 'status':'Missing' ... 'issue_tracker':'#1235' },
2985 # ],
2986 # 'twif' : [
2987 # { 'testname':'test1', 'status':'Failed' ... 'issue_tracker':'#1234' },
2988 # { 'testname':'test4', 'status':'Failed' ... 'issue_tracker':'#1234' },
2989 # ],
2990 # 'twinr' : [
2991 # { 'testname':'test7', 'status':'Not Run' ... 'issue_tracker':'#1236' },
2992 # ],
2993 #
2994 def binTestDictsByTestsetAcro(testsLOD):
2995 testDictsByTestsetAcro = {}
2996 for testDict in testsLOD:
2997 testsetAcron = getTestsetAcroFromTestDict(testDict)
2998 testsetAcronBinTestsLOD = testDictsByTestsetAcro.setdefault(testsetAcron, [])
2999 testsetAcronBinTestsLOD.append(testDict)
3000 return testDictsByTestsetAcro
3001
3002
3003 #########################################
3004 # HTML Email stuff
3005 #########################################
3006
3007
3008 #
3009 # Create an HTML MIME Email
3010 #
3011
3012 import smtplib
3013
3014 from email.mime.multipart import MIMEMultipart
3015 from email.mime.text import MIMEText
3016 from email.utils import formatdate
3017 from email.charset import Charset, QP
3018 import email.message
3019
3020
3021 # Create MINE formatted email object (but don't send it)
3022 #
3023 def createHtmlMimeEmail(fromAddress, toAddress, subject, textBody, htmlBody,
3024 doRemoveSoftHyphens=False \
3025 ):
3026
3027 # Create message container - the correct MIME type is multipart/alternative.
3028 msg = MIMEMultipart('alternative')
3029 msg['From'] = fromAddress
3030 msg['To'] = toAddress
3031 msg['Date'] = formatdate(localtime=True)
3032 msg['Subject'] = subject
3033 msg['Content-Type'] = "text/html; charset=utf-8"
3034
3035 # RFC 821 states that the text line size including <CRLF> is 1000 characters.
3036 # This text line size is causing spaces to be inserted into the email body and
3037 # break long hyperlinks. Below, we use quoted-printable
3038 # "Content-Transfer-Encoding" to ensure integrity of the data being sent
3039 # through the gateway when html text lines are longer than 1000 characters.
3040 msg['Content-Transfer-Encoding'] = "quoted-printable"
3041
3042 # Remove hidden soft hyphens from htmlBody so triagers can easily copy paste
3043 # long build and test names into CDash queries.
3044 if doRemoveSoftHyphens:
3045 htmlBody = htmlBody.replace('&shy;', '')
3046
3047 # Record the MIME types of both parts - text/plain and text/html.
3048 part1 = MIMEText(textBody.encode('utf-8'), 'plain', 'utf-8')
3049 part2 = MIMEText(htmlBody.encode('utf-8'), 'html', 'utf-8')
3050
3051 # Attach parts into message container. According to RFC 2046, the last part
3052 # of a multipart message, in this case the HTML message, is best and
3053 # preferred.
3054 msg.attach(part1)
3055 msg.attach(part2)
3056
3057 return msg
3058
3059
3060 # Send a MIME formatted email
3061 #
3062 def sendMineEmail(mimeEmail):
3063 # Send the message via local SMTP server.
3064 s = smtplib.SMTP('localhost')
3065 # sendmail function takes 3 arguments: sender's address, recipient's address
3066 # and message to send - here it is sent as one string.
3067 s.sendmail(mimeEmail['From'], mimeEmail['To'], mimeEmail.as_string())
3068 s.quit()