-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion_info.py
276 lines (247 loc) · 10.6 KB
/
version_info.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import re
from configparser import ConfigParser
from constants import PROJECT_FOLDER, RELEASE_LEVEL_DICT
from release_type import ReleaseLevel, ReleaseType, value_from_key
class VersionInfo:
def __init__(
self,
major: int = 0,
minor: int = 0,
bugfix: int = 0,
hotfix: int = 0,
release_level: ReleaseLevel = ReleaseLevel.public,
serial: int = None,
release_type: ReleaseType = None,
short_version: bool = False,
):
self.major = 0 if major is None else int(major)
self.minor = 0 if minor is None else int(minor)
self.bugfix = 0 if bugfix is None else int(bugfix)
self.hotfix = 0 if hotfix is None else int(hotfix)
self.short_version = short_version
self.release_type = release_type
if self.release_type is None:
if self.hotfix == 0:
if self.bugfix == 0:
if self.minor == 0:
if self.major != 0:
self.release_type = ReleaseType.major
else:
self.release_type = ReleaseType.minor
else:
self.release_type = ReleaseType.bugfix
else:
self.release_type = ReleaseType.hotfix
self.serial = (serial and int(serial)) or 0
self.release_level = value_from_key(RELEASE_LEVEL_DICT, release_level) or release_level or ReleaseLevel.public
def __str__(self):
version: str = f'v{self.major}.{self.minor}.{self.bugfix}'
if self.release_type == ReleaseType.hotfix:
version = f'{version}.{self.hotfix}'
elif self.release_level != ReleaseLevel.public:
version = f'{version}{RELEASE_LEVEL_DICT[self.release_level]}{self.serial}'
return version
def increment(self, release_level: ReleaseLevel, release_type: ReleaseType = None):
sequel: bool = False
if release_type == self.release_type and self.release_level == ReleaseLevel.public:
sequel = True
if self.release_type != release_type or sequel:
if release_type == ReleaseType.hotfix:
self.hotfix += 1
else:
self.hotfix = 0
if release_type == ReleaseType.bugfix:
self.bugfix += 1
else:
self.bugfix = 0
if release_type == ReleaseType.minor:
self.minor += 1
else:
self.minor = 0
if release_type == ReleaseType.major:
self.major += 1
self.serial = None
self.release_type = release_type
if release_level != ReleaseLevel.public:
self.increase_serial(release_level)
elif release_level == ReleaseLevel.public:
self.serial = 0
self.release_level = release_level
self.release_type = release_type
def increase_serial(self, release_level: ReleaseLevel):
if self.serial is not None and self.release_level != release_level:
self.serial = 0
else:
if self.serial is not None:
self.serial += 1
else:
self.serial = 0
self.release_level = release_level
def convert_to_godot_format(self):
return repr(str(self).lstrip("v")).replace("'", '"')
@classmethod
def start_version(cls):
return cls(0, 1, 0)
@classmethod
def load_version(cls, version: str):
pattern: re.Pattern = re.compile(r"(\d)\.(\d)\.?(\d)?\.?(\d)?\.?([a-z]{1,2})?(\d{1,3})?")
match: re.Match = pattern.match(version.replace('"', ''))
if match:
return cls(*match.groups())
else:
return cls.start_version()
@classmethod
def check_version(cls, version: str):
pattern: re.Pattern = re.compile(r"(\d)\.(\d)\.?(\d)?\.?(\d)?\.?([a-z]{1,2})?(\d{1,3})?")
match: re.Match = pattern.match(version.replace('"', ''))
return bool(match)
def set_version(new_version: VersionInfo) -> None:
config = ConfigParser()
with open(list(PROJECT_FOLDER.glob("export_presets.cfg"))[0], 'r') as exports_config:
config.read_file(exports_config)
for section_name, section in config.items():
for key, value in section.items():
if key.endswith('version'):
config.set(section_name, key, new_version.convert_to_godot_format())
config_file = open(list(PROJECT_FOLDER.glob("export_presets.cfg"))[0], "w")
config.write(config_file)
config_file.close()
with open(list(PROJECT_FOLDER.glob("version.txt"))[0], 'w') as version_file:
version_file.write(str(new_version))
def get_version() -> VersionInfo:
try:
version_file = open(list(PROJECT_FOLDER.glob("version.txt"))[0], 'r')
except IndexError:
version_file = open(PROJECT_FOLDER/"version.txt", "w+")
else:
if not VersionInfo.check_version(version_file.read()):
print("Falling back to export presets")
config = ConfigParser()
with open(list(PROJECT_FOLDER.glob("export_presets.cfg"))[0], 'r') as exports_config:
config.read_file(exports_config)
version: VersionInfo = VersionInfo.start_version()
for section_name, section in config.items():
for key, value in section.items():
if key.endswith('version'):
version = VersionInfo.load_version(value)
return version
else:
return VersionInfo.load_version(version_file.read())
if __name__ == '__main__': # Test Script
index = 0
version_info = VersionInfo(1, 0, 0, 0, ReleaseLevel.public, None, ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.bugfix)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.bugfix)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.hotfix)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.beta, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.release_candidate, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.major)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.hotfix)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.public, release_type=ReleaseType.hotfix)
print(index, version_info)
index += 1
version_info.increment(ReleaseLevel.alpha, release_type=ReleaseType.minor)
print(index, version_info)
index += 1
_version = version_info.convert_to_godot_format()
print(_version)
_pattern: re.Pattern = re.compile(r"(\d)\.(\d)\.?(\d)?\.?(\d)?\.?([a-z]{1,2})?(\d{1,3})?")
_match: re.Match = _pattern.match(_version.replace('"', ''))
print(index, VersionInfo(*_match.groups()))