diff --git a/core/__init__.py b/core/__init__.py index 606246c14..e5cdf8297 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1 +1,37 @@ -from core.config import Config \ No newline at end of file +from core.config import Config +from subprocess import run, PIPE +from collections import namedtuple + +__all__ = ["Config", "__version__"] +version_info = namedtuple("VersionInfo", "major minor patch") + +BASE_VERSION = version_info(3, 0, 0) + + +def get_latest_version(): + try: + p = run( + "git describe --abbrev=0 --tags".split(), + stdout=PIPE + ) + except FileNotFoundError: + # No git + return BASE_VERSION + + if p.returncode != 0: + return BASE_VERSION + + stdout = p.stdout.strip().decode() + if stdout.startswith("v"): + numbers = stdout[1:].split('.') + args = [0, 0, 0] + for i in range(3): + try: + args[i] = int(numbers[i]) + except (IndexError, ValueError): + args[i] = 0 + return version_info(*args) + return BASE_VERSION + +__version__ = get_latest_version() + diff --git a/tests/core/test_version.py b/tests/core/test_version.py new file mode 100644 index 000000000..67b72c3df --- /dev/null +++ b/tests/core/test_version.py @@ -0,0 +1,6 @@ +import core + + +def test_version_working(): + assert hasattr(core, '__version__') + assert core.__version__ >= (3, 0, 0)