-
Notifications
You must be signed in to change notification settings - Fork 2
/
version.py
52 lines (40 loc) · 1.58 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
import os
import shlex
import subprocess
def _get_git_version(path):
# Check if we are a git clone.
if not os.path.isdir(os.path.join(path, ".git")):
return None
# Base the version off the "git describe --tags".
command = shlex.split(f"git -C {path} describe --tags --long")
try:
describe = subprocess.check_output(command, universal_newlines=True).strip()
except subprocess.CalledProcessError:
# Most likely this was a clone without tags
return None
version, commit_since, git_hash = describe.rsplit("-", 3)
# If we are not a release, we are a "post"; locally we can also be a
# "dev", but as it is locally, it doesn't really matter. The hash will
# change anyway, showing it is a different version.
if commit_since != "0":
return f"{version}.post{commit_since}+{git_hash}"
return version
def get_version():
path = os.path.dirname(os.path.realpath(__file__))
version_file = os.path.join(path, "VERSION")
# Load the version from disk if possible.
if os.path.isfile(version_file):
with open(version_file, "r") as f:
disk_version = f.read()
else:
disk_version = None
# Check if we have a git version.
git_version = _get_git_version(path)
# If we don't have a git version, return the disk version.
if not git_version:
return disk_version
# IF the git version and disk version differ, write a new disk version.
if git_version != disk_version:
with open(version_file, "w") as f:
f.write(git_version)
return git_version