-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
327 lines (274 loc) · 12.7 KB
/
manage.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import argparse
import json
from datetime import datetime, timedelta
import sys
import re
def load_json(file_path):
with open(file_path, 'r') as f:
return json.load(f)
def save_json(data, file_path):
with open(file_path, 'w') as f:
json.dump(data, f, indent=2)
def validate_version(version):
if not re.match(r'^stable2[45][01][0-9](-[0-9]*)?$', version):
raise ValueError("Invalid version format. Expected 'stableYYMM' or 'stableYYMM-X' for patches.")
def validate_date(date_str):
try:
return datetime.strptime(date_str, '%Y-%m-%d').strftime('%Y-%m-%d')
except ValueError:
raise ValueError("Invalid date format. Expected 'YYYY-MM-DD'.")
def find_release(data, version):
release_version = version.split('-')[0]
for project in data.values():
for release in project['releases']:
if release['name'] == release_version:
return project, release
return None, None
def update_patch(release, patch_number, date, field):
if 'patches' not in release:
release['patches'] = []
for patch in release['patches']:
if patch['name'].endswith(f'-{patch_number}'):
if field == 'cutoff':
patch['cutoff'] = date
patch['state'] = 'testing'
elif field == 'publish':
patch['publish'] = {'when': date, 'tag': f'polkadot-{patch["name"]}'}
patch['state'] = 'released'
# Also set the cutoff if it was not set
if isinstance(patch['cutoff'], dict) and 'estimated' in patch['cutoff']:
patch['cutoff'] = date
elif field == 'plan':
patch['cutoff'] = {'estimated': date}
patch['publish'] = {'estimated': date}
patch['state'] = 'planned'
return True
# If the patch doesn't exist, create it
new_patch = {
'name': f'{release["name"]}-{patch_number}',
'cutoff': date if field == 'cutoff' else {'estimated': date},
'publish': {'when': date, 'tag': f'polkadot-{release["name"]}-{patch_number}'} if field == 'publish' else {'estimated': date},
'state': 'testing' if field == 'cutoff' else ('released' if field == 'publish' else 'planned')
}
release['patches'].append(new_patch)
return True
def get_nth_monday(date):
return (date.day - 1) // 7 + 1
def next_nth_monday(date, n):
next_month = date.replace(day=1) + timedelta(days=32)
next_month = next_month.replace(day=1)
while next_month.weekday() != 0:
next_month += timedelta(days=1)
next_month += timedelta(weeks=n-1)
return next_month
def create_planned_patches(release, start_date, num_patches=13):
if 'patches' not in release:
release['patches'] = []
existing_patch_numbers = set(int(patch['name'].split('-')[-1]) for patch in release['patches'] if '-' in patch['name'])
cutoff_date = datetime.strptime(start_date, '%Y-%m-%d')
if cutoff_date.weekday() != 0:
raise ValueError("Start date must be a Monday")
nth_monday = get_nth_monday(cutoff_date)
for i in range(1, num_patches + 1):
if i not in existing_patch_numbers:
publish_date = cutoff_date + timedelta(days=3) # Thursday of the same week
cutoff_str = cutoff_date.strftime('%Y-%m-%d')
publish_str = publish_date.strftime('%Y-%m-%d')
new_patch = {
'name': f'{release["name"]}-{i}',
'cutoff': {'estimated': cutoff_str},
'publish': {'estimated': publish_str},
'state': 'planned'
}
release['patches'].append(new_patch)
cutoff_date = next_nth_monday(cutoff_date, nth_monday)
# Sort patches by their number to maintain order
release['patches'].sort(key=lambda x: int(x['name'].split('-')[-1]))
def update_release(data, version, date, field):
project, release = find_release(data, version)
if not release and field == 'plan':
# Create a new release if it doesn't exist
project = next(iter(data.values())) # Get the first (and only) project
cutoff_date = datetime.strptime(date, '%Y-%m-%d')
publish_date = cutoff_date + timedelta(days=45) # 1.5 months after cutoff
# If the publish date is on a weekend, move it to the next monday
if publish_date.weekday() >= 5:
publish_date += timedelta(days=(7 - publish_date.weekday()))
new_release = {
'name': version,
'cutoff': {'estimated': date},
'publish': {'estimated': publish_date.strftime('%Y-%m-%d')},
'state': 'planned',
'endOfLife': {'estimated': (publish_date + timedelta(days=365)).strftime('%Y-%m-%d')}
}
project['releases'].append(new_release)
return True
if not release:
return False
if '-' in version: # It's a patch
return update_patch(release, version.split('-')[1], date, field)
else: # It's a release
if field == 'cutoff':
release['cutoff'] = { 'when': date, 'tag': f'polkadot-{version}-rc1' }
release['state'] = 'drafted'
elif field == 'publish':
release['publish'] = {'when': date, 'tag': f'polkadot-{version}'}
release['state'] = 'released'
elif field == 'plan':
cutoff_date = datetime.strptime(date, '%Y-%m-%d')
publish_date = cutoff_date + timedelta(days=45) # 1.5 months after cutoff
release['cutoff'] = {'estimated': date}
release['publish'] = {'estimated': publish_date.strftime('%Y-%m-%d')}
release['state'] = 'planned'
return True
def deprecate_release(data, version, date, use_instead):
project, release = find_release(data, version)
if not release:
return False
release['state'] = {
'deprecated': {
'since': date,
'useInstead': use_instead
}
}
return True
def backfill_patches(data, version=None, start_date=None):
releases_updated = False
for project in data.values():
for release in project['releases']:
if release['name'].startswith('stable') and (version is None or release['name'] == version):
if start_date:
create_planned_patches(release, start_date)
releases_updated = True
elif 'publish' in release and isinstance(release['publish'], dict):
if 'when' in release['publish']:
release_date = release['publish']['when']
elif 'estimated' in release['publish']:
release_date = release['publish']['estimated']
else:
continue # Skip if no valid publish date
# Find the next Monday after the release date
release_date = datetime.strptime(release_date, '%Y-%m-%d')
while release_date.weekday() != 0:
release_date += timedelta(days=1)
create_planned_patches(release, release_date.strftime('%Y-%m-%d'))
releases_updated = True
else:
continue # Skip if no valid publish field
if version: # If a specific version was requested, we're done after processing it
return True
return releases_updated
def remove_planned_patches(data, version):
project, release = find_release(data, version)
if not release:
return False
if 'patches' not in release:
return False
release['patches'] = [patch for patch in release['patches']
if not (patch['state'] == 'planned' and
isinstance(patch['cutoff'], dict) and
'estimated' in patch['cutoff'])]
return True
def handle_release_command(args, data):
validate_version(args.version)
date = validate_date(args.date)
if update_release(data, args.version, date, args.field):
save_json(data, args.file)
print(f"Successfully updated {args.field} for {args.version}")
else:
print(f"Release or patch {args.version} not found")
def handle_deprecate_command(args, data):
validate_version(args.version)
date = validate_date(args.date)
validate_version(args.use_instead)
if deprecate_release(data, args.version, date, args.use_instead):
save_json(data, args.file)
print(f"Successfully deprecated {args.version}")
else:
print(f"Release {args.version} not found")
def handle_backfill_patches_command(args, data):
if args.version:
validate_version(args.version)
start_date = None
if args.start_date:
start_date = validate_date(args.start_date)
start_date_obj = datetime.strptime(start_date, '%Y-%m-%d')
if start_date_obj.weekday() != 0:
print("Error: Start date must be a Monday")
return
if args.version:
if backfill_patches(data, args.version, start_date):
save_json(data, args.file)
print(f"Successfully backfilled patches for {args.version}")
else:
print(f"Failed to backfill patches for {args.version}. Release may not exist or have a valid publish date.")
else:
if backfill_patches(data, start_date=start_date):
save_json(data, args.file)
print("Successfully backfilled patches for all applicable stable releases")
else:
print("No releases were updated. All releases may already have patches or lack valid publish dates.")
def handle_remove_command(args, data):
validate_version(args.version)
project, release = find_release(data, args.version)
if not release:
print(f"Release {args.version} not found")
return
project['releases'].remove(release)
save_json(data, args.file)
print(f"Successfully removed release {args.version}")
def handle_remove_patches_command(args, data):
validate_version(args.version)
if remove_planned_patches(data, args.version):
save_json(data, args.file)
print(f"Successfully removed planned patches for {args.version}")
else:
print(f"No planned patches found or release {args.version} not found")
def main():
parser = argparse.ArgumentParser(description="Manage releases-v1.json file")
subparsers = parser.add_subparsers(dest='action', required=True)
# Release parser
release_parser = subparsers.add_parser('release')
release_parser.add_argument('field', choices=['cutoff', 'publish', 'plan'])
release_parser.add_argument('version', help="Release version (e.g., stable2401 or stable2401-1 for patches)")
release_parser.add_argument('date', help="Date in YYYY-MM-DD format")
# Release remove parser
remove_parser = subparsers.add_parser('remove')
remove_parser.add_argument('version', help="Release version to remove")
# Deprecate parser
deprecate_parser = subparsers.add_parser('deprecate')
deprecate_parser.add_argument('version', help="Release version to deprecate")
deprecate_parser.add_argument('date', help="Deprecation date in YYYY-MM-DD format")
deprecate_parser.add_argument('use_instead', help="Version to use instead")
# Backfill patches parser
backfill_parser = subparsers.add_parser('backfill-patches')
backfill_parser.add_argument('version', nargs='?', help="Specific stable version to backfill (e.g., stable2401)")
backfill_parser.add_argument('--start-date', help="Start date for the first patch cutoff (must be a Monday, format: YYYY-MM-DD)")
# Remove patches parser
remove_patches_parser = subparsers.add_parser('remove-patches')
remove_patches_parser.add_argument('version', help="Release version to remove planned patches from")
parser.add_argument('--file', default='releases-v1.json', help="Path to the JSON file")
args = parser.parse_args()
try:
data = load_json(args.file)
if args.action == 'release':
handle_release_command(args, data)
elif args.action == 'deprecate':
handle_deprecate_command(args, data)
elif args.action == 'backfill-patches':
handle_backfill_patches_command(args, data)
elif args.action == 'remove':
handle_remove_command(args, data)
elif args.action == 'remove-patches':
handle_remove_patches_command(args, data)
except ValueError as e:
print(f"Error: {str(e)}")
sys.exit(1)
except FileNotFoundError:
print(f"Error: File '{args.file}' not found")
sys.exit(1)
except json.JSONDecodeError:
print(f"Error: Invalid JSON in '{args.file}'")
sys.exit(1)
if __name__ == "__main__":
main()