| Takuto Ikuta | 3dab32e0 | 2023-01-12 18:52:00 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2016 The Chromium Authors |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 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 | |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 12 | def main(): |
| Ho Cheung | 1775adf | 2023-10-26 16:01:45 | [diff] [blame] | 13 | argument_parser = argparse.ArgumentParser() |
| 14 | argument_parser.add_argument('output_file', help='The file to write to') |
| 15 | argument_parser.add_argument('timestamp') |
| 16 | args = argument_parser.parse_args() |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 17 | |
| Ho Cheung | 1775adf | 2023-10-26 16:01:45 | [diff] [blame] | 18 | date_val = int(args.timestamp) |
| 19 | date = datetime.datetime.fromtimestamp(date_val, tz=datetime.timezone.utc) |
| 20 | output = ('// Generated by //base/write_build_date_header.py\n' |
| 21 | '#ifndef BASE_GENERATED_BUILD_DATE_TIMESTAMP \n' |
| 22 | f'#define BASE_GENERATED_BUILD_DATE_TIMESTAMP {date_val}' |
| 23 | f' // {date:%b %d %Y %H:%M:%S}\n' |
| 24 | '#endif // BASE_GENERATED_BUILD_DATE_TIMESTAMP \n') |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 25 | |
| Ho Cheung | 1775adf | 2023-10-26 16:01:45 | [diff] [blame] | 26 | current_contents = '' |
| 27 | if os.path.isfile(args.output_file): |
| 28 | with open(args.output_file, 'r') as current_file: |
| 29 | current_contents = current_file.read() |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 30 | |
| Ho Cheung | 1775adf | 2023-10-26 16:01:45 | [diff] [blame] | 31 | if current_contents != output: |
| 32 | with open(args.output_file, 'w') as output_file: |
| 33 | output_file.write(output) |
| 34 | return 0 |
| zforman | 08d91b7 | 2016-02-12 06:23:42 | [diff] [blame] | 35 | |
| 36 | if __name__ == '__main__': |
| Ho Cheung | 1775adf | 2023-10-26 16:01:45 | [diff] [blame] | 37 | sys.exit(main()) |