Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ venv/
*.egg-info/
*.swp
build/
dist/
.local/
version.py
18 changes: 18 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ py = import('python').find_installation(pure: false)
libzz = dependency('zz', version: '>= 0.7.0')
py.extension_module('gmp', ['fmt.c', 'gmp.c', 'utils.c'],
install: true, dependencies: libzz)
install_dir = py.get_install_dir()

# Generate version.py for sdist
meson.add_dist_script(['scripts/gitversion.py', '--meson-dist',
'--write', '_version.py'])
fs = import('fs')
if not fs.exists('_version.py')
generate_version = custom_target('generate-version',
install: true,
build_always_stale: true,
build_by_default: true,
output: '_version.py',
input: 'scripts/gitversion.py',
command: [py, '@INPUT@', '--write',
'@OUTPUT@'],
install_dir: install_dir,
install_tag: 'python-runtime')
endif
84 changes: 57 additions & 27 deletions scripts/gitversion.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
#!/usr/bin/env python3

import argparse
import os
import re
import subprocess
import sys


def git_version():
# Append last commit date and hash to dev version information,
# if available
return version, git_hash

p = subprocess.Popen(["git", "describe"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(__file__))
out, err = p.communicate()
if p.returncode:
raise RuntimeError("Non-zero return code from git-describe: "
f"{p.returncode}")
out = out.decode("ascii").removesuffix("\n")

if __name__ == "__main__":
try:
p = subprocess.Popen(["git", "describe"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(__file__))
except FileNotFoundError:
exit(1)
version, *other = out.removesuffix("\n").split("-")
if other:
g, h = other
m = re.match("(.*)([0-9]+)", version)
version = m[1] + str(int(m[2])+1) + ".dev" + g
git_hash = h
else:
out, err = p.communicate()
if p.returncode:
exit(p.returncode)
out = out.decode("ascii").removesuffix("\n")

version, *other = out.removesuffix("\n").split("-")
if other:
g, h = other
m = re.match("(.*)([0-9]+)", version)
version = m[1] + str(int(m[2])+1) + ".dev" + g
git_hash = h
else:
git_hash = ""
git_hash = ""

if git_hash:
version += "+" + git_hash
print(version)
exit(0)
return version


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--write", help="Save version to this file")
parser.add_argument(
"--meson-dist",
help="Output path is relative to MESON_DIST_ROOT",
action="store_true"
)
args = parser.parse_args()

try:
version = git_version()
except (FileNotFoundError, RuntimeError):
sys.path.insert(0, os.getcwd())
from _version import version

if args.write:
template = f'version = "{version}"'
outfile = args.write
if args.meson_dist:
outfile = os.path.join(
os.environ.get("MESON_DIST_ROOT", ""),
outfile
)

# Print human readable output path
relpath = os.path.relpath(outfile)
if relpath.startswith("."):
relpath = outfile

with open(outfile, "w") as f:
print(f"Saving version to {relpath}")
f.write(template)
else:
print(version)
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def pytest_report_header(config):
Bits per digit : {gmp.mpz_info.bits_per_digit}
sizeof(zz_digit_t): {gmp.mpz_info.sizeof_digit}
Maximal bit count : {gmp.mpz_info.bitcnt_max}

The gmp module v{gmp.__version__}
""")


Expand Down
Loading