Debian Codegraph @

mesonbuild/scripts/vcstagger.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2015-2016 The Meson development team
3
4 from __future__ import annotations
5
6 import sys, os, subprocess, re
7 import typing as T
8
9 def config_vcs_tag(infile: str, outfile: str, fallback: str, source_dir: str, replace_string: str, regex_selector: str, cmd: T.List[str]) -> None:
10 try:
11 output = subprocess.check_output(cmd, cwd=source_dir, stderr=subprocess.DEVNULL)
12 new_string = re.search(regex_selector, output.decode()).group(1).rstrip('\r\n')
13 except Exception:
14 new_string = fallback
15
16 with open(infile, encoding='utf-8') as f:
17 new_data = f.read().replace(replace_string, new_string)
18 if os.path.exists(outfile):
19 with open(outfile, encoding='utf-8') as f:
20 needs_update = f.read() != new_data
21 else:
22 needs_update = True
23 if needs_update:
24 with open(outfile, 'w', encoding='utf-8') as f:
25 f.write(new_data)
26
27
28 def run(args: T.List[str]) -> int:
29 infile, outfile, fallback, source_dir, replace_string, regex_selector = args[0:6]
30 command = args[6:]
31 config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, command)
32 return 0
33
34 if __name__ == '__main__':
35 sys.exit(run(sys.argv[1:]))