-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_version.py
95 lines (76 loc) · 2.81 KB
/
set_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import re
import subprocess # nosec
from shutil import copy
_PKG_DIR = os.path.dirname(os.path.realpath(__file__))
def _version_from_git_describe():
"""
Read the version from ``git describe``. It returns the latest tag with an
optional suffix if the current directory is not exactly on the tag.
Example::
$ git describe --always
v2.3.2-346-g164a52c075c8
The tag prefix (``v``) and the git commit sha1 (``-g164a52c075c8``) are
removed if present.
If the current directory is not exactly on the tag, a ``.devN`` suffix is
appended where N is the number of commits made after the last tag.
Example::
'>>> _version_from_git_describe()'
'2.3.2.dev346'
"""
if not os.path.isdir(os.path.join(_PKG_DIR, ".git")): # noqa: E501
raise ValueError("not in git repo")
process = subprocess.Popen( # nosec
["git", "describe", "--tags"],
cwd=_PKG_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = process.communicate()
if process.returncode == 0:
tag = out.decode().strip()
match = re.match("^v?(.+?)-(\\d+)-g[a-f0-9]+$", tag)
if match:
# remove the 'v' prefix and add a '.devN' suffix
return "%s.dev%s" % (match.group(1), match.group(2))
else:
# just remove the 'v' prefix
return re.sub("^v", "", tag)
else:
raise subprocess.CalledProcessError(process.returncode, err)
def _version():
version_file = os.path.join(_PKG_DIR, "VERSION")
try:
tag = _version_from_git_describe()
# successfully read the tag from git, write it in VERSION for
# installation and/or archive generation.
with open(version_file, "w") as fdesc:
fdesc.write(tag)
return tag
except Exception:
# failed to read the tag from git, try to read it from a VERSION file
try:
with open(version_file, "r") as fdsec:
tag = fdsec.read()
return tag
except Exception:
# Rely on git archive "export-subst" git attribute.
# See 'man gitattributes' for more details.
git_archive_id = "$Format:%h %d$"
sha1 = git_archive_id.strip().split()[0]
match = re.search("tag:(\\S+)", git_archive_id)
if match:
return "git-archive.dev" + match.group(1)
elif sha1:
return "git-archive.dev" + sha1
else:
return "unknown.version"
finally:
try:
copy(
version_file, os.path.join(_PKG_DIR, "spectacles", "webapp", "VERSION")
)
except FileNotFoundError:
pass
VERSION = __version__ = _version()
VERSION_MAIN = re.search(r"[0-9.]+", VERSION).group()