-
Notifications
You must be signed in to change notification settings - Fork 20
/
version.py
68 lines (55 loc) · 2.28 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
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copied from https://github.com/ohmu/ohmu_common_py version.py version 0.0.1-0-unknown-fa54b44
"""
pglookout - version detection and version.py __version__ generation
Copyright (c) 2015 Ohmu 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, "w") as fp:
fp.write("__version__ = '{}'\n".format(new_ver))
return True
def get_project_version(version_file):
version_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), version_file)
try:
# Load the source file using SourceFileLoader
loader = importlib.machinery.SourceFileLoader("verfile", version_file)
spec = importlib.util.spec_from_loader(loader.name, loader)
module = importlib.util.module_from_spec(spec)
# Execute the module code
loader.exec_module(module)
# Access the version attribute from the module
file_ver = module.__version__
except IOError:
file_ver = None
os.chdir(os.path.dirname(__file__) or ".")
try:
git_out = subprocess.check_output(["git", "describe", "--always"],
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
makefile = os.path.join(os.path.dirname(__file__), "Makefile")
if os.path.exists(makefile):
with open(makefile, "r") as fp:
lines = fp.readlines()
short_ver = [line.split("=", 1)[1].strip() for line in lines if line.startswith("short_ver")][0]
if save_version(short_ver, file_ver, version_file):
return short_ver
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])