circuitpython/tools/ci_set_matrix.py

308 lines
9.6 KiB
Python
Raw Normal View History

2021-09-15 05:35:05 -04:00
#! /usr/bin/env python3
# SPDX-FileCopyrightText: 2021 Scott Shawcroft
# SPDX-FileCopyrightText: 2021 microDev
#
# SPDX-License-Identifier: MIT
"""
This script is used in GitHub Actions to determine what docs/boards are
built based on what files were changed. The base commit varies depending
on the event that triggered run. Pull request runs will compare to the
base branch while pushes will compare to the current ref. We override this
for the adafruit/circuitpython repo so we build all docs/boards for pushes.
When making changes to the script it is useful to manually test it.
You can for instance run
```shell
tools/ci_set_matrix ports/raspberrypi/common-hal/socket/SSLSocket.c
```
and (at the time this comment was written) get a series of messages indicating
that only the single board raspberry_pi_pico_w would be built.
2021-09-15 05:35:05 -04:00
"""
import re
import os
import sys
import json
import pathlib
2023-01-07 07:47:48 -05:00
import subprocess
from concurrent.futures import ThreadPoolExecutor
tools_dir = pathlib.Path(__file__).resolve().parent
top_dir = tools_dir.parent
sys.path.insert(0, str(tools_dir / "adabot"))
sys.path.insert(0, str(top_dir / "docs"))
2021-09-15 05:35:05 -04:00
import build_board_info
from shared_bindings_matrix import (
get_settings_from_makefile,
SUPPORTED_PORTS,
all_ports_all_boards,
)
2021-09-15 05:35:05 -04:00
# Files that never influence board builds
IGNORE_BOARD = {
".devcontainer",
"conf.py",
"docs",
"tests",
"tools/ci_changes_per_commit.py",
"tools/ci_check_duplicate_usb_vid_pid.py",
"tools/ci_set_matrix.py",
}
2022-12-21 07:27:23 -05:00
PATTERN_DOCS = (
r"^(?:\.github|docs|extmod\/ulab)|"
r"^(?:(?:ports\/\w+\/bindings|shared-bindings)\S+\.c|tools\/extract_pyi\.py|\.readthedocs\.yml|conf\.py|requirements-doc\.txt)$|"
r"(?:-stubs|\.(?:md|MD|mk|rst|RST)|/Makefile)$"
)
PATTERN_WINDOWS = {
".github/",
"extmod/",
"lib/",
"mpy-cross/",
"ports/unix/",
"py/",
"tools/",
"requirements-dev.txt",
}
def git_diff(pattern: str):
return set(
subprocess.run(
f"git diff {pattern} --name-only",
capture_output=True,
shell=True,
)
.stdout.decode("utf-8")
.split("\n")[:-1]
)
2023-03-21 13:02:40 -04:00
compute_diff = bool(os.environ.get("BASE_SHA") and os.environ.get("HEAD_SHA"))
if len(sys.argv) > 1:
print("Using files list on commandline")
changed_files = set(sys.argv[1:])
2023-03-21 13:02:40 -04:00
elif compute_diff:
print("Using files list by computing diff")
changed_files = git_diff("$BASE_SHA...$HEAD_SHA")
if os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
2023-03-11 23:12:16 -05:00
changed_files.intersection_update(git_diff("$GITHUB_SHA~...$GITHUB_SHA"))
else:
print("Using files list in CHANGED_FILES")
changed_files = set(json.loads(os.environ.get("CHANGED_FILES") or "[]"))
print("Using jobs list in LAST_FAILED_JOBS")
last_failed_jobs = json.loads(os.environ.get("LAST_FAILED_JOBS") or "{}")
def print_enclosed(title, content):
print("::group::" + title)
print(content)
print("::endgroup::")
print_enclosed("Log: changed_files", changed_files)
print_enclosed("Log: last_failed_jobs", last_failed_jobs)
2021-09-15 05:35:05 -04:00
def set_output(name: str, value):
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "at") as f:
print(f"{name}={value}", file=f)
else:
print(f"Would set GitHub actions output {name} to '{value}'")
2023-03-21 13:02:40 -04:00
def set_boards(build_all: bool):
2021-09-15 05:35:05 -04:00
all_board_ids = set()
boards_to_build = all_board_ids if build_all else set()
2021-09-15 05:35:05 -04:00
board_to_port = {}
port_to_board = {}
board_setting = {}
for id, info in build_board_info.get_board_mapping().items():
if info.get("alias"):
2021-09-15 05:35:05 -04:00
continue
port = info["port"]
all_board_ids.add(id)
board_to_port[id] = port
port_to_board.setdefault(port, set()).add(id)
2021-09-15 05:35:05 -04:00
def compute_board_settings(boards):
need = set(boards) - set(board_setting.keys())
if not need:
return
def get_settings(board):
return (
board,
get_settings_from_makefile(str(top_dir / "ports" / board_to_port[board]), board),
)
with ThreadPoolExecutor(max_workers=os.cpu_count()) as ex:
board_setting.update(ex.map(get_settings, need))
2021-09-15 05:35:05 -04:00
if not build_all:
pattern_port = re.compile(r"^ports/([^/]+)/")
pattern_board = re.compile(r"^ports/[^/]+/boards/([^/]+)/")
pattern_module = re.compile(
r"^(ports/[^/]+/(?:common-hal|bindings)|shared-bindings|shared-module)/([^/]+)/"
)
for file in changed_files:
if len(all_board_ids) == len(boards_to_build):
break
if any([file.startswith(path) for path in IGNORE_BOARD]):
continue
2021-09-15 05:35:05 -04:00
# See if it is board specific
board_matches = pattern_board.search(file)
2021-09-15 05:35:05 -04:00
if board_matches:
boards_to_build.add(board_matches.group(1))
2021-09-15 05:35:05 -04:00
continue
# See if it is port specific
port_matches = pattern_port.search(file)
module_matches = pattern_module.search(file)
port = port_matches.group(1) if port_matches else None
if port and not module_matches:
if port != "unix":
boards_to_build.update(port_to_board[port])
continue
2021-09-15 05:35:05 -04:00
# As a (nearly) last resort, for some certain files, we compute the settings from the
# makefile for each board and determine whether to build them that way
if file.startswith("frozen") or file.startswith("supervisor") or module_matches:
boards = port_to_board[port] if port else all_board_ids
compute_board_settings(boards)
for board in boards:
settings = board_setting[board]
# Check frozen files to see if they are in each board
if file.startswith("frozen"):
if file in settings["FROZEN_MPY_DIRS"]:
boards_to_build.add(board)
continue
# Check supervisor files
# This is useful for limiting workflow changes to the relevant boards
if file.startswith("supervisor"):
if file in settings["SRC_SUPERVISOR"]:
boards_to_build.add(board)
continue
if file.startswith("supervisor/shared/web_workflow/static/"):
web_workflow = settings["CIRCUITPY_WEB_WORKFLOW"]
if web_workflow != "0":
boards_to_build.add(board)
continue
# Check module matches
if module_matches:
module = module_matches.group(2) + "/"
if module in settings["SRC_PATTERNS"]:
boards_to_build.add(board)
continue
continue
2021-09-15 05:35:05 -04:00
# Otherwise build it all
boards_to_build = all_board_ids
break
# Append previously failed boards
boards_to_build.update(last_failed_jobs.get("ports", []))
2023-03-04 00:02:19 -05:00
print("Building boards:", bool(boards_to_build))
# Split boards by port
port_to_boards_to_build = {}
2023-02-24 00:55:06 -05:00
# Append boards according to job
2022-11-19 12:59:05 -05:00
for board in sorted(boards_to_build):
2021-10-26 15:06:07 -04:00
port = board_to_port.get(board)
# A board can appear due to its _deletion_ (rare)
# if this happens it's not in `board_to_port`.
if not port:
continue
2023-03-04 00:02:19 -05:00
port_to_boards_to_build.setdefault(port, []).append(board)
print(" ", board)
2021-09-15 05:35:05 -04:00
2023-03-04 00:02:19 -05:00
if port_to_boards_to_build:
port_to_boards_to_build["ports"] = sorted(list(port_to_boards_to_build.keys()))
2021-09-15 05:35:05 -04:00
2023-03-04 00:02:19 -05:00
# Set the step outputs
set_output("ports", json.dumps(port_to_boards_to_build))
2021-09-15 05:35:05 -04:00
2023-03-04 00:02:19 -05:00
2023-03-21 13:02:40 -04:00
def set_docs(run: bool):
if not run:
2023-03-21 13:02:40 -04:00
if last_failed_jobs.get("docs"):
run = True
else:
pattern_doc = re.compile(PATTERN_DOCS)
github_workspace = os.environ.get("GITHUB_WORKSPACE") or ""
github_workspace = github_workspace and github_workspace + "/"
for file in changed_files:
if pattern_doc.search(file) and (
(
subprocess.run(
f"git diff -U0 $BASE_SHA...$HEAD_SHA {github_workspace + file} | grep -o -m 1 '^[+-]\/\/|'",
capture_output=True,
shell=True,
).stdout
)
if file.endswith(".c")
else True
):
run = True
break
2021-09-15 05:35:05 -04:00
# Set the step outputs
print("Building docs:", run)
set_output("docs", run)
2023-03-21 13:02:40 -04:00
def set_windows(run: bool):
if not run:
2023-03-21 13:02:40 -04:00
if last_failed_jobs.get("windows"):
run = True
else:
for file in changed_files:
for pattern in PATTERN_WINDOWS:
if file.startswith(pattern) and not any(
[file.startswith(path) for path in IGNORE_BOARD]
):
run = True
break
else:
continue
break
# Set the step outputs
print("Building windows:", run)
set_output("windows", run)
2021-09-15 05:35:05 -04:00
def main():
2023-03-21 13:02:40 -04:00
run_all = not changed_files and not compute_diff
print("Running: " + ("all" if run_all else "conditionally"))
2023-03-04 00:02:19 -05:00
# Set jobs
2023-03-21 13:02:40 -04:00
set_docs(run_all)
set_windows(run_all)
set_boards(run_all)
2021-09-15 05:35:05 -04:00
if __name__ == "__main__":
main()