aboutsummaryrefslogtreecommitdiffstats
path: root/meson/test cases/common/65 build always/version_gen.py
blob: fbe2df93f7c77c6db4c2616a7160ec590c92d5ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3

import sys, os, subprocess

def generate(infile, outfile, fallback):
    workdir = os.path.split(infile)[0]
    if workdir == '':
        workdir = '.'
    try:
        version = subprocess.check_output(['git', 'describe'], cwd=workdir).decode().strip()
    except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
        version = fallback
    with open(infile) as f:
        newdata = f.read().replace('@VERSION@', version)
    try:
        with open(outfile) as f:
            olddata = f.read()
        if olddata == newdata:
            return
    except OSError:
        pass
    with open(outfile, 'w') as f:
        f.write(newdata)

if __name__ == '__main__':
    infile = sys.argv[1]
    outfile = sys.argv[2]
    fallback = sys.argv[3]
    generate(infile, outfile, fallback)