Debian Codegraph @

cmake/tribits/ci_support/cdash_build_testing_date.py

raw
1 #!/usr/bin/env python3
2
3 # @HEADER
4 # *****************************************************************************
5 # TriBITS: Tribal Build, Integrate, and Test System
6 #
7 # Copyright 2013-2016 NTESS and the TriBITS contributors.
8 # SPDX-License-Identifier: BSD-3-Clause
9 # *****************************************************************************
10 # @HEADER
11
12 import datetime
13
14 #
15 # Help message
16 #
17
18
19 usageHelp = r"""cdash_testing_date.py --cdash-project-start-time="hh:mm" [other options]
20
21 Returns to STDOUT the date string YYYY-MM-DD that corresponds the the matching
22 CDash test day. Is should be compatible with the 'date=YYYY-MM-DD' argument
23 to many CDash PHP pages for a build that has a starting time stamp that
24 matches the requested date.
25
26 This tool is meant to match the CDash logic for what testing day a given CDash
27 build matches for the CDash 'date' field in various PHP pages for a given
28 CDash build with a give build start time (as reported to CDash from CTest).
29
30 For example, if the CDash project start time is 04:00 UTC and the CDash
31 build start time is 2018-01-22T00:00 UTC, then the relative CDash build date
32 time would be 2018-01-21T20:00 UTC which is the previous testing day
33 2018-01-21. But if the CDash build start time was 2018-01-22T04:10 UTC,
34 then the relative CDash build date time would be 2018-01-22:00:10 UTC and
35 the testing day would be 2018-01-22.
36 """
37
38 #
39 # Helper functions
40 #
41
42
43 def injectCmndLineOptionsInParser(clp, gitoliteRootDefault=""):
44
45 clp.add_option(
46 "--cdash-project-start-time", dest="cdashProjectStartTimeUtcStr", type="string", default="",
47 help="Starting time for the CDash testing day in 'hh:mm' in UTC."\
48 + " Check the CDash project settings for the testing day start time." )
49
50 clp.add_option(
51 "--day-incr", dest="dayIncrInt", type="int", default="0",
52 help="Increment for the testing date (default '0') [optional]" )
53
54 clp.add_option(
55 "--cdash-build-start-time", dest="cdashBuildStartTime", type="string", default="",
56 help="CDash build start time in format 'YYYY-MM-DDThh:mm UTC'. If empty ''"\
57 +" then the current date/time in UTC is used. (default '') [optional]" )
58
59 clp.add_option(
60 "--debug-level", dest="debugLevel", type="int", default="0",
61 help="Debug level. An integer >= 0 (default '0')" )
62
63
64 def getCmndLineOptions():
65 from optparse import OptionParser
66 clp = OptionParser(usage=usageHelp)
67 injectCmndLineOptionsInParser(clp)
68 (options, args) = clp.parse_args()
69 if options.cdashProjectStartTimeUtcStr == "":
70 raise Exception("Error, input argument --cdash-project-start-time must be non"\
71 +"-empty and must be of the format hh:mm UTC")
72 return options
73
74
75 # Return the current time in UTC as a datetime object
76 def getCurrentDateTimeUtc():
77 return datetime.datetime.utcnow()
78
79
80 # Get the timezone offset as a timedelta object w.r.t to UTC
81 #
82 # The supported timezones for timeZoneStr are the strings:
83 #
84 # * UTC: 0
85 # * EDT: 4
86 # * EST: 5
87 # * CDT: 5
88 # * CST: 6
89 # * MDT: 6
90 # * MST: 7
91 #
92 # NOTE: Any timezone that CDash returns for the 'buildstarttime' field must be
93 # added below.
94 #
95 def getTimeZoneOffset(timeZoneStr):
96 if timeZoneStr == "UTC": timezoneOffsetInt = 0
97 elif timeZoneStr == "EDT": timezoneOffsetInt = 4
98 elif timeZoneStr == "EST": timezoneOffsetInt = 5
99 elif timeZoneStr == "CDT": timezoneOffsetInt = 5
100 elif timeZoneStr == "CST": timezoneOffsetInt = 6
101 elif timeZoneStr == "MDT": timezoneOffsetInt = 6
102 elif timeZoneStr == "MST": timezoneOffsetInt = 7
103 else: raise Exception("Error, unrecognized timezone '"+timeZoneStr+"'!")
104 return datetime.timedelta(hours=timezoneOffsetInt)
105
106
107 # Return a timezone aware datetime object given an input date and time given
108 # in the format "<YYYY>-<MM>-<DD>T<hh>:<mm>:<ss> <TZ>".
109 #
110 # Note. the timezone <TZ> can be any of those supported by the function
111 # getTimeZoneOffset()
112 def getBuildStartTimeUtcFromStr(buildStartTimeStr):
113 buildStartTimeStrArray = buildStartTimeStr.split(" ")
114 if len(buildStartTimeStrArray) == 2:
115 timezoneOffset = getTimeZoneOffset(buildStartTimeStrArray[1])
116 else:
117 timezoneOffset = 0
118 localDateTime = datetime.datetime.strptime(buildStartTimeStrArray[0], "%Y-%m-%dT%H:%M:%S")
119 return localDateTime + timezoneOffset
120
121
122 # Return a timedelta object for the CDash Project start time passed in as a
123 # string in the format "<hh>:<mm>" in UTC.
124 def getProjectTestingDayStartTimeDeltaFromStr(cdashProjectStartTimeUtcStr):
125 t = datetime.datetime.strptime(cdashProjectStartTimeUtcStr, '%H:%M')
126 return datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)
127
128
129 # Return a timedelta object for a day increment pass in as an signed integer.
130 def getDayIncrTimeDeltaFromInt(dayIncrInt):
131 return datetime.timedelta(days=dayIncrInt)
132
133
134 # Return the string "YYYY-MM-DDThh:mm:ss UTC" corresponding to the input
135 # datetime object.
136 def getBuildStartTimeUtcStrFromUtcDT(datetimeUtcDT, removeSpace=False):
137 dateTimeStr = datetimeUtcDT.strftime("%Y-%m-%dT%H:%M:%S UTC")
138 if removeSpace:
139 return dateTimeStr.replace(" ", "")
140 return dateTimeStr
141
142
143 # Return the string "<YYYY>-<MM>-<DD>" for an input datetime object.
144 def getDateStrFromDateTime(dateTime):
145 return dateTime.strftime("%Y-%m-%d")
146
147
148 # Return the datetime object for just <YYYY>-<MM>-<DD> for an input datetime
149 # object.
150 def getDateOnlyFromDateTime(dateTime):
151 return datetime.datetime(year=dateTime.year, month=dateTime.month, day=dateTime.day)
152
153
154 # Compute the CDash testing day for a given build using its 'buildstarttime'
155 # field and the testing day start time and return it as a datetime object.
156 #
157 # buildStartTimeStr [in]: The 'buildstarttime' field string as returned from
158 # CDash in the format "<YYYY>-<MM>-<DD>T<hh>:<mm>:<ss> <TZ>". Note. the
159 # timezone <TZ> can be any of those supported by the function
160 # getTimeZoneOffset()
161 #
162 # testingDayStartTimeUtcTD [in]: The testing day start time as the a timedelta
163 # object.
164 #
165 # ToDo: Take into account the CDash server timezone which determine what noon
166 # means!
167 #
168 def getTestingDayDateFromBuildStartTimeDT(
169 buildStartTimeStr, testingDayStartTimeUtcTD,
170 ):
171
172 #print()
173 #print("testingDayStartTimeUtcTD = "+str(testingDayStartTimeUtcTD))
174 #print("testingDayStartTimeUtcTD.seconds = "+str(testingDayStartTimeUtcTD.seconds))
175
176 # Constants
177 oneDayTD = datetime.timedelta(days=1)
178 noonTD = getProjectTestingDayStartTimeDeltaFromStr("12:00")
179
180 # Convert input 'buildstartime' to datetime object in UtC
181 buildStartTimeUtcDT = getBuildStartTimeUtcFromStr(buildStartTimeStr)
182
183 # Get the date and time separately for the input 'buildstartime'
184 buildStartTimeDateDT = datetime.datetime(
185 year=buildStartTimeUtcDT.year,
186 month=buildStartTimeUtcDT.month,
187 day=buildStartTimeUtcDT.day,
188 )
189 #print("buildStartTimeDateDT = "+str(buildStartTimeDateDT))
190 buildStartTimeTimeTD = buildStartTimeUtcDT - buildStartTimeDateDT
191 #print("buildStartTimeTimeTD = "+str(buildStartTimeTimeTD))
192 #print("buildStartTimeTimeTD.seconds = "+str(buildStartTimeTimeTD.seconds))
193
194 if buildStartTimeTimeTD.seconds < testingDayStartTimeUtcTD.seconds:
195 buildStartTimeDateDT -= oneDayTD
196 #print("buildStartTimeDateDT = "+str(buildStartTimeDateDT))
197
198 if testingDayStartTimeUtcTD > noonTD:
199 buildStartTimeDateDT += oneDayTD
200 #print("buildStartTimeDateDT = "+str(buildStartTimeDateDT))
201
202 return buildStartTimeDateDT
203
204
205 # Compute the CDash testing day for a given build using its 'buildstarttime'
206 # field and the testing day start time and return as a string "YYYY-MM-DD".
207 #
208 # See getTestingDayDateFromBuildStartTimeDT()
209 #
210 def getTestingDayDateFromBuildStartTimeStr(
211 buildStartTimeStr, testingDayStartTimeUtcTD,
212 ):
213 return getDateStrFromDateTime(
214 getTestingDayDateFromBuildStartTimeDT(buildStartTimeStr, testingDayStartTimeUtcTD) )
215
216
217 # Return the shifted CDash build start relative to the given CDash project
218 # testing day start time (as configured on CDash).
219 #
220 # This function is meant to match the CDash logic for what testing day a given
221 # CDash build matches for the CDash 'date' field in various PHP pages for a
222 # given CDash build with a give build start time (as reported to CDash from
223 # CTest).
224 #
225 # For example, if the CDash project start time is 04:00 UTC and the CDash
226 # build start time is 2018-01-22T00:00 UTC, then the relative CDash build date
227 # time would be 2018-01-21T20:00 UTC which is the previous testing day
228 # 2018-01-21. But if the CDash build start time was 2018-01-22T04:10 UTC,
229 # then the relative CDash build date time would be 2018-01-22:00:10 UTC and
230 # the testing day would be 2018-01-22.
231 #
232 # To extract just the year, month and day from the returned datetime object as
233 # a datetime object, use the function getDateOnlyFromDateTime().
234 #
235 # To extract the "<YYYY>-<MM>-<DD>" string from the returned datetime object,
236 # use the function getDateStrFromDateTime().
237 #
238 def getRelativeCDashBuildStartTime(
239 cdashBuildStartTimeUtc, # CDash build start time in UTC (datetime object)
240 cdashProjectStartTimeUtc, # CDash start time in UTC (timedelta object)
241 dayIncr, # Day increment in past or future (timedelta object)
242 ):
243 relativeCDashBuildDateTime = \
244 cdashBuildStartTimeUtc - cdashProjectStartTimeUtc + dayIncr
245 return relativeCDashBuildDateTime
246
247
248 # Return the shifted CDash build start relative to the given CDash project
249 # testing day start time given input from the command-line arguments.
250 #
251 # This function just converts the input command-line args and then calls
252 # getRelativeCDashBuildStartTime().
253 #
254 # Arguments:
255 #
256 # cdashBuildStartTimeStr [in] Reference build start time in
257 # "YYY-MM-DDThh:mm:ss TZ". If 'None', then is taken from
258 # getCurrentDateTimeUtc().
259 #
260 # cdashProjectStartTimeUtcStr [in] CDash project build start time in
261 # UTC in the format "hh:mm".
262 #
263 # dayIncrInt [in] Integer for the day to return relative to
264 # cdashProjectStartTimeUtcStr. The current testing day would be 0. Yesterday
265 # would be -1 and so on.
266 #
267 # debugLevel [in] If 0, then no debugging. If > 0, then debug output will be
268 # printed.
269 #
270 def getRelativeCDashBuildStartTimeFromCmndLineArgs(
271 cdashBuildStartTimeStr,
272 cdashProjectStartTimeUtcStr,
273 dayIncrInt,
274 debugLevel=0,
275 ):
276
277 if cdashBuildStartTimeStr:
278 buildStartTimeUtc = getBuildStartTimeUtcFromStr(cdashBuildStartTimeStr)
279 else:
280 buildStartTimeUtc = getCurrentDateTimeUtc()
281 if debugLevel: print("buildStartTimeUtc = "+str(buildStartTimeUtc))
282
283 cdashStartTime = \
284 getProjectTestingDayStartTimeDeltaFromStr(cdashProjectStartTimeUtcStr)
285 if debugLevel: print("cdashStartTime = "+str(cdashStartTime))
286
287 dayIncr = getDayIncrTimeDeltaFromInt(dayIncrInt)
288 if debugLevel: print("dayIncr = "+str(dayIncr))
289
290 return getRelativeCDashBuildStartTime(buildStartTimeUtc, cdashStartTime, dayIncr)
291
292
293 # Class to encapsulate handling of CDash testing day logic
294 #
295 class CDashProjectTestingDay(object):
296
297 # Construct
298 #
299 # currentTestingDayDateStr [in]: The current project CDash testing date in
300 # string format "YYYY-MM-DD".
301 #
302 # projectTestingDayStartTimeUtcStr [in]: The CDash projects's testing day start
303 # time in UTC. This is a string in the format "hh:mm".
304 #
305 def __init__(self, currentTestingDayDateStr, projectTestingDayStartTimeUtcStr):
306 # Store input args
307 self.__currentTestingDayDateStr = currentTestingDayDateStr
308 self.__projectTestingDayStartTimeUtcStr = projectTestingDayStartTimeUtcStr
309 # Set the start of the testing day in UTC
310 self.__currentTestingDayDateDT = \
311 datetime.datetime.strptime(currentTestingDayDateStr, "%Y-%m-%d")
312 self.__projectTestingDayStartTimeUtcTD = \
313 getProjectTestingDayStartTimeDeltaFromStr(projectTestingDayStartTimeUtcStr)
314 noonTD = getProjectTestingDayStartTimeDeltaFromStr("12:00")
315 oneDayTD = datetime.timedelta(days=1)
316 if self.__projectTestingDayStartTimeUtcTD >= noonTD:
317 self.__testingDayStartDateTimeUtcDT = \
318 self.__currentTestingDayDateDT - oneDayTD + self.__projectTestingDayStartTimeUtcTD
319 else:
320 self.__testingDayStartDateTimeUtcDT = \
321 self.__currentTestingDayDateDT + self.__projectTestingDayStartTimeUtcTD
322
323 # Return the testing day start in UTC as a datetime object
324 def getTestingDayStartUtcDT(self):
325 return self.__testingDayStartDateTimeUtcDT
326
327 # Return the testing day date (not time) as a datetime object
328 def getCurrentTestingDayDateDT(self):
329 return self.__currentTestingDayDateDT
330
331 # Return the testing day as a datetime object for the input 'buildstartime'
332 # string.
333 def getTestingDayDateFromBuildStartTimeDT(self, buildStartTimeStr):
334 return getTestingDayDateFromBuildStartTimeDT(
335 buildStartTimeStr, self.__projectTestingDayStartTimeUtcTD)
336
337 # Return the testing day string "YYYY-MM-DD" for the input 'buildstartime'
338 # string.
339 def getTestingDayDateFromBuildStartTimeStr(self, buildStartTimeStr):
340 return getDateStrFromDateTime(
341 self.getTestingDayDateFromBuildStartTimeDT(buildStartTimeStr) )
342
343
344 #
345 # Run the script
346 #
347
348 if __name__ == '__main__':
349
350 inOptions = getCmndLineOptions()
351
352 relativeCDashBuildBuildDateTime = getRelativeCDashBuildStartTimeFromCmndLineArgs(
353 inOptions.cdashBuildStartTime, inOptions.cdashProjectStartTimeUtcStr,
354 inOptions.dayIncrInt, inOptions.debugLevel,
355 )
356
357 print(getDateStrFromDateTime(relativeCDashBuildBuildDateTime))