forked from aiven/aiven-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
40 lines (32 loc) · 1.14 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
"""
automatically maintains the latest git tag + revision info in a python file
"""
import imp
import os
import subprocess
def get_project_version(version_file):
version_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), version_file)
try:
module = imp.load_source("verfile", version_file)
file_ver = module.__version__
except:
file_ver = None
try:
proc = subprocess.Popen(["git", "describe", "--always"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
if stdout:
git_ver = stdout.splitlines()[0].strip().decode("utf-8")
if git_ver and ((git_ver != file_ver) or not file_ver):
open(version_file, "w").write("__version__ = '%s'\n" % git_ver)
return git_ver
except OSError:
pass
if not file_ver:
raise Exception("version not available from git or from file %r"
% version_file)
return file_ver
if __name__ == "__main__":
import sys
get_project_version(sys.argv[1])