Chromium Code Reviews| Index: build/write_build_date_header.py |
| diff --git a/build/write_build_date_header.py b/build/write_build_date_header.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..f7445cc1bb5c6f53168a64d96ba42e65a55acc1a |
| --- /dev/null |
| +++ b/build/write_build_date_header.py |
| @@ -0,0 +1,72 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# 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
|
| +""" |
| +Writes a file that contains a define that approximates the build date. |
| +The build date is set to the most recent first Sunday of a month, in GMT time. |
| +""" |
| + |
| +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.
|
| +from datetime import datetime, date |
| +import argparse |
| +import sys |
| + |
| + |
| +def GetFirstSundayOfMonth(month, year): |
| + """ |
|
M-A Ruel
2016/02/04 15:24:13
"""Returns ...
same everywhere.
Zachary Forman
2016/02/04 22:29:11
Done
|
| + Returns the first sunday of the given month of the given year. |
| + """ |
| + cal = Calendar() |
| + 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
|
| + # Return the first day in the first week that is a Sunday. |
| + 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
|
| + |
| + |
| +def GetBuildDate(build_type): |
| + """ |
| + Gets the approximate build date given the specific build type. |
| + """ |
| + gmt_now = datetime.utcnow() |
| + day = gmt_now.day |
| + month = gmt_now.month |
| + year = gmt_now.year |
| + if build_type != 'official': |
| + first_sunday = GetFirstSundayOfMonth(month, year) |
| + # If our build is after the first Sunday, we've already refreshed our build |
| + # cache on a quiet day, so just use that day. |
| + # Otherwise, take the first Sunday of the previous month. |
| + if day >= first_sunday: |
| + day = first_sunday |
| + else: |
| + month -= 1 |
| + if month == 0: |
| + month = 12 |
| + year -= 1 |
| + day = GetFirstSundayOfMonth(month, year) |
| + return "{:%b %d %Y}".format(date(year, month, day)) |
| + |
| + |
| +def main(): |
| + argument_parser = argparse.ArgumentParser() |
| + argument_parser.add_argument('output_file', help='The file to write to') |
| + 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
|
| + 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
|
| + help='Optional override for the build date') |
| + args = argument_parser.parse_args() |
| + |
| + 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
|
| + build_date = args.build_date_override |
| + else: |
| + build_date = GetBuildDate(args.build_type) |
| + |
| + with open(args.output_file, 'w') as output_file: |
| + output_file.write('// Generated by //build/write_build_date_header.py\n') |
| + output_file.write('#ifndef BUILD_DATE\n') |
| + output_file.write('#define BUILD_DATE \"{}\"\n'.format(build_date)) |
| + output_file.write('#endif // BUILD_DATE\n') |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |