-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathgenerate_version.py
More file actions
executable file
·59 lines (47 loc) · 1.65 KB
/
Copy pathgenerate_version.py
File metadata and controls
executable file
·59 lines (47 loc) · 1.65 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 The meson-python developers
#
# SPDX-License-Identifier: MIT
import argparse
import os
import subprocess
def get_version_from_pyproject():
here = os.path.dirname(os.path.abspath(__file__))
pyproject_toml = os.path.join(here, 'pyproject.toml')
with open(pyproject_toml) as f:
for line in f:
if line.startswith('version ='):
return line.split('=', 1)[1].strip().strip('\'"')
raise RuntimeError('version not found in pyproject.toml')
def get_git_hash():
here = os.path.dirname(os.path.abspath(__file__))
try:
result = subprocess.run(
['git', 'rev-parse', 'HEAD'],
cwd=here,
capture_output=True,
check=True,
text=True,
)
except (subprocess.CalledProcessError, FileNotFoundError):
return 'unknown'
return result.stdout.strip()
def write_version_file(outfile, version, git_hash):
if 'MESON_DIST_ROOT' in os.environ:
outfile = os.path.join(os.environ['MESON_DIST_ROOT'], outfile)
with open(outfile, 'w') as f:
f.write(f"__version__ = '{version}'\n")
f.write(f"__git_hash__ = '{git_hash}'\n")
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--print-version', action='store_true')
group.add_argument('-o', '--outfile')
args = parser.parse_args()
version = get_version_from_pyproject()
if args.print_version:
print(version)
return
write_version_file(args.outfile, version, get_git_hash())
if __name__ == '__main__':
main()