compute_build_timestamp.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. build_type impacts the timestamp generated, both relative to the date of the
  7. last recent commit:
  8. - default: the build date is set to the most recent first Sunday of a month at
  9. 5:00am. The reason is that it is a time where invalidating the build cache
  10. shouldn't have major repercussions (due to lower load).
  11. - official: the build date is set to the time of the most recent commit.
  12. Either way, it is guaranteed to be in the past and always in UTC.
  13. """
  14. # The requirements for the timestamp:
  15. # (1) for the purposes of continuous integration, longer duration
  16. # between cache invalidation is better, but >=1mo is preferable.
  17. # (2) for security purposes, timebombs would ideally be as close to
  18. # the actual time of the build as possible. It must be in the past.
  19. # (3) HSTS certificate pinning is valid for 70 days. To make CI builds enforce
  20. # HTST pinning, <=1mo is preferable.
  21. #
  22. # On Windows, the timestamp is also written in the PE/COFF file header of
  23. # executables of dlls. That timestamp and the executable's file size are
  24. # the only two pieces of information that identify a given executable on
  25. # the symbol server, so rarely changing timestamps can cause conflicts there
  26. # as well. We only upload symbols for official builds to the symbol server.
  27. from __future__ import print_function
  28. import argparse
  29. import calendar
  30. import datetime
  31. import doctest
  32. import os
  33. import sys
  34. THIS_DIR = os.path.abspath(os.path.dirname(__file__))
  35. def GetFirstSundayOfMonth(year, month):
  36. """Returns the first sunday of the given month of the given year.
  37. >>> GetFirstSundayOfMonth(2016, 2)
  38. 7
  39. >>> GetFirstSundayOfMonth(2016, 3)
  40. 6
  41. >>> GetFirstSundayOfMonth(2000, 1)
  42. 2
  43. """
  44. weeks = calendar.Calendar().monthdays2calendar(year, month)
  45. # Return the first day in the first week that is a Sunday.
  46. return [date_day[0] for date_day in weeks[0] if date_day[1] == 6][0]
  47. def GetUnofficialBuildDate(build_date):
  48. """Gets the approximate build date given the specific build type.
  49. >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 6, 1, 2, 3))
  50. datetime.datetime(2016, 1, 3, 5, 0)
  51. >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 7, 5))
  52. datetime.datetime(2016, 2, 7, 5, 0)
  53. >>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 8, 5))
  54. datetime.datetime(2016, 2, 7, 5, 0)
  55. """
  56. if build_date.hour < 5:
  57. # The time is locked at 5:00 am in UTC to cause the build cache
  58. # invalidation to not happen exactly at midnight. Use the same calculation
  59. # as the day before.
  60. # See //base/build_time.cc.
  61. build_date = build_date - datetime.timedelta(days=1)
  62. build_date = datetime.datetime(build_date.year, build_date.month,
  63. build_date.day, 5, 0, 0)
  64. day = build_date.day
  65. month = build_date.month
  66. year = build_date.year
  67. first_sunday = GetFirstSundayOfMonth(year, month)
  68. # If our build is after the first Sunday, we've already refreshed our build
  69. # cache on a quiet day, so just use that day.
  70. # Otherwise, take the first Sunday of the previous month.
  71. if day >= first_sunday:
  72. day = first_sunday
  73. else:
  74. month -= 1
  75. if month == 0:
  76. month = 12
  77. year -= 1
  78. day = GetFirstSundayOfMonth(year, month)
  79. return datetime.datetime(
  80. year, month, day, build_date.hour, build_date.minute, build_date.second)
  81. def main():
  82. if doctest.testmod()[0]:
  83. return 1
  84. argument_parser = argparse.ArgumentParser()
  85. argument_parser.add_argument(
  86. 'build_type', help='The type of build', choices=('official', 'default'))
  87. args = argument_parser.parse_args()
  88. # The mtime of the revision in build/util/LASTCHANGE is stored in a file
  89. # next to it. Read it, to get a deterministic time close to "now".
  90. # That date is then modified as described at the top of the file so that
  91. # it changes less frequently than with every commit.
  92. # This intentionally always uses build/util/LASTCHANGE's commit time even if
  93. # use_dummy_lastchange is set.
  94. lastchange_file = os.path.join(THIS_DIR, 'util', 'LASTCHANGE.committime')
  95. last_commit_timestamp = int(open(lastchange_file).read())
  96. build_date = datetime.datetime.utcfromtimestamp(last_commit_timestamp)
  97. # For official builds we want full fidelity time stamps because official
  98. # builds are typically added to symbol servers and Windows symbol servers
  99. # use the link timestamp as the prime differentiator, but for unofficial
  100. # builds we do lots of quantization to avoid churn.
  101. if args.build_type != 'official':
  102. build_date = GetUnofficialBuildDate(build_date)
  103. print(int(calendar.timegm(build_date.utctimetuple())))
  104. return 0
  105. if __name__ == '__main__':
  106. sys.exit(main())