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