blob: ceb507b26c96ed021dacf9a3c102768a840cc373 [file] [log] [blame]
Nico Weber967d1f12018-08-17 02:42:021#!/usr/bin/env python
2# Copyright 2018 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Returns a timestamp that approximates the build date.
6
7build_type impacts the timestamp generated, both relative to the date of the
8last recent commit:
9- default: the build date is set to the most recent first Sunday of a month at
10 5:00am. The reason is that it is a time where invalidating the build cache
Bruce Dawsonf74fe5d52020-04-17 17:30:0111 shouldn't have major repercussions (due to lower load).
12- official: the build date is set to the time of the most recent commit.
Nico Weber967d1f12018-08-17 02:42:0213Either way, it is guaranteed to be in the past and always in UTC.
14"""
15
16# The requirements for the timestamp:
17# (1) for the purposes of continuous integration, longer duration
18# between cache invalidation is better, but >=1mo is preferable.
19# (2) for security purposes, timebombs would ideally be as close to
20# the actual time of the build as possible. It must be in the past.
21# (3) HSTS certificate pinning is valid for 70 days. To make CI builds enforce
22# HTST pinning, <=1mo is preferable.
23#
24# On Windows, the timestamp is also written in the PE/COFF file header of
25# executables of dlls. That timestamp and the executable's file size are
26# the only two pieces of information that identify a given executable on
27# the symbol server, so rarely changing timestamps can cause conflicts there
28# as well. We only upload symbols for official builds to the symbol server.
29
Raul Tambre4197d3a2019-03-19 15:04:2030from __future__ import print_function
31
Nico Weber967d1f12018-08-17 02:42:0232import argparse
33import calendar
34import datetime
35import doctest
36import os
37import sys
38
39
40THIS_DIR = os.path.abspath(os.path.dirname(__file__))
41
42
43def GetFirstSundayOfMonth(year, month):
44 """Returns the first sunday of the given month of the given year.
45
46 >>> GetFirstSundayOfMonth(2016, 2)
47 7
48 >>> GetFirstSundayOfMonth(2016, 3)
49 6
50 >>> GetFirstSundayOfMonth(2000, 1)
51 2
52 """
53 weeks = calendar.Calendar().monthdays2calendar(year, month)
54 # Return the first day in the first week that is a Sunday.
55 return [date_day[0] for date_day in weeks[0] if date_day[1] == 6][0]
56
57
Bruce Dawson02f0edc2019-08-15 18:23:0558def GetUnofficialBuildDate(build_date):
Nico Weber967d1f12018-08-17 02:42:0259 """Gets the approximate build date given the specific build type.
60
Bruce Dawson02f0edc2019-08-15 18:23:0561 >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 6, 1, 2, 3))
62 datetime.datetime(2016, 1, 3, 5, 0)
63 >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 7, 5))
Nico Weber967d1f12018-08-17 02:42:0264 datetime.datetime(2016, 2, 7, 5, 0)
Bruce Dawson02f0edc2019-08-15 18:23:0565 >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 8, 5))
Nico Weber967d1f12018-08-17 02:42:0266 datetime.datetime(2016, 2, 7, 5, 0)
Nico Weber967d1f12018-08-17 02:42:0267 """
Bruce Dawson02f0edc2019-08-15 18:23:0568
69 if build_date.hour < 5:
70 # The time is locked at 5:00 am in UTC to cause the build cache
71 # invalidation to not happen exactly at midnight. Use the same calculation
72 # as the day before.
73 # See //base/build_time.cc.
74 build_date = build_date - datetime.timedelta(days=1)
75 build_date = datetime.datetime(build_date.year, build_date.month,
76 build_date.day, 5, 0, 0)
77
78 day = build_date.day
79 month = build_date.month
80 year = build_date.year
81 first_sunday = GetFirstSundayOfMonth(year, month)
82 # If our build is after the first Sunday, we've already refreshed our build
83 # cache on a quiet day, so just use that day.
84 # Otherwise, take the first Sunday of the previous month.
85 if day >= first_sunday:
86 day = first_sunday
87 else:
88 month -= 1
89 if month == 0:
90 month = 12
91 year -= 1
92 day = GetFirstSundayOfMonth(year, month)
Nico Weber967d1f12018-08-17 02:42:0293 return datetime.datetime(
Bruce Dawson02f0edc2019-08-15 18:23:0594 year, month, day, build_date.hour, build_date.minute, build_date.second)
Nico Weber967d1f12018-08-17 02:42:0295
96
97def main():
98 if doctest.testmod()[0]:
99 return 1
100 argument_parser = argparse.ArgumentParser()
101 argument_parser.add_argument(
102 'build_type', help='The type of build', choices=('official', 'default'))
103 args = argument_parser.parse_args()
104
105 # The mtime of the revision in build/util/LASTCHANGE is stored in a file
106 # next to it. Read it, to get a deterministic time close to "now".
107 # That date is then modified as described at the top of the file so that
108 # it changes less frequently than with every commit.
109 # This intentionally always uses build/util/LASTCHANGE's commit time even if
110 # use_dummy_lastchange is set.
111 lastchange_file = os.path.join(THIS_DIR, 'util', 'LASTCHANGE.committime')
112 last_commit_timestamp = int(open(lastchange_file).read())
Bruce Dawson02f0edc2019-08-15 18:23:05113 build_date = datetime.datetime.utcfromtimestamp(last_commit_timestamp)
Nico Weber967d1f12018-08-17 02:42:02114
Bruce Dawson02f0edc2019-08-15 18:23:05115 # For official builds we want full fidelity time stamps because official
116 # builds are typically added to symbol servers and Windows symbol servers
117 # use the link timestamp as the prime differentiator, but for unofficial
118 # builds we do lots of quantization to avoid churn.
119 if args.build_type != 'official':
120 build_date = GetUnofficialBuildDate(build_date)
Raul Tambre4197d3a2019-03-19 15:04:20121 print(int(calendar.timegm(build_date.utctimetuple())))
Nico Weber967d1f12018-08-17 02:42:02122 return 0
123
124
125if __name__ == '__main__':
126 sys.exit(main())