forked from ubisoft/mixer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinject_version.py
95 lines (81 loc) · 3.62 KB
/
inject_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
# MIT License
#
# Copyright (c) 2020 Ubisoft
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from datetime import datetime, timezone
import os
import re
import subprocess
from typing import Tuple
def get_version():
cp = subprocess.run(
["git", "describe", "--tags", "--dirty", "--match=v*"],
stdout=subprocess.PIPE,
check=True,
)
version = str(cp.stdout, encoding="utf8").strip()
return version
def parse(version) -> Tuple[Tuple[int], str]:
"""Parse version string like "v1.0.4-14-g241472-dirty" into ((0,14,0), "-g241472-dirty")"""
# similar regexp in gitlab .yml files
# tested with https://regoio.herokuapp.com/
# vMAJOR.MINOR.PATCH-PRERELEASE+BUILD as in https://semver.org/
re_string = r"^v([0-9]+)\.([0-9]+)\.([0-9]+)((?:\-[0-9A-Za-z-]+)?(?:\+[0-9A-Za-z-]+)?)?$"
match = re.match(re_string, version)
if not match:
raise RuntimeError('invalid version string "{version}"')
groups = match.groups()
# ([0-9]+)\.([0-9]+)\.([0-9]+)
# e.g. 0.15.15
version = tuple((int(s) for s in groups[0:3]))
# ((?:\-[0-9A-Za-z-]+)?(?:\+[0-9A-Za-z-]+)?)?
# e.g. "" or "-xxx" or "+yyy" or "-xxx+yyy"
prerelease_build = "" if len(groups) < 4 else groups[3]
return version, prerelease_build
def main():
version = get_version()
version_numbers, suffix = parse(version)
version_strings = [str(i) for i in version_numbers]
version_string = ".".join(version_strings)
init_file = os.path.join("mixer", "__init__.py")
new_init_file_str = ""
done = False
comment = " # Generated by inject_version.py"
with open(init_file, "r") as fp:
for line in fp.readlines():
if not done:
if line.startswith("__version__ = "):
new_init_file_str += f'__version__ = "v{version_string}"{comment}\n'
elif line.startswith("display_version = "):
new_init_file_str += f'display_version = "{version_string}{suffix}"{comment}\n'
elif line.startswith("version_date = "):
date = datetime.now(timezone.utc).strftime("%Y-%m-%d:%H:%M:%S %Z")
new_init_file_str += f'version_date = "{date}"{comment}\n'
elif line.startswith(' "version": ('):
new_init_file_str += f' "version": {str(tuple(version_numbers))},{comment}\n'
done = True
else:
new_init_file_str += f"{line}"
else:
new_init_file_str += f"{line}"
with open(init_file, "w") as fp:
fp.write(new_init_file_str)
if __name__ == "__main__":
main()