2018-11-28 19:58:44 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-06-03 18:40:05 -04:00
|
|
|
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
import json
|
|
|
|
import os
|
2023-03-14 22:37:56 -04:00
|
|
|
import requests
|
2018-11-28 19:58:44 -05:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import sh
|
|
|
|
import base64
|
2023-02-09 22:58:37 -05:00
|
|
|
from io import StringIO
|
2018-11-28 19:58:44 -05:00
|
|
|
from datetime import date
|
|
|
|
from sh.contrib import git
|
|
|
|
|
|
|
|
sys.path.append("adabot")
|
|
|
|
import adabot.github_requests as github
|
|
|
|
|
2022-04-16 20:04:03 -04:00
|
|
|
sys.path.append("../docs")
|
|
|
|
from shared_bindings_matrix import (
|
|
|
|
SUPPORTED_PORTS,
|
|
|
|
support_matrix_by_board,
|
2022-07-20 21:08:51 -04:00
|
|
|
get_board_mapping,
|
2022-04-16 20:04:03 -04:00
|
|
|
)
|
2020-11-18 20:38:06 -05:00
|
|
|
|
2022-07-20 21:08:51 -04:00
|
|
|
LANGUAGE_ALLOW_LIST = set(
|
2021-04-30 16:30:13 -04:00
|
|
|
[
|
|
|
|
"ID",
|
|
|
|
"de_DE",
|
|
|
|
"en_GB",
|
|
|
|
"en_US",
|
|
|
|
"en_x_pirate",
|
|
|
|
"es",
|
|
|
|
"fil",
|
|
|
|
"fr",
|
|
|
|
"it_IT",
|
|
|
|
"ja",
|
|
|
|
"nl",
|
|
|
|
"pl",
|
|
|
|
"pt_BR",
|
2021-11-16 10:45:28 -05:00
|
|
|
"ru",
|
2021-04-30 16:30:13 -04:00
|
|
|
"sv",
|
2022-01-10 14:08:29 -05:00
|
|
|
"tr",
|
2021-04-30 16:30:13 -04:00
|
|
|
"zh_Latn_pinyin",
|
|
|
|
]
|
|
|
|
)
|
2021-03-24 18:39:47 -04:00
|
|
|
|
|
|
|
|
2021-04-30 16:30:13 -04:00
|
|
|
def get_languages(list_all=False):
|
2021-03-24 18:39:47 -04:00
|
|
|
languages = set()
|
2018-11-28 19:58:44 -05:00
|
|
|
for f in os.scandir("../locale"):
|
|
|
|
if f.name.endswith(".po"):
|
2021-03-24 18:39:47 -04:00
|
|
|
languages.add(f.name[:-3])
|
|
|
|
if not list_all:
|
2022-07-20 21:08:51 -04:00
|
|
|
languages = languages & LANGUAGE_ALLOW_LIST
|
2021-03-29 04:54:11 -04:00
|
|
|
return sorted(list(languages), key=str.casefold)
|
2018-11-28 19:58:44 -05:00
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
def get_version_info():
|
|
|
|
version = None
|
2023-02-09 22:58:37 -05:00
|
|
|
buffer = StringIO()
|
|
|
|
git("rev-parse", "--short", "HEAD", _out=buffer)
|
|
|
|
sha = buffer.getvalue().strip()
|
2018-11-28 19:58:44 -05:00
|
|
|
try:
|
2023-02-09 22:58:37 -05:00
|
|
|
buffer = StringIO()
|
|
|
|
git("describe", "--tags", "--exact-match", _out=buffer)
|
|
|
|
version = buffer.getvalue().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:
|
2020-11-18 20:38:06 -05:00
|
|
|
version = "{}-{}".format(date.today().strftime("%Y%m%d"), sha[:7])
|
2018-11-28 19:58:44 -05:00
|
|
|
|
|
|
|
return sha, version
|
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
def get_current_info():
|
2021-12-13 08:58:26 -05:00
|
|
|
response = github.get("/repos/adafruit/circuitpython-org/git/refs/heads/main")
|
2018-11-28 19:58:44 -05:00
|
|
|
if not response.ok:
|
|
|
|
print(response.text)
|
2021-12-13 08:58:26 -05:00
|
|
|
raise RuntimeError("cannot get main sha")
|
2018-11-28 19:58:44 -05:00
|
|
|
commit_sha = response.json()["object"]["sha"]
|
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
response = github.get(
|
|
|
|
"/repos/adafruit/circuitpython-org/contents/_data/files.json?ref=" + commit_sha
|
|
|
|
)
|
2018-11-28 19:58:44 -05:00
|
|
|
if not response.ok:
|
|
|
|
print(response.text)
|
|
|
|
raise RuntimeError("cannot get previous files.json")
|
|
|
|
|
|
|
|
response = response.json()
|
|
|
|
|
|
|
|
git_info = commit_sha, response["sha"]
|
2023-03-14 22:37:56 -04:00
|
|
|
|
|
|
|
if response["content"] != "":
|
|
|
|
# if the file is there
|
|
|
|
current_list = json.loads(base64.b64decode(response["content"]).decode("utf-8"))
|
|
|
|
else:
|
|
|
|
# if too big, the file is not included
|
|
|
|
download_url = response["download_url"]
|
|
|
|
response = requests.get(download_url)
|
|
|
|
if not response.ok:
|
|
|
|
print(response.text)
|
|
|
|
raise RuntimeError("cannot get previous files.json")
|
|
|
|
current_list = response.json()
|
|
|
|
|
2019-03-11 20:07:15 -04:00
|
|
|
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
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
|
2021-03-24 18:39:47 -04:00
|
|
|
def create_json(updated):
|
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)
|
2021-03-24 18:39:47 -04:00
|
|
|
return json.dumps(updated_list, sort_keys=True, indent=1).encode("utf-8") + b"\n"
|
2019-03-11 20:07:15 -04:00
|
|
|
|
2021-03-24 18:39:47 -04:00
|
|
|
|
|
|
|
def create_pr(changes, updated, git_info, user):
|
|
|
|
commit_sha, original_blob_sha = git_info
|
|
|
|
branch_name = "new_release_" + changes["new_release"]
|
|
|
|
updated = create_json(updated)
|
2020-11-18 20:38:06 -05:00
|
|
|
# print(updated.decode("utf-8"))
|
2021-03-24 18:39:47 -04:00
|
|
|
|
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(
|
2020-11-18 20:38:06 -05:00
|
|
|
changes["new_release"], boards, languages
|
2018-11-28 19:58:44 -05:00
|
|
|
)
|
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
create_branch = {"ref": "refs/heads/" + branch_name, "sha": commit_sha}
|
2021-03-15 09:57:36 -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,
|
2020-11-18 20:38:06 -05:00
|
|
|
"branch": branch_name,
|
2018-11-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
response = github.put(
|
2021-09-05 19:52:37 -04:00
|
|
|
"/repos/{}/circuitpython-org/contents/_data/files.json".format(user), json=update_file
|
2020-11-18 20:38:06 -05:00
|
|
|
)
|
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,
|
2021-12-13 08:58:26 -05:00
|
|
|
"base": "main",
|
2018-11-28 19:58:44 -05:00
|
|
|
"body": message,
|
2020-11-18 20:38:06 -05:00
|
|
|
"maintainer_can_modify": True,
|
2018-11-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
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 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
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
|
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
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
changes = {"new_release": new_tag, "new_boards": [], "new_languages": []}
|
2018-11-28 19:58:44 -05:00
|
|
|
|
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()
|
|
|
|
|
2022-07-19 17:35:51 -04:00
|
|
|
support_matrix = support_matrix_by_board(use_branded_name=False, withurl=False)
|
2020-06-29 01:27:45 -04:00
|
|
|
|
2018-11-28 19:58:44 -05:00
|
|
|
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]
|
2021-04-12 17:37:41 -04:00
|
|
|
for version in list(info["versions"]):
|
2018-11-28 19:58:44 -05:00
|
|
|
previous_releases.add(version["version"])
|
2021-01-27 17:07:14 -05:00
|
|
|
previous_languages.update(version["languages"])
|
2020-11-18 20:38:06 -05:00
|
|
|
if version["stable"] == new_stable or (
|
|
|
|
new_stable and version["version"].startswith(this_version)
|
|
|
|
):
|
2018-11-28 19:58:44 -05:00
|
|
|
info["versions"].remove(version)
|
|
|
|
|
|
|
|
board_mapping = get_board_mapping()
|
|
|
|
|
|
|
|
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)
|
2020-11-18 20:38:06 -05:00
|
|
|
current_info[alias] = {"downloads": 0, "versions": []}
|
2019-04-11 14:25:04 -04:00
|
|
|
new_version = {
|
|
|
|
"stable": new_stable,
|
|
|
|
"version": new_tag,
|
2020-07-03 15:54:48 -04:00
|
|
|
"languages": languages,
|
2022-07-19 17:35:51 -04:00
|
|
|
# add modules, extensions, frozen_libraries explicitly
|
|
|
|
"modules": support_matrix[alias]["modules"],
|
|
|
|
"extensions": support_matrix[alias]["extensions"],
|
|
|
|
"frozen_libraries": support_matrix[alias]["frozen_libraries"],
|
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:
|
2021-03-29 12:14:37 -04:00
|
|
|
if "DEBUG" in os.environ:
|
|
|
|
print(create_json(current_info).decode("utf8"))
|
2022-07-19 17:35:51 -04:00
|
|
|
else:
|
|
|
|
print("No new release to update")
|
2018-11-28 19:58:44 -05:00
|
|
|
|
2020-11-18 20:38:06 -05:00
|
|
|
|
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")
|