forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_board_info.py
286 lines (240 loc) · 9.31 KB
/
build_board_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
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
import json
import os
import subprocess
import sys
import sh
import base64
from datetime import date
from sh.contrib import git
sys.path.append("adabot")
import adabot.github_requests as github
SUPPORTED_PORTS = ["nrf", "atmel-samd"]
BIN = ('bin',)
UF2 = ('uf2',)
BIN_UF2 = ('bin', 'uf2')
HEX = ('hex',)
# Default extensions
extension_by_port = {
"nrf": UF2,
"atmel-samd": UF2,
}
# Per board overrides
extension_by_board = {
# samd
"arduino_mkr1300": BIN,
"arduino_zero": BIN,
"feather_m0_adalogger": BIN_UF2,
"feather_m0_basic": BIN_UF2,
"feather_m0_rfm69": BIN_UF2,
"feather_m0_rfm9x": BIN_UF2,
# nRF52840 dev kits that may not have UF2 bootloaders,
"makerdiary_nrf52840_mdk": HEX,
"makerdiary_nrf52840_mdk_usb_dongle": HEX,
"pca10056": BIN_UF2,
"pca10059": BIN_UF2,
"electronut_labs_blip": HEX
}
aliases_by_board = {
"circuitplayground_express": ["circuitplayground_express_4h", "circuitplayground_express_digikey_pycon2019"],
"gemma_m0": ["gemma_m0_pycon2018"],
"pewpew10": ["pewpew13"]
}
def get_languages():
languages = []
for f in os.scandir("../locale"):
if f.name.endswith(".po"):
languages.append(f.name[:-3])
return languages
def get_board_mapping():
boards = {}
for port in SUPPORTED_PORTS:
board_path = os.path.join("../ports", port, "boards")
for board_path in os.scandir(board_path):
if board_path.is_dir():
board_files = os.listdir(board_path.path)
board_id = board_path.name
extensions = extension_by_port[port]
extensions = extension_by_board.get(board_path.name, extensions)
aliases = aliases_by_board.get(board_path.name, [])
boards[board_id] = {"port": port,
"extensions": extensions,
"download_count": 0,
"aliases": aliases}
for alias in aliases:
boards[alias] = {"port": port,
"extensions": extensions,
"download_count": 0,
"alias": True,
"aliases": []}
return boards
def get_version_info():
version = None
sha = git("rev-parse", "--short", "HEAD").stdout.decode("utf-8")
try:
version = git("describe", "--tags", "--exact-match").stdout.decode("utf-8").strip()
except sh.ErrorReturnCode_128:
# No exact match
pass
if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true":
sha = os.environ["TRAVIS_COMMIT"]
if os.environ["TRAVIS_PULL_REQUEST"] != "false":
sha = os.environ["TRAVIS_PULL_REQUEST_SHA"]
if not version:
version="{}-{}".format(date.today().strftime("%Y%m%d"), sha[:7])
return sha, version
def get_current_info():
response = github.get("/repos/adafruit/circuitpython-org/git/refs/heads/master")
if not response.ok:
print(response.text)
raise RuntimeError("cannot get master sha")
commit_sha = response.json()["object"]["sha"]
response = github.get("/repos/adafruit/circuitpython-org/contents/_data/files.json?ref=" + commit_sha)
if not response.ok:
print(response.text)
raise RuntimeError("cannot get previous files.json")
response = response.json()
git_info = commit_sha, response["sha"]
current_list = json.loads(base64.b64decode(response["content"]).decode("utf-8"))
current_info = {}
for info in current_list:
current_info[info["id"]] = info
return git_info, current_info
def create_pr(changes, updated, git_info):
commit_sha, original_blob_sha = git_info
branch_name = "new_release_" + changes["new_release"]
# Convert the dictionary to a list of boards. Liquid templates only handle arrays.
updated_list = []
all_ids = sorted(updated.keys())
for id in all_ids:
info = updated[id]
info["id"] = id
updated_list.append(info)
updated = json.dumps(updated_list, sort_keys=True, indent=4).encode("utf-8") + b"\n"
print(updated.decode("utf-8"))
pr_title = "Automated website update for release {}".format(changes["new_release"])
boards = ""
if changes["new_boards"]:
boards = "New boards:\n* " + "\n* ".join(changes["new_boards"])
languages = ""
if changes["new_languages"]:
languages = "New languages:\n* " + "\n* ".join(changes["new_languages"])
message = "Automated website update for release {} by AdaBot.\n\n{}\n\n{}\n".format(
changes["new_release"],
boards,
languages
)
create_branch = {
"ref": "refs/heads/" + branch_name,
"sha": commit_sha
}
response = github.post("/repos/adafruit-adabot/circuitpython-org/git/refs", json=create_branch)
if not response.ok and response.json()["message"] != "Reference already exists":
print("unable to create branch")
print(response.text)
return
update_file = {
"message": message,
"content": base64.b64encode(updated).decode("utf-8"),
"sha": original_blob_sha,
"branch": branch_name
}
response = github.put("/repos/adafruit-adabot/circuitpython-org/contents/_data/files.json", json=update_file)
if not response.ok:
print("unable to post new file")
print(response.text)
return
pr_info = {
"title": pr_title,
"head": "adafruit-adabot:" + branch_name,
"base": "master",
"body": message,
"maintainer_can_modify": True
}
response = github.post("/repos/adafruit/circuitpython-org/pulls", json=pr_info)
if not response.ok:
print("unable to create pr")
print(response.text)
return
print(changes)
print(pr_info)
def update_downloads(boards, release):
response = github.get("/repos/adafruit/circuitpython/releases/tags/{}".format(release))
if not response.ok:
print(response.text)
raise RuntimeError("cannot get previous release info")
assets = response.json()["assets"]
for asset in assets:
board_name = asset["name"].split("-")[2]
if board_name not in boards:
continue
boards[board_name]["download_count"] += asset["download_count"]
def print_active_user():
response = github.get("/user")
if response.ok:
print("Logged in as {}".format(response.json()["login"]))
else:
print("Not logged in")
def generate_download_info():
boards = {}
errors = []
new_tag = os.environ["TRAVIS_TAG"]
changes = {
"new_release": new_tag,
"new_boards": [],
"new_languages": []
}
print_active_user()
sha, this_version = get_version_info()
git_info, current_info = get_current_info()
languages = get_languages()
new_stable = "-" not in new_tag
previous_releases = set()
previous_languages = set()
# Delete the release we are replacing
for board in current_info:
info = current_info[board]
for version in info["versions"]:
previous_releases.add(version["version"])
previous_languages.update(version["files"].keys())
if version["stable"] == new_stable:
info["versions"].remove(version)
board_mapping = get_board_mapping()
print(previous_releases)
for release in previous_releases:
update_downloads(board_mapping, release)
for port in SUPPORTED_PORTS:
board_path = os.path.join("../ports", port, "boards")
for board_path in os.scandir(board_path):
if board_path.is_dir():
board_files = os.listdir(board_path.path)
board_id = board_path.name
board_info = board_mapping[board_id]
for alias in [board_id] + board_info["aliases"]:
alias_info = board_mapping[alias]
if alias not in current_info:
changes["new_boards"].append(alias)
current_info[alias] = {"downloads": 0,
"versions": []}
new_version = {
"stable": new_stable,
"version": new_tag,
"files": {}
}
for language in languages:
files = []
new_version["files"][language] = files
for extension in board_info["extensions"]:
files.append("https://github.com/adafruit/circuitpython/releases/download/{tag}/adafruit-circuitpython-{alias}-{language}-{tag}.{extension}".format(tag=new_tag, alias=alias, language=language, extension=extension))
current_info[alias]["downloads"] = alias_info["download_count"]
current_info[alias]["versions"].append(new_version)
changes["new_languages"] = set(languages) - previous_languages
if changes["new_release"]:
create_pr(changes, current_info, git_info)
else:
print("No new release to update")
if __name__ == "__main__":
if "TRAVIS_TAG" in os.environ and os.environ["TRAVIS_TAG"]:
generate_download_info()
else:
print("skipping website update because this isn't a tag")