|
1 | 1 | #!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
2 | 4 | import os |
3 | 5 | import re |
4 | 6 | import subprocess |
| 7 | +import sys |
5 | 8 |
|
6 | 9 |
|
7 | 10 | 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") |
12 | 20 |
|
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 |
21 | 27 | 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 = "" |
35 | 29 |
|
36 | 30 | if git_hash: |
37 | 31 | 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) |
0 commit comments