Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 1 | #!/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 | |
| 7 | build_type impacts the timestamp generated, both relative to the date of the |
| 8 | last 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 Dawson | f74fe5d5 | 2020-04-17 17:30:01 | [diff] [blame] | 11 | 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 Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 13 | Either 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 Tambre | 4197d3a | 2019-03-19 15:04:20 | [diff] [blame] | 30 | from __future__ import print_function |
| 31 | |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 32 | import argparse |
| 33 | import calendar |
| 34 | import datetime |
| 35 | import doctest |
| 36 | import os |
| 37 | import sys |
| 38 | |
| 39 | |
| 40 | THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 41 | |
| 42 | |
| 43 | def 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 Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 58 | def GetUnofficialBuildDate(build_date): |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 59 | """Gets the approximate build date given the specific build type. |
| 60 | |
Bruce Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 61 | >>> 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 Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 64 | datetime.datetime(2016, 2, 7, 5, 0) |
Bruce Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 65 | >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 8, 5)) |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 66 | datetime.datetime(2016, 2, 7, 5, 0) |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 67 | """ |
Bruce Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 68 | |
| 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 Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 93 | return datetime.datetime( |
Bruce Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 94 | year, month, day, build_date.hour, build_date.minute, build_date.second) |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def 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 Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 113 | build_date = datetime.datetime.utcfromtimestamp(last_commit_timestamp) |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 114 | |
Bruce Dawson | 02f0edc | 2019-08-15 18:23:05 | [diff] [blame] | 115 | # 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 Tambre | 4197d3a | 2019-03-19 15:04:20 | [diff] [blame] | 121 | print(int(calendar.timegm(build_date.utctimetuple()))) |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 122 | return 0 |
| 123 | |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | sys.exit(main()) |