-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel-tea-leaves
executable file
·195 lines (147 loc) · 5.13 KB
/
kernel-tea-leaves
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
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) 2023 Jani Nikula <[email protected]>
import argparse
import datetime
import re
import jinja2
def _version_plus(major, minor):
minor += 1
if minor >= 20:
major += 1
minor = 0
return major, minor
def _version_minus(major, minor):
minor -= 1
if minor < 0:
major -= 1
minor = 19
return major, minor
class Tag:
def __init__(self, major=None, minor=None, rc=None,
date_start=None, date_end=None, prediction=False):
self._major = major
self._minor = minor
self._rc = rc
self._date_start = date_start
self._date_end = date_end if date_end else date_start
self._prediction = prediction
@property
def tag(self):
rc = f'-rc{self._rc}' if self._rc is not None else ''
return f'v{self._major}.{self._minor}{rc}'
@property
def development_cycle(self):
return f'v{self._major}.{self._minor}'
@property
def next_development_cycle(self):
major, minor = _version_plus(self._major, self._minor)
return f'v{major}.{minor}'
@property
def date(self):
fmt = '%Y-%m-%d'
start = self._date_start.strftime(fmt)
if self._date_start == self._date_end:
end = ''
else:
end = '/' + self._date_end.strftime(fmt)
return f'{start}{end}'
def __repr__(self):
return f'{self.tag} {self.date}'
def is_release(self):
return self._rc is None
def is_rc(self, rc):
return self._rc == rc
def previous(self):
if self.is_release():
return None # can't go back
if self._rc == 1:
major, minor = _version_minus(self._major, self._minor)
rc = None
weeks = (-2, -2)
else:
major = self._major
minor = self._minor
rc = self._rc - 1
weeks = (-1, -1)
date_start = self._date_start + datetime.timedelta(weeks=weeks[0])
date_end = self._date_end + datetime.timedelta(weeks=weeks[1])
return Tag(major=major, minor=minor, rc=rc, date_start=date_start,
date_end=date_end, prediction=False)
def next(self):
if self.is_release():
major, minor = _version_plus(self._major, self._minor)
rc = 1
weeks = (2, 2)
elif self._rc < 7:
major = self._major
minor = self._minor
rc = self._rc + 1
weeks = (1, 1)
else: # rc7+ -> release
major = self._major
minor = self._minor
rc = None
if self._rc == 7:
# Don't predict -rc8, predict release with inaccuracy
weeks = (1, 2)
else:
weeks = (1, 1)
date_start = self._date_start + datetime.timedelta(weeks=weeks[0])
date_end = self._date_end + datetime.timedelta(weeks=weeks[1])
return Tag(major=major, minor=minor, rc=rc, date_start=date_start,
date_end=date_end, prediction=True)
def _tag_type(s):
mo = re.match(r'^v?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)(-rc(?P<rc>[0-9]+))?$', s)
if mo is None:
raise argparse.ArgumentTypeError(f'"{s}" is not a valid tag format')
def to_int(v): return int(v) if v is not None else None
return {k: to_int(v) for k, v in mo.groupdict().items()}
def _date_type(s):
try:
d = datetime.datetime.fromisoformat(s)
except ValueError as e:
raise argparse.ArgumentTypeError(e)
# If it's not a Sunday, round to nearest Sunday
if d.weekday() != 6:
if d.weekday() < 3:
days = -(d.weekday() + 1)
else:
days = 6 - d.weekday()
d += datetime.timedelta(days=days)
return d
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--tag', type=_tag_type, required=True,
help='Tag of the format v<major>.<minor>[-rc<n>], e.g. v6.7 or v6.7-rc5')
parser.add_argument('--date', type=_date_type, required=True,
help='Date of the tag in ISO format')
parser.add_argument('--releases', type=int, default=3,
help='Number of development cycles to predict')
parser.add_argument('--template', default='index.template.rst',
help='Template to use')
args = parser.parse_args()
tag_params = {
'date_start': args.date
} | args.tag
reference_tag = Tag(**tag_params)
tags = [reference_tag]
# backtrack to previous release
t = reference_tag.previous()
while t is not None:
tags.insert(0, t)
t = t.previous()
# predict future tags
t = reference_tag
release = 0
while release < args.releases:
t = t.next()
if t.is_release():
release += 1
tags.append(t)
env = jinja2.Environment(loader=jinja2.FileSystemLoader('.'))
template = env.get_template(args.template)
output = template.render(reference_tag=reference_tag, tags=tags)
print(output)
if __name__ == '__main__':
main()