1#!/usr/bin/env python 2# 3# Copyright 2017, Data61 4# Commonwealth Scientific and Industrial Research Organisation (CSIRO) 5# ABN 41 687 119 230. 6# 7# This software may be distributed and modified according to the terms of 8# the BSD 2-Clause license. Note that NO WARRANTY is provided. 9# See "LICENSE_BSD2.txt" for details. 10# 11# @TAG(DATA61_BSD) 12# 13 14import subprocess 15import datetime 16import sys 17 18output = [] 19 20# Check arguments. 21if len(sys.argv) != 2: 22 print("Usage: gen_env.py <output file>") 23 sys.exit(1) 24output_filename = sys.argv[1] 25 26# Fetch details about the current repo revision. 27p = subprocess.Popen(["git", "log", "-r", "HEAD", "-n", "1", "--pretty=format:%ci"], 28 stdout=subprocess.PIPE) 29commit_date_string = p.communicate()[0] 30 31# in python3, this is a bytes, so convert it to a str 32if isinstance(commit_date_string, bytes): 33 commit_date_string = commit_date_string.decode("utf-8") 34 35commit_date = datetime.datetime.strptime(commit_date_string.split()[0], "%Y-%m-%d") 36 37# Output in a format that LaTeX can read. 38output.append('\\newcommand{\\commitdate}{%s}' % ( 39 commit_date.strftime("%-d %B %Y"))) 40output.append('\\newcommand{\\commityear}{%s}' % ( 41 commit_date.strftime("%Y"))) 42 43# Output file, if it has changed. 44new_data = "\n".join(output) + "\n" 45old_data = None 46try: 47 with open(sys.argv[1], "r") as f: 48 old_data = f.read() 49except: 50 pass 51if new_data != old_data: 52 with open(sys.argv[1], "w") as f: 53 f.write(new_data) 54 55# Done! 56sys.exit(0) 57 58