xref: /aosp_15_r20/external/cronet/base/write_build_date_header.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2# Copyright 2016 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Takes a timestamp and writes it in as readable text to a .h file."""
6
7import argparse
8import datetime
9import os
10import sys
11
12def main():
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()
17
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')
25
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()
30
31    if current_contents != output:
32        with open(args.output_file, 'w') as output_file:
33            output_file.write(output)
34    return 0
35
36if __name__ == '__main__':
37    sys.exit(main())
38