-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-badges.py
81 lines (62 loc) · 2.29 KB
/
update-badges.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
"""
This script updates the SVG files in the badges folder.
Just run it in its stock configuration from the root folder:
python scripts/update-badges.py
"""
import json
import os
import requests
import re
from datetime import datetime
releases = json.load(open("releases-v1.json"))
def download(url, filename):
print(f"Downloading {url}")
response = requests.get(url)
if response.status_code == 200:
with open(filename, 'wb') as file:
file.write(response.content)
print(f"Downloaded {filename}")
else:
raise Exception(f"Failed to download SVG. Status code: {response.status_code}")
def update_latest():
recommended = releases["Polkadot SDK"]["recommended"]
latest = recommended['release']
if 'patch' in recommended:
latest += f"_{recommended['patch']}"
latest_url = f"https://img.shields.io/badge/Latest%20Release-{latest}-green"
latest_name = "badges/polkadot-sdk-latest.svg"
download(latest_url, latest_name)
def find_next_unreleased_release(releases):
for release in releases:
if release['state'] in ['planned', 'staging']:
return release
return None
def format_date(date_info):
if isinstance(date_info, dict):
if 'estimated' in date_info:
date_str = date_info['estimated']
elif 'when' in date_info:
date_str = date_info['when']
else:
return "Unknown"
else:
date_str = date_info
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
return date_obj.strftime("%Y/%m/%d")
def update_next():
releases = json.load(open("releases-v1.json"))
sdk_releases = releases["Polkadot SDK"]["releases"]
next_release = find_next_unreleased_release(sdk_releases)
if next_release:
publish_info = next_release['publish']
date = format_date(publish_info)
# extract the 'stableYYMM' part
stable = re.search(r'(stable\d+)', next_release['name']).group(1)
next_url = f"https://img.shields.io/badge/Next%20Release%20%28{stable}%29-{date}-orange"
next_name = "badges/polkadot-sdk-next.svg"
download(next_url, next_name)
else:
print("No upcoming unreleased version found.")
if __name__ == "__main__":
update_latest()
update_next()