1 import subprocess as sp 2 import sys 3 import os 4 5 # Usage: commit_id.py check <angle_dir> (checks if git is present) 6 # Usage: commit_id.py gen <angle_dir> <file_to_write> (generates commit id) 7 8 def grab_output(command, cwd): 9 return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip() 10 11 operation = sys.argv[1] 12 cwd = sys.argv[2] 13 14 if operation == 'check': 15 index_path = os.path.join(cwd, '.git', 'index') 16 if os.path.exists(index_path): 17 print("1") 18 else: 19 print("0") 20 sys.exit(0) 21 22 output_file = sys.argv[3] 23 commit_id_size = 12 24 25 try: 26 commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd) 27 commit_date = grab_output('git show -s --format=%ci HEAD', cwd) 28 except: 29 commit_id = 'invalid-hash' 30 commit_date = 'invalid-date' 31 32 hfile = open(output_file, 'w') 33 34 hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % commit_id) 35 hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size) 36 hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date) 37 38 hfile.close() 39