From 2549e3e387bd553e871823d9090463ecbeeb0841 Mon Sep 17 00:00:00 2001 From: Ivan Yurchenko Date: Mon, 21 Nov 2022 11:57:58 +0200 Subject: [PATCH] Allow specifying version directly during build Sometimes the Git metadata is not available during build. In this case, we can rely on externally specified version using the `KARAPACE_VERSION` environment variable. --- version.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/version.py b/version.py index 8ceae2897..14362cc56 100644 --- a/version.py +++ b/version.py @@ -25,19 +25,23 @@ def get_project_version(version_file: str) -> str: module = importlib.util.module_from_spec(module_spec) file_ver = getattr(module, "__version__", None) - os.chdir(os.path.dirname(__file__) or ".") - try: - git_out = subprocess.check_output( - ["git", "describe", "--always", "--tags"], stderr=getattr(subprocess, "DEVNULL", None) - ) - except (OSError, subprocess.CalledProcessError): - pass - else: - git_ver = git_out.splitlines()[0].strip().decode("utf-8") - if "." not in git_ver: - git_ver = "0.0.1-0-unknown-{}".format(git_ver) - if save_version(git_ver, file_ver, version_file): - return git_ver + version = os.getenv("KARAPACE_VERSION") + if version is None: + os.chdir(os.path.dirname(__file__) or ".") + try: + git_out = subprocess.check_output( + ["git", "describe", "--always", "--tags"], stderr=getattr(subprocess, "DEVNULL", None) + ) + except (OSError, subprocess.CalledProcessError): + pass + else: + git_ver = git_out.splitlines()[0].strip().decode("utf-8") + if "." not in git_ver: + git_ver = "0.0.1-0-unknown-{}".format(git_ver) + version = git_ver + + if save_version(version, file_ver, version_file): + return version if not file_ver: raise Exception("version not available from git or from file {!r}".format(version_file))