Debian Codegraph @

test cases/common/65 build always/version_gen.py

raw
1 #!/usr/bin/env python3
2
3 import sys, os, subprocess
4
5 def generate(infile, outfile, fallback):
6 workdir = os.path.split(infile)[0]
7 if workdir == '':
8 workdir = '.'
9 try:
10 version = subprocess.check_output(['git', 'describe'], cwd=workdir).decode().strip()
11 except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
12 version = fallback
13 with open(infile) as f:
14 newdata = f.read().replace('@VERSION@', version)
15 try:
16 with open(outfile) as f:
17 olddata = f.read()
18 if olddata == newdata:
19 return
20 except OSError:
21 pass
22 with open(outfile, 'w', encoding='utf-8') as f:
23 f.write(newdata)
24
25 if __name__ == '__main__':
26 infile = sys.argv[1]
27 outfile = sys.argv[2]
28 fallback = sys.argv[3]
29 generate(infile, outfile, fallback)