OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2016 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 """Writes a file that contains a define that approximates the build date. |
| 6 |
| 7 For unofficial builds, the build date is set to the most recent first Sunday |
| 8 of a month, in UTC time. |
| 9 |
| 10 For official builds, the build date is set to the current date (in UTC). |
| 11 |
| 12 It is also possible to explicitly set a build date to be used. |
| 13 |
| 14 The reason for using the first Sunday of a month for unofficial builds is that |
| 15 it is a time where invalidating the build cache shouldn't have major |
| 16 reprecussions (due to lower load). |
| 17 """ |
| 18 |
| 19 import argparse |
| 20 import calendar |
| 21 import datetime |
| 22 import os |
| 23 import sys |
| 24 |
| 25 |
| 26 def GetFirstSundayOfMonth(year, month): |
| 27 """Returns the first sunday of the given month of the given year.""" |
| 28 weeks = calendar.Calendar().monthdays2calendar(year, month) |
| 29 # Return the first day in the first week that is a Sunday. |
| 30 return [date_day[0] for date_day in weeks[0] if date_day[1] == 6][0] |
| 31 |
| 32 |
| 33 # Validate that GetFirstSundayOfMonth works. |
| 34 assert GetFirstSundayOfMonth(2016, 2) == 7 |
| 35 assert GetFirstSundayOfMonth(2016, 3) == 6 |
| 36 assert GetFirstSundayOfMonth(2000, 1) == 2 |
| 37 |
| 38 |
| 39 def GetBuildDate(build_type, utc_now): |
| 40 """Gets the approximate build date given the specific build type.""" |
| 41 day = utc_now.day |
| 42 month = utc_now.month |
| 43 year = utc_now.year |
| 44 if build_type != 'official': |
| 45 first_sunday = GetFirstSundayOfMonth(year, month) |
| 46 # If our build is after the first Sunday, we've already refreshed our build |
| 47 # cache on a quiet day, so just use that day. |
| 48 # Otherwise, take the first Sunday of the previous month. |
| 49 if day >= first_sunday: |
| 50 day = first_sunday |
| 51 else: |
| 52 month -= 1 |
| 53 if month == 0: |
| 54 month = 12 |
| 55 year -= 1 |
| 56 day = GetFirstSundayOfMonth(year, month) |
| 57 return '{:%b %d %Y}'.format(datetime.date(year, month, day)) |
| 58 |
| 59 |
| 60 # Validate that GetBuildDate works. |
| 61 assert GetBuildDate('default', datetime.date(2016, 2, 6)) == 'Jan 03 2016' |
| 62 assert GetBuildDate('default', datetime.date(2016, 2, 7)) == 'Feb 07 2016' |
| 63 assert GetBuildDate('default', datetime.date(2016, 2, 8)) == 'Feb 07 2016' |
| 64 |
| 65 |
| 66 def main(): |
| 67 argument_parser = argparse.ArgumentParser() |
| 68 argument_parser.add_argument('output_file', help='The file to write to') |
| 69 argument_parser.add_argument('build_type', help='The type of build', |
| 70 choices=('official', 'default')) |
| 71 argument_parser.add_argument('build_date_override', nargs='?', |
| 72 help='Optional override for the build date') |
| 73 args = argument_parser.parse_args() |
| 74 |
| 75 if args.build_date_override: |
| 76 build_date = args.build_date_override |
| 77 else: |
| 78 build_date = GetBuildDate(args.build_type, datetime.datetime.utcnow()) |
| 79 |
| 80 output = ('// Generated by //build/write_build_date_header.py\n' |
| 81 '#ifndef BUILD_DATE\n' |
| 82 '#define BUILD_DATE "{}"\n' |
| 83 '#endif // BUILD_DATE\n'.format(build_date)) |
| 84 |
| 85 current_contents = '' |
| 86 if os.path.isfile(args.output_file): |
| 87 with open(args.output_file, 'r') as current_file: |
| 88 current_contents = current_file.read() |
| 89 |
| 90 if current_contents != output: |
| 91 with open(args.output_file, 'w') as output_file: |
| 92 output_file.write(output) |
| 93 return 0 |
| 94 |
| 95 |
| 96 if __name__ == '__main__': |
| 97 sys.exit(main()) |
OLD | NEW |