Debian Codegraph @

cmake/tribits/ci_support/CreateIssueTrackerFromCDashQuery.py

raw
1 import os
2 import sys
3 import pprint
4
5 import CDashQueryAnalyzeReport as CDQAR
6 import cdash_build_testing_date as CDBTD
7 from Python2and3 import dictItems
8
9 g_pp = pprint.PrettyPrinter(indent=2)
10
11
12 # Class that is used to create issue trackers for nonpassing tests downloaded
13 # from a CDash queryTests.php query.
14 #
15 # To make this general, the user has to create a class object that is passed
16 # in as the argument 'issueTrackerFormatter' in this class that supports
17 # the following functions:
18 #
19 # issueTrackerFormatter.createFormattedIssueTracker((issueTrackerData):
20 #
21 # This function is expected to return the created text for the issue tracker
22 # given the data provided by the argument 'issueTrackerData' of type
23 # 'IssueTrackerData'. The format of this text can be anything such as
24 # Markdown, RestructredText, JIRA Issue Markup, etc. The only assumption
25 # about formatting is in the HTML table passed in the argument
26 # 'issueTrackerData.testHistoryHtmlTableText'.
27 #
28 # The object 'issueTrackerFormatter' can format the issue tracker text anyway
29 # it wants. The object 'issueTrackerFormatter' is stateless w.r.t. this class
30 # CreateIssueTrackerFromCDashQueryDriver.
31 #
32 class CreateIssueTrackerFromCDashQueryDriver:
33
34
35 def __init__(self, issueTrackerFormatter, cdashProjectStartTimeUtc=None,
36 usageHelp="", issueTrackerUrlTemplate="", issueTrackerTemplate="",
37 ):
38 self.issueTrackerFormatter = issueTrackerFormatter
39 self.cdashProjectStartTimeUtc = cdashProjectStartTimeUtc
40 self.usageHelp = usageHelp
41 self.issueTrackerUrlTemplate = issueTrackerUrlTemplate
42 self.issueTrackerTemplate = issueTrackerTemplate
43 self.options = None
44
45
46 def runDriver(self):
47
48 print("\n***")
49 print("*** Getting data to create a new issue tracker")
50 print("***\n")
51
52 self.getCmndLineOptions()
53
54 print("Downloading full list of nonpassing tests from CDash URL:\n")
55 print(" "+self.options.nonpassingTestsUrl+"\n")
56
57 nonpassingTestsLOD = self.downloadNonpassingTestsData()
58 print("\nTotal number of nonpassing tests over all days = "\
59 +str(len(nonpassingTestsLOD)))
60
61 uniqNonpassingTestsLOD = getUniqueTestsListOfDicts(nonpassingTestsLOD)
62 print("\nTotal number of unique nonpassing test/build pairs over all days = "\
63 +str(len(uniqNonpassingTestsLOD)))
64
65 (testnameList, buildnameList) = getTestnameAndBuildnameLists(uniqNonpassingTestsLOD)
66 print("\nNumber of test names = "+str(len(testnameList)))
67 print("\nNumber of build names = "+str(len(buildnameList)))
68
69 testingDayStartNonpassingDate = getTestingDayStartNonpassingDate(
70 nonpassingTestsLOD, self.options.cdashProjectStartTimeUtc)
71 #print("testingDayStartNonpassingDate = "+testingDayStartNonpassingDate)
72
73 testHistoryHtmlTableText = "" # ToDo: Implement!
74
75 issueTrackerText = self.issueTrackerFormatter.createFormattedIssueTracker(
76 IssueTrackerData(
77 summaryLine=self.options.summaryLine,
78 testingDayStartNonpassingDate=testingDayStartNonpassingDate,
79 nonpassingTestsUrl=self.options.nonpassingTestsUrl,
80 uniqNonpassingTestsLOD=uniqNonpassingTestsLOD,
81 buildnameList=buildnameList,
82 testnameList=testnameList,
83 testHistoryHtmlTableText=testHistoryHtmlTableText,
84 )
85 )
86
87 self.writeNewIssueTrackerFile(issueTrackerText)
88
89 self.writeNewTestsWithIssueTrackersFile(uniqNonpassingTestsLOD)
90
91
92 def injectExtraCmndLineOptions(self, clp):
93 clp.add_argument(
94 "--nonpassing-tests-url", "-u", dest="nonpassingTestsUrl", default="",
95 help="Full CDash queryTest.php URL for the list of nonpassing tests over a"\
96 +" time range given as 'begin' and 'end' fields [Required]." )
97 clp.add_argument(
98 "--cdash-project-start-time", dest="cdashProjectStartTimeUtc", default="",
99 help="Starting time for the CDash testing day in 'hh:mm' in UTC."\
100 + " Check the CDash project settings for the testing day start time." )
101 clp.add_argument(
102 "--summary-line", "-s", dest="summaryLine", default="<summary-line>",
103 help="The summary line text for the issue tracker [Default '<summary-line>']." )
104 clp.add_argument(
105 "--new-issue-tracker-file", "-i", dest="newIssueTrackerFile", default="",
106 help="File created with the new issue tracker text if"\
107 +" specified [Default empty '']." )
108 clp.add_argument(
109 "--new-tests-with-issue-trackers-file", "-t", dest="newTestsWithIssueTrackersFile",
110 default="",
111 help="CSV file created with entries for the list of nonpassing tests "\
112 +" if specified [Default empty '']." )
113
114
115 def getCmndLineOptions(self):
116 from argparse import ArgumentParser, RawDescriptionHelpFormatter
117 clp = ArgumentParser(
118 description=self.usageHelp,
119 formatter_class=RawDescriptionHelpFormatter)
120 self.injectExtraCmndLineOptions(clp)
121 self.options = clp.parse_args(sys.argv[1:])
122 self.postReadFixupCommandlineOptions()
123 self.validateCommandLineOptions()
124
125
126 def postReadFixupCommandlineOptions(self):
127 if (self.options.cdashProjectStartTimeUtc == "") \
128 and self.cdashProjectStartTimeUtc \
129 :
130 self.options.cdashProjectStartTimeUtc = self.cdashProjectStartTimeUtc
131
132
133 def validateCommandLineOptions(self):
134 if self.options.nonpassingTestsUrl == "":
135 print("Error, the argument --nopassing-tests-url is required (see --help)")
136 sys.exit(1)
137 if self.options.cdashProjectStartTimeUtc == "":
138 print("Error, the argument --cdash-project-start-time is required (see --help)")
139 sys.exit(1)
140
141
142 def downloadNonpassingTestsData(self):
143 apiQueryTestsUrl = self.options.nonpassingTestsUrl.replace(
144 "/queryTests.php", "/api/v1/queryTests.php")
145 cdashDownloadFileForTesting = \
146 os.environ.get('CREATE_ISSUE_TRACKER_FROM_CDASH_QUERY_FILE_FOR_UNIT_TESTING',
147 '' )
148 nonpassingTestsLOD = \
149 CDQAR.downloadTestsOffCDashQueryTestsAndFlatten(apiQueryTestsUrl,
150 fullCDashQueryTestsJsonCacheFile=cdashDownloadFileForTesting,
151 alwaysUseCacheFileIfExists=True )
152 return nonpassingTestsLOD
153
154
155 def writeNewIssueTrackerFile(self, issueTrackerText):
156 if self.options.newIssueTrackerFile:
157 print("\nWriting out new issue tracker text to '"\
158 +self.options.newIssueTrackerFile+"'")
159 with open(self.options.newIssueTrackerFile, 'w') as fileHandle:
160 fileHandle.write(issueTrackerText)
161
162
163 def writeNewTestsWithIssueTrackersFile(self, uniqNonpassingTestsLOD):
164 if self.options.newTestsWithIssueTrackersFile:
165 print("\nWriting out list of test/build pairs for CSV file '"\
166 +self.options.newTestsWithIssueTrackersFile+"'")
167 csvFileStruct = CDQAR.writeTestsListOfDictsToCsvFileStructure(uniqNonpassingTestsLOD,
168 self.issueTrackerUrlTemplate, self.issueTrackerTemplate )
169 with open(self.options.newTestsWithIssueTrackersFile, 'w') as csvFile:
170 csvFile.write(CDQAR.writeCsvFileStructureToStr(csvFileStruct))
171
172
173 # Class object for issue tracker data
174 #
175 class IssueTrackerData:
176
177 def __init__(self,
178 summaryLine=None,
179 testingDayStartNonpassingDate=None,
180 nonpassingTestsUrl=None,
181 uniqNonpassingTestsLOD=None,
182 buildnameList=None,
183 testnameList=None,
184 testHistoryHtmlTableText=None,
185 ):
186 self.summaryLine = summaryLine
187 self.testingDayStartNonpassingDate = testingDayStartNonpassingDate
188 self.nonpassingTestsUrl = nonpassingTestsUrl
189 self.uniqNonpassingTestsLOD = uniqNonpassingTestsLOD
190 self.buildnameList = buildnameList
191 self.testnameList = testnameList
192 self.testHistoryHtmlTableText = testHistoryHtmlTableText
193
194
195 #
196 # Nonmember functions
197 #
198
199
200 def getUniqueTestsListOfDicts(cdashTestsLOD): #LOD == "List of Dicts"
201 # Get a dict that has just unique keys 'site', 'buildName' and 'testname' values
202 uniqTestDict = {}
203 for testDict in cdashTestsLOD:
204 site = testDict.get('site')
205 buildName = testDict.get('buildName')
206 testname = testDict.get('testname')
207 uniqTestDict.update( {
208 site+"_"+buildName+"_"+testname :
209 {'site':site, 'buildName':buildName, 'testname':testname}
210 } )
211 # Get a flat list of test dicts
212 uniqNonpassingTestsLOD= []
213 for key, value in dictItems(uniqTestDict):
214 uniqNonpassingTestsLOD.append(value)
215 uniqNonpassingTestsLOD = CDQAR.sortAndLimitListOfDicts(uniqNonpassingTestsLOD,
216 CDQAR.getDefaultTestDictsSortKeyList())
217 return uniqNonpassingTestsLOD
218
219
220 def getTestnameAndBuildnameLists(uniqNonpassingTestsLOD):
221 testnameSet = set()
222 buildnameSet = set()
223 for testDict in uniqNonpassingTestsLOD:
224 testnameSet.add(testDict.get('testname'))
225 buildnameSet.add(testDict.get('buildName'))
226 testnameList = extractListAndSort(testnameSet)
227 buildnameList = extractListAndSort(buildnameSet)
228 return (testnameList, buildnameList)
229
230
231 def extractListAndSort(setIn):
232 listOut = []
233 for item in setIn: listOut.append(item)
234 listOut.sort()
235 return listOut
236
237
238 def getTestingDayStartNonpassingDate(nonpassingTestsLOD, cdashProjectStartTimeUtcStr):
239 cdashProjectStartTimeUtcStrTD = CDBTD.getProjectTestingDayStartTimeDeltaFromStr(
240 cdashProjectStartTimeUtcStr)
241 testingDayStartNonpassingDate = None
242 for testDict in nonpassingTestsLOD:
243 #print("")
244 #g_pp.pprint(testDict)
245 buildStartTime = testDict.get('buildstarttime')
246 #print("buildStartTime = "+str(buildStartTime))
247 testingDate = CDBTD.getTestingDayDateFromBuildStartTimeStr(buildStartTime,
248 cdashProjectStartTimeUtcStrTD)
249 if (testingDayStartNonpassingDate==None) \
250 or (testingDate < testingDayStartNonpassingDate) \
251 :
252 testingDayStartNonpassingDate = testingDate
253 #print("testingDayStartNonpassingDate = "+str(testingDayStartNonpassingDate))
254 return testingDayStartNonpassingDate
255
256
257 #
258 # Helper functions to be used in concrete drivers
259 #
260
261
262 def getMarkdownListStr(inputList, formatChars):
263 markdownText = ""
264 for item in inputList:
265 markdownText += "* "+formatChars+item+formatChars+"\n"
266 return markdownText