1#!/usr/bin/env python
2
3"""
4Generate the contents of the git_sha1.h file.
5The output of this script goes to stdout.
6"""
7
8
9import argparse
10import os
11import os.path
12import subprocess
13import sys
14
15
16def get_git_sha1():
17    """Try to get the git SHA1 with git rev-parse."""
18    git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git')
19    try:
20        git_sha1 = subprocess.check_output([
21            'git',
22            '--git-dir=' + git_dir,
23            'rev-parse',
24            'HEAD',
25        ], stderr=open(os.devnull, 'w')).decode("ascii")
26    except:
27        # don't print anything if it fails
28        git_sha1 = ''
29    return git_sha1
30
31parser = argparse.ArgumentParser()
32parser.add_argument('--output', help='File to write the #define in',
33        required=True)
34args = parser.parse_args()
35
36git_sha1 = os.environ.get('MESA_GIT_SHA1_OVERRIDE', get_git_sha1())[:10]
37if git_sha1:
38    git_sha1_h_in_path = os.path.join(os.path.dirname(sys.argv[0]),
39            '..', 'src', 'git_sha1.h.in')
40    with open(git_sha1_h_in_path , 'r') as git_sha1_h_in:
41        new_sha1 = git_sha1_h_in.read().replace('@VCS_TAG@', git_sha1)
42        if os.path.isfile(args.output):
43            with open(args.output, 'r') as git_sha1_h:
44                if git_sha1_h.read() == new_sha1:
45                    quit()
46        with open(args.output, 'w') as git_sha1_h:
47            git_sha1_h.write(new_sha1)
48else:
49    open(args.output, 'w').close()
50