Skip to content

Commit 0ea8feb

Browse files
authored
Merge pull request #328 from skirpichev/install_version.py
Generate version.py file for sdist
2 parents 5a9d917 + a474e53 commit 0ea8feb

4 files changed

Lines changed: 79 additions & 27 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ venv/
88
*.egg-info/
99
*.swp
1010
build/
11+
dist/
1112
.local/
13+
version.py

meson.build

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ py = import('python').find_installation(pure: false)
66
libzz = dependency('zz', version: '>= 0.7.0')
77
py.extension_module('gmp', ['fmt.c', 'gmp.c', 'utils.c'],
88
install: true, dependencies: libzz)
9+
install_dir = py.get_install_dir()
10+
11+
# Generate version.py for sdist
12+
meson.add_dist_script(['scripts/gitversion.py', '--meson-dist',
13+
'--write', '_version.py'])
14+
fs = import('fs')
15+
if not fs.exists('_version.py')
16+
generate_version = custom_target('generate-version',
17+
install: true,
18+
build_always_stale: true,
19+
build_by_default: true,
20+
output: '_version.py',
21+
input: 'scripts/gitversion.py',
22+
command: [py, '@INPUT@', '--write',
23+
'@OUTPUT@'],
24+
install_dir: install_dir,
25+
install_tag: 'python-runtime')
26+
endif

scripts/gitversion.py

100644100755
Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,69 @@
11
#!/usr/bin/env python3
2+
3+
import argparse
24
import os
35
import re
46
import subprocess
7+
import sys
58

69

710
def git_version():
8-
# Append last commit date and hash to dev version information,
9-
# if available
10-
return version, git_hash
11-
11+
p = subprocess.Popen(["git", "describe"],
12+
stdout=subprocess.PIPE,
13+
stderr=subprocess.PIPE,
14+
cwd=os.path.dirname(__file__))
15+
out, err = p.communicate()
16+
if p.returncode:
17+
raise RuntimeError("Non-zero return code from git-describe: "
18+
f"{p.returncode}")
19+
out = out.decode("ascii").removesuffix("\n")
1220

13-
if __name__ == "__main__":
14-
try:
15-
p = subprocess.Popen(["git", "describe"],
16-
stdout=subprocess.PIPE,
17-
stderr=subprocess.PIPE,
18-
cwd=os.path.dirname(__file__))
19-
except FileNotFoundError:
20-
exit(1)
21+
version, *other = out.removesuffix("\n").split("-")
22+
if other:
23+
g, h = other
24+
m = re.match("(.*)([0-9]+)", version)
25+
version = m[1] + str(int(m[2])+1) + ".dev" + g
26+
git_hash = h
2127
else:
22-
out, err = p.communicate()
23-
if p.returncode:
24-
exit(p.returncode)
25-
out = out.decode("ascii").removesuffix("\n")
26-
27-
version, *other = out.removesuffix("\n").split("-")
28-
if other:
29-
g, h = other
30-
m = re.match("(.*)([0-9]+)", version)
31-
version = m[1] + str(int(m[2])+1) + ".dev" + g
32-
git_hash = h
33-
else:
34-
git_hash = ""
28+
git_hash = ""
3529

3630
if git_hash:
3731
version += "+" + git_hash
38-
print(version)
39-
exit(0)
32+
return version
33+
34+
35+
if __name__ == "__main__":
36+
parser = argparse.ArgumentParser()
37+
parser.add_argument("--write", help="Save version to this file")
38+
parser.add_argument(
39+
"--meson-dist",
40+
help="Output path is relative to MESON_DIST_ROOT",
41+
action="store_true"
42+
)
43+
args = parser.parse_args()
44+
45+
try:
46+
version = git_version()
47+
except (FileNotFoundError, RuntimeError):
48+
sys.path.insert(0, os.getcwd())
49+
from _version import version
50+
51+
if args.write:
52+
template = f'version = "{version}"'
53+
outfile = args.write
54+
if args.meson_dist:
55+
outfile = os.path.join(
56+
os.environ.get("MESON_DIST_ROOT", ""),
57+
outfile
58+
)
59+
60+
# Print human readable output path
61+
relpath = os.path.relpath(outfile)
62+
if relpath.startswith("."):
63+
relpath = outfile
64+
65+
with open(outfile, "w") as f:
66+
print(f"Saving version to {relpath}")
67+
f.write(template)
68+
else:
69+
print(version)

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def pytest_report_header(config):
2323
Bits per digit : {gmp.mpz_info.bits_per_digit}
2424
sizeof(zz_digit_t): {gmp.mpz_info.sizeof_digit}
2525
Maximal bit count : {gmp.mpz_info.bitcnt_max}
26+
27+
The gmp module v{gmp.__version__}
2628
""")
2729

2830

0 commit comments

Comments
 (0)