write_build_date_header.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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.
  5. """Takes a timestamp and writes it in as readable text to a .h file."""
  6. import argparse
  7. import datetime
  8. import os
  9. import sys
  10. def main():
  11. argument_parser = argparse.ArgumentParser()
  12. argument_parser.add_argument('output_file', help='The file to write to')
  13. argument_parser.add_argument('timestamp')
  14. args = argument_parser.parse_args()
  15. date = datetime.datetime.utcfromtimestamp(int(args.timestamp))
  16. output = ('// Generated by //build/write_build_date_header.py\n'
  17. '#ifndef BUILD_DATE\n'
  18. '#define BUILD_DATE "{:%b %d %Y %H:%M:%S}"\n'
  19. '#endif // BUILD_DATE\n'.format(date))
  20. current_contents = ''
  21. if os.path.isfile(args.output_file):
  22. with open(args.output_file, 'r') as current_file:
  23. current_contents = current_file.read()
  24. if current_contents != output:
  25. with open(args.output_file, 'w') as output_file:
  26. output_file.write(output)
  27. return 0
  28. if __name__ == '__main__':
  29. sys.exit(main())