2018-11-28 19:58:44 -05:00
|
|
|
#!/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
|
|
|
|
|
2020-03-30 22:05:31 -04:00
|
|
|
SUPPORTED_PORTS = ["nrf", "atmel-samd", "stm", "cxd56", "mimxrt10xx", "litex"]
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
BIN = ('bin',)
|
|
|
|
UF2 = ('uf2',)
|
|
|
|
BIN_UF2 = ('bin', 'uf2')
|
|
|
|
HEX = ('hex',)
|
2020-01-08 23:32:45 -05:00
|
|
|
HEX_UF2 = ('hex', 'uf2')
|
2019-10-11 06:00:58 -04:00
|
|
|
SPK = ('spk',)
|
2020-03-31 02:08:14 -04:00
|
|
|
DFU = ('dfu',)
|
|
|
|
BIN_DFU = ('bin', 'dfu')
|
2018-11-28 19:58:44 -05:00
|
|
|
|
2020-02-28 14:57:32 -05:00
|
|
|
# Example:
|
2020-02-28 16:23:49 -05:00
|
|
|
# https://downloads.circuitpython.org/bin/trinket_m0/en_US/adafruit-circuitpython-trinket_m0-en_US-5.0.0-rc.0.uf2
|
|
|
|
DOWNLOAD_BASE_URL = "https://downloads.circuitpython.org/bin"
|
2020-02-28 14:57:32 -05:00
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
# Default extensions
|
|
|
|
extension_by_port = {
|
2018-12-11 14:51:07 -05:00
|
|
|
"nrf": UF2,
|
2018-11-28 19:58:44 -05:00
|
|
|
"atmel-samd": UF2,
|
2020-03-11 19:00:49 -04:00
|
|
|
"stm": BIN,
|
2019-10-11 06:00:58 -04:00
|
|
|
"cxd56": SPK,
|
2020-01-08 23:32:45 -05:00
|
|
|
"mimxrt10xx": HEX_UF2,
|
2020-03-31 02:08:14 -04:00
|
|
|
"litex": DFU,
|
2018-11-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Per board overrides
|
|
|
|
extension_by_board = {
|
2018-12-11 14:51:07 -05:00
|
|
|
# samd
|
2020-02-28 21:18:48 -05:00
|
|
|
"arduino_mkr1300": BIN_UF2,
|
|
|
|
"arduino_mkrzero": BIN_UF2,
|
|
|
|
"arduino_nano_33_iot": BIN_UF2,
|
|
|
|
"arduino_zero": BIN_UF2,
|
2018-11-28 19:58:44 -05:00
|
|
|
"feather_m0_adalogger": BIN_UF2,
|
|
|
|
"feather_m0_basic": BIN_UF2,
|
2019-03-05 13:11:10 -05:00
|
|
|
"feather_m0_rfm69": BIN_UF2,
|
|
|
|
"feather_m0_rfm9x": BIN_UF2,
|
2020-04-23 01:39:02 -04:00
|
|
|
"uchip": BIN_UF2,
|
2018-12-11 14:51:07 -05:00
|
|
|
|
|
|
|
# nRF52840 dev kits that may not have UF2 bootloaders,
|
2018-12-07 20:37:25 -05:00
|
|
|
"makerdiary_nrf52840_mdk": HEX,
|
2019-01-15 04:37:22 -05:00
|
|
|
"makerdiary_nrf52840_mdk_usb_dongle": HEX,
|
2018-12-11 14:51:07 -05:00
|
|
|
"pca10056": BIN_UF2,
|
2019-07-08 04:46:31 -04:00
|
|
|
"pca10059": BIN_UF2,
|
2019-09-04 18:40:44 -04:00
|
|
|
"electronut_labs_blip": HEX
|
2018-11-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
|
2019-04-11 14:25:04 -04:00
|
|
|
aliases_by_board = {
|
|
|
|
"circuitplayground_express": ["circuitplayground_express_4h", "circuitplayground_express_digikey_pycon2019"],
|
2019-11-19 17:53:58 -05:00
|
|
|
"pybadge": ["edgebadge"],
|
2019-12-11 14:41:10 -05:00
|
|
|
"pyportal": ["pyportal_pynt"],
|
2019-04-11 14:25:04 -04:00
|
|
|
"gemma_m0": ["gemma_m0_pycon2018"],
|
|
|
|
"pewpew10": ["pewpew13"]
|
|
|
|
}
|
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
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)
|
2019-04-11 14:25:04 -04:00
|
|
|
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": []}
|
2018-11-28 19:58:44 -05:00
|
|
|
return boards
|
|
|
|
|
|
|
|
def get_version_info():
|
|
|
|
version = None
|
|
|
|
sha = git("rev-parse", "--short", "HEAD").stdout.decode("utf-8")
|
|
|
|
try:
|
2018-12-10 13:13:09 -05:00
|
|
|
version = git("describe", "--tags", "--exact-match").stdout.decode("utf-8").strip()
|
2018-11-28 19:58:44 -05:00
|
|
|
except sh.ErrorReturnCode_128:
|
|
|
|
# No exact match
|
|
|
|
pass
|
|
|
|
|
2019-08-15 03:07:29 -04:00
|
|
|
if "GITHUB_SHA" in os.environ:
|
|
|
|
sha = os.environ["GITHUB_SHA"]
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
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"]
|
2019-03-11 20:07:15 -04:00
|
|
|
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
|
2018-11-28 19:58:44 -05:00
|
|
|
|
2019-09-04 19:50:13 -04:00
|
|
|
def create_pr(changes, updated, git_info, user):
|
2018-11-28 19:58:44 -05:00
|
|
|
commit_sha, original_blob_sha = git_info
|
|
|
|
branch_name = "new_release_" + changes["new_release"]
|
|
|
|
|
2019-03-11 20:07:15 -04:00
|
|
|
# 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"
|
2019-08-15 03:07:29 -04:00
|
|
|
#print(updated.decode("utf-8"))
|
2018-11-28 19:58:44 -05:00
|
|
|
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"])
|
2019-09-04 19:50:13 -04:00
|
|
|
message = "Automated website update for release {} by Blinka.\n\n{}\n\n{}\n".format(
|
2018-11-28 19:58:44 -05:00
|
|
|
changes["new_release"],
|
|
|
|
boards,
|
|
|
|
languages
|
|
|
|
)
|
|
|
|
|
|
|
|
create_branch = {
|
|
|
|
"ref": "refs/heads/" + branch_name,
|
|
|
|
"sha": commit_sha
|
|
|
|
}
|
2019-09-04 19:50:13 -04:00
|
|
|
response = github.post("/repos/{}/circuitpython-org/git/refs".format(user), json=create_branch)
|
2018-11-28 19:58:44 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-04 19:50:13 -04:00
|
|
|
response = github.put("/repos/{}/circuitpython-org/contents/_data/files.json".format(user), json=update_file)
|
2018-11-28 19:58:44 -05:00
|
|
|
if not response.ok:
|
|
|
|
print("unable to post new file")
|
|
|
|
print(response.text)
|
|
|
|
return
|
|
|
|
pr_info = {
|
|
|
|
"title": pr_title,
|
2019-09-04 19:50:13 -04:00
|
|
|
"head": user + ":" + branch_name,
|
2018-11-28 19:58:44 -05:00
|
|
|
"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:
|
2019-01-24 13:22:14 -05:00
|
|
|
board_name = asset["name"].split("-")[2]
|
|
|
|
if board_name not in boards:
|
|
|
|
continue
|
|
|
|
boards[board_name]["download_count"] += asset["download_count"]
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
def print_active_user():
|
|
|
|
response = github.get("/user")
|
|
|
|
if response.ok:
|
2019-09-04 19:50:13 -04:00
|
|
|
user = response.json()["login"]
|
|
|
|
print("Logged in as {}".format(user))
|
|
|
|
return user
|
2018-11-28 19:58:44 -05:00
|
|
|
else:
|
|
|
|
print("Not logged in")
|
2019-09-04 19:50:13 -04:00
|
|
|
return None
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
def generate_download_info():
|
|
|
|
boards = {}
|
|
|
|
errors = []
|
|
|
|
|
2019-08-15 03:07:29 -04:00
|
|
|
new_tag = os.environ["RELEASE_TAG"]
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
changes = {
|
|
|
|
"new_release": new_tag,
|
|
|
|
"new_boards": [],
|
|
|
|
"new_languages": []
|
|
|
|
}
|
|
|
|
|
2019-09-04 19:50:13 -04:00
|
|
|
user = print_active_user()
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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():
|
2018-12-10 16:17:05 -05:00
|
|
|
board_files = os.listdir(board_path.path)
|
2018-11-28 19:58:44 -05:00
|
|
|
board_id = board_path.name
|
|
|
|
board_info = board_mapping[board_id]
|
|
|
|
|
2019-04-11 14:25:04 -04:00
|
|
|
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"]:
|
2020-02-28 14:57:32 -05:00
|
|
|
files.append(
|
|
|
|
"{base_url}/{alias}/{language}/adafruit-circuitpython-{alias}-{language}-{tag}.{extension}"
|
|
|
|
.format(
|
|
|
|
base_url=DOWNLOAD_BASE_URL,
|
|
|
|
tag=new_tag,
|
|
|
|
alias=alias,
|
|
|
|
language=language,
|
|
|
|
extension=extension))
|
2019-04-11 14:25:04 -04:00
|
|
|
current_info[alias]["downloads"] = alias_info["download_count"]
|
|
|
|
current_info[alias]["versions"].append(new_version)
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
changes["new_languages"] = set(languages) - previous_languages
|
|
|
|
|
2019-09-04 19:50:13 -04:00
|
|
|
if changes["new_release"] and user:
|
|
|
|
create_pr(changes, current_info, git_info, user)
|
2018-12-10 16:17:05 -05:00
|
|
|
else:
|
|
|
|
print("No new release to update")
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-15 03:07:29 -04:00
|
|
|
if "RELEASE_TAG" in os.environ and os.environ["RELEASE_TAG"]:
|
2018-11-28 19:58:44 -05:00
|
|
|
generate_download_info()
|
|
|
|
else:
|
|
|
|
print("skipping website update because this isn't a tag")
|