Chromium Code Reviews| 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(month, year): | |
|
Nico
2016/02/09 22:53:49
nit: year, month order seems more natural
Zachary Forman
2016/02/09 23:26:23
Fair - I think that's probably true in general. I
| |
| 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 def GetBuildDate(build_type): | |
| 34 """Gets the approximate build date given the specific build type.""" | |
| 35 gmt_now = datetime.datetime.utcnow() | |
|
Nico
2016/02/09 22:53:49
i'd make gmt_now a parameter, then you could have
Zachary Forman
2016/02/09 23:26:23
I like this idea, will implement.
| |
| 36 day = gmt_now.day | |
| 37 month = gmt_now.month | |
| 38 year = gmt_now.year | |
| 39 if build_type != 'official': | |
| 40 first_sunday = GetFirstSundayOfMonth(month, year) | |
| 41 # If our build is after the first Sunday, we've already refreshed our build | |
| 42 # cache on a quiet day, so just use that day. | |
| 43 # Otherwise, take the first Sunday of the previous month. | |
| 44 if day >= first_sunday: | |
| 45 day = first_sunday | |
| 46 else: | |
| 47 month -= 1 | |
| 48 if month == 0: | |
| 49 month = 12 | |
| 50 year -= 1 | |
| 51 day = GetFirstSundayOfMonth(month, year) | |
| 52 return '{:%b %d %Y}'.format(datetime.date(year, month, day)) | |
| 53 | |
|
Nico
2016/02/09 22:53:49
nit: For small scripts like this were having a ded
Zachary Forman
2016/02/09 23:26:23
I like this idea. Will implement.
| |
| 54 | |
| 55 def main(): | |
| 56 argument_parser = argparse.ArgumentParser() | |
| 57 argument_parser.add_argument('output_file', help='The file to write to') | |
| 58 argument_parser.add_argument('build_type', help='The type of build', | |
| 59 choices=('official', 'default')) | |
| 60 argument_parser.add_argument('build_date_override', nargs='?', | |
| 61 help='Optional override for the build date') | |
| 62 args = argument_parser.parse_args() | |
| 63 | |
| 64 if args.build_date_override: | |
| 65 build_date = args.build_date_override | |
| 66 else: | |
| 67 build_date = GetBuildDate(args.build_type) | |
| 68 | |
| 69 output = ('// Generated by //build/write_build_date_header.py\n' | |
| 70 '#ifndef BUILD_DATE\n' | |
| 71 '#define BUILD_DATE "{}"\n' | |
| 72 '#endif // BUILD_DATE\n'.format(build_date)) | |
| 73 | |
| 74 current_contents = '' | |
| 75 if os.path.isfile(args.output_file): | |
| 76 with open(args.output_file, 'r') as current_file: | |
| 77 current_contents = current_file.read() | |
| 78 | |
| 79 if current_contents != output: | |
| 80 with open(args.output_file, 'w') as output_file: | |
| 81 output_file.write(output) | |
| 82 return 0 | |
| 83 | |
| 84 | |
| 85 if __name__ == '__main__': | |
| 86 sys.exit(main()) | |
| OLD | NEW |