zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 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. |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 5 | """Takes a timestamp and writes it in as readable text to a .h file.""" |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 6 | |
| 7 | import argparse |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 8 | import datetime |
| 9 | import os |
| 10 | import sys |
| 11 | |
| 12 | |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 13 | def main(): |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 14 | argument_parser = argparse.ArgumentParser() |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 15 | argument_parser.add_argument('output_file', help='The file to write to') |
Greg Thompson | 05eb6e2 | 2018-11-05 12:14:55 | [diff] [blame] | 16 | argument_parser.add_argument('timestamp') |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 17 | args = argument_parser.parse_args() |
| 18 | |
Greg Thompson | 05eb6e2 | 2018-11-05 12:14:55 | [diff] [blame] | 19 | date = datetime.datetime.utcfromtimestamp(int(args.timestamp)) |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 20 | output = ('// Generated by //build/write_build_date_header.py\n' |
| 21 | '#ifndef BUILD_DATE\n' |
Nico Weber | 967d1f1 | 2018-08-17 02:42:02 | [diff] [blame] | 22 | '#define BUILD_DATE "{:%b %d %Y %H:%M:%S}"\n' |
| 23 | '#endif // BUILD_DATE\n'.format(date)) |
zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 24 | |
| 25 | current_contents = '' |
| 26 | if os.path.isfile(args.output_file): |
| 27 | with open(args.output_file, 'r') as current_file: |
| 28 | current_contents = current_file.read() |
| 29 | |
| 30 | if current_contents != output: |
| 31 | with open(args.output_file, 'w') as output_file: |
| 32 | output_file.write(output) |
| 33 | return 0 |
| 34 | |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | sys.exit(main()) |