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. | |
|
M-A Ruel
2016/02/04 15:24:13
...
# found in the LICENSE file.
"""Writes a file
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 5 """ | |
| 6 Writes a file that contains a define that approximates the build date. | |
| 7 The build date is set to the most recent first Sunday of a month, in GMT time. | |
| 8 """ | |
| 9 | |
| 10 from calendar import Calendar | |
|
M-A Ruel
2016/02/04 15:24:13
The style is to import only the package, then sort
Zachary Forman
2016/02/04 22:29:11
Acknowledged and fixed.
| |
| 11 from datetime import datetime, date | |
| 12 import argparse | |
| 13 import sys | |
| 14 | |
| 15 | |
| 16 def GetFirstSundayOfMonth(month, year): | |
| 17 """ | |
|
M-A Ruel
2016/02/04 15:24:13
"""Returns ...
same everywhere.
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 18 Returns the first sunday of the given month of the given year. | |
| 19 """ | |
| 20 cal = Calendar() | |
| 21 weeks = cal.monthdays2calendar(year, month) | |
|
M-A Ruel
2016/02/04 15:24:13
weeks = calendar.Calendar().monthdays2calendar(yea
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 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] | |
|
M-A Ruel
2016/02/04 15:24:13
return [date_day[0] ...
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 24 | |
| 25 | |
| 26 def GetBuildDate(build_type): | |
| 27 """ | |
| 28 Gets the approximate build date given the specific build type. | |
| 29 """ | |
| 30 gmt_now = datetime.utcnow() | |
| 31 day = gmt_now.day | |
| 32 month = gmt_now.month | |
| 33 year = gmt_now.year | |
| 34 if build_type != 'official': | |
| 35 first_sunday = GetFirstSundayOfMonth(month, year) | |
| 36 # If our build is after the first Sunday, we've already refreshed our build | |
| 37 # cache on a quiet day, so just use that day. | |
| 38 # Otherwise, take the first Sunday of the previous month. | |
| 39 if day >= first_sunday: | |
| 40 day = first_sunday | |
| 41 else: | |
| 42 month -= 1 | |
| 43 if month == 0: | |
| 44 month = 12 | |
| 45 year -= 1 | |
| 46 day = GetFirstSundayOfMonth(month, year) | |
| 47 return "{:%b %d %Y}".format(date(year, month, day)) | |
| 48 | |
| 49 | |
| 50 def main(): | |
| 51 argument_parser = argparse.ArgumentParser() | |
| 52 argument_parser.add_argument('output_file', help='The file to write to') | |
| 53 argument_parser.add_argument('build_type', help='The type of build') | |
|
M-A Ruel
2016/02/04 15:24:13
Use choices=().
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 54 argument_parser.add_argument('build_date_override', nargs='?', default=None, | |
|
M-A Ruel
2016/02/04 15:24:13
Remove default=None, it's the default.
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 55 help='Optional override for the build date') | |
| 56 args = argument_parser.parse_args() | |
| 57 | |
| 58 if args.build_date_override is not None: | |
|
M-A Ruel
2016/02/04 15:24:13
if args.build_date_override:
Zachary Forman
2016/02/04 22:29:11
Done
| |
| 59 build_date = args.build_date_override | |
| 60 else: | |
| 61 build_date = GetBuildDate(args.build_type) | |
| 62 | |
| 63 with open(args.output_file, 'w') as output_file: | |
| 64 output_file.write('// Generated by //build/write_build_date_header.py\n') | |
| 65 output_file.write('#ifndef BUILD_DATE\n') | |
| 66 output_file.write('#define BUILD_DATE \"{}\"\n'.format(build_date)) | |
| 67 output_file.write('#endif // BUILD_DATE\n') | |
| 68 return 0 | |
| 69 | |
| 70 | |
| 71 if __name__ == '__main__': | |
| 72 sys.exit(main()) | |
| OLD | NEW |