X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=version.py;h=3213e3277a3a4dac17f71a4c368bc67b4031c574;hb=3d787920d51a35e74e442c7265be3b13b69ad8e4;hp=ff491bb5dae7f7909a4e0182d34d2150ba2129de;hpb=33f0918cde36accb606271c379bc8e75973e9d59;p=tinc diff --git a/version.py b/version.py index ff491bb5..3213e327 100755 --- a/version.py +++ b/version.py @@ -1,26 +1,51 @@ #!/usr/bin/env python3 -from sys import argv, exit +"""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-" +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"), "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 not result.returncode and version and version.startswith(prefix): - version = version[len(prefix):].strip() +if not version: + try: + 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() print(version if version else "unknown", end="") -exit(not version)