X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=version.py;h=3213e3277a3a4dac17f71a4c368bc67b4031c574;hb=4436af55e55e79b496264fe114039fbc1198d71f;hp=e9419a03999619d6405d4afb44e4075885e36cbc;hpb=ee988a6e3758994cb0552ef17f872e2530e7ceff;p=tinc diff --git a/version.py b/version.py index e9419a03..3213e327 100755 --- a/version.py +++ b/version.py @@ -1,36 +1,51 @@ #!/usr/bin/env python3 +"""Print current tinc version for using in build scripts. + +First try to determine the latest version using git tags. If this fails (because +the .git directory is missing, git is not installed, or for some other reason), +fall back to using the VERSION file. If it is not present or could not be read, +use 'unknown'. +""" + from os import path, environ from sys import argv, stderr import subprocess as subp +import typing as T -prefix = "release-" -source_root = path.dirname(path.realpath(__file__)) -source_root = environ.get("MESON_SOURCE_ROOT", source_root) +PREFIX = "release-" +SOURCE_ROOT = path.dirname(path.realpath(__file__)) +SOURCE_ROOT = environ.get("MESON_SOURCE_ROOT", SOURCE_ROOT) cmd = [ "git", "--git-dir", - path.join(source_root, ".git"), + path.join(SOURCE_ROOT, ".git"), "describe", "--always", "--tags", - "--match=" + prefix + "*", + "--match=" + PREFIX + "*", ] if "short" in argv: cmd.append("--abbrev=0") -result = subp.run(cmd, stdout=subp.PIPE, encoding="utf-8") -version = result.stdout +version: T.Optional[str] = None + +try: + result = subp.run(cmd, stdout=subp.PIPE, encoding="utf-8", check=False) + if not result.returncode: + version = result.stdout +except FileNotFoundError: + pass -if result.returncode or not version: +if not version: try: - with open(path.join(source_root, "VERSION"), "r") as f: + with open(path.join(SOURCE_ROOT, "VERSION"), "r", encoding="utf-8") as f: version = f.read().strip() except OSError as e: print("could not read version from file", e, file=stderr) -elif version.startswith(prefix): - version = version[len(prefix):].strip() +elif version.startswith(PREFIX): + version = version[len(PREFIX) :].strip() print(version if version else "unknown", end="")