1#!/usr/bin/env python 2# Copyright 2018 The ANGLE Project 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 6# Generate commit.h with git commit hash. 7# 8 9import subprocess as sp 10import sys 11import os 12 13usage = """\ 14Usage: commit_id.py check <angle_dir> - check if git is present 15 commit_id.py gen <angle_dir> <file_to_write> - generate commit.h""" 16 17 18def grab_output(command, cwd): 19 return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip() 20 21 22if len(sys.argv) < 3: 23 sys.exit(usage) 24 25operation = sys.argv[1] 26cwd = sys.argv[2] 27 28if operation == 'check': 29 index_path = os.path.join(cwd, '.git', 'index') 30 if os.path.exists(index_path): 31 print("1") 32 else: 33 print("0") 34 sys.exit(0) 35 36if len(sys.argv) < 4 or operation != 'gen': 37 sys.exit(usage) 38 39output_file = sys.argv[3] 40commit_id_size = 12 41 42try: 43 commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd) 44 commit_date = grab_output('git show -s --format=%ci HEAD', cwd) 45except: 46 commit_id = 'invalid-hash' 47 commit_date = 'invalid-date' 48 49hfile = open(output_file, 'w') 50 51hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % commit_id) 52hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size) 53hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date) 54 55hfile.close() 56