forked from Aiven-Open/karapace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.py
55 lines (43 loc) · 1.7 KB
/
version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
karapace - version
Copyright (c) 2019 Aiven Ltd
See LICENSE for details
"""
import importlib.util
import os
import subprocess
def save_version(new_ver, old_ver, version_file):
if not new_ver:
return False
version_file = os.path.join(os.path.dirname(__file__), version_file)
if not old_ver or new_ver != old_ver:
with open(version_file, mode="w", encoding="utf8") as fp:
fp.write('"""{}"""\n__version__ = "{}"\n'.format(__doc__, new_ver))
return True
def get_project_version(version_file: str) -> str:
version_file_full_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), version_file)
module_spec = importlib.util.spec_from_file_location("verfile", version_file_full_path)
module = importlib.util.module_from_spec(module_spec)
file_ver = getattr(module, "__version__", None)
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))
return file_ver
if __name__ == "__main__":
import sys
get_project_version(sys.argv[1])