From 39a0f3479896f9a4377fe0dc6bba43c82e09797e Mon Sep 17 00:00:00 2001 From: Yurii Date: Mon, 23 Nov 2026 18:39:40 +0300 Subject: [PATCH] fix: download TinyUSB sources using SDK revisions Download lib-builder and TinyUSB source snapshots directly using the revisions in the installed SDK versions.txt. Avoid the GitHub REST lookup without maintaining a separate set of commit pins. Check the archive commit ID, extract the required files and reuse completed downloads. Incomplete extraction is retried on the next invocation. --- tools/tinyusb_s2.py | 56 ++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/tools/tinyusb_s2.py b/tools/tinyusb_s2.py index a081e3d..8116273 100644 --- a/tools/tinyusb_s2.py +++ b/tools/tinyusb_s2.py @@ -1,9 +1,10 @@ """Provide Arduino TinyUSB for the ESP32-S2 HybridCompile SDK.""" +from io import BytesIO from pathlib import Path import shutil -import subprocess from urllib.request import Request, urlopen +from zipfile import ZipFile from SCons.Script import Return @@ -13,31 +14,27 @@ if env.IsCleanTarget(): Return() -def checkout(repository, revision, directory): - if not (directory / ".git").is_dir(): - subprocess.run(["git", "clone", "--filter=blob:none", "--no-checkout", - repository, str(directory)], check=True) - result = subprocess.run(["git", "-C", str(directory), "rev-parse", "--verify", revision + "^{commit}"], - capture_output=True, text=True) - if result.returncode: - # SDK metadata can reference commits no longer reachable from upstream branches. - api = repository.removesuffix(".git").replace("https://github.com/", "https://api.github.com/repos/", 1) - request = Request(api + "/commits/" + revision, headers={ - "Accept": "application/vnd.github.sha", - "User-Agent": "OTGateway" - }) - with urlopen(request, timeout=30) as response: - commit = response.read().decode("ascii").strip() - if (len(commit) != 40 or not commit.startswith(revision) - or any(char not in "0123456789abcdef" for char in commit)): - raise RuntimeError("Unexpected GitHub commit for SDK revision " + revision) - subprocess.run(["git", "-C", str(directory), "fetch", "--depth=1", "origin", commit], check=True) +def download_sources(repository, revision, cache, paths): + directory = cache / (repository.rsplit("/", 1)[-1] + "-" + revision) + marker = directory / ".ready" + if marker.is_file() and all((directory / path).exists() for path in paths): + return directory + marker.unlink(missing_ok=True) - else: - commit = result.stdout.strip() + request = Request("https://codeload.github.com/" + repository + "/zip/" + revision, + headers={"User-Agent": "OTGateway"}) + with urlopen(request, timeout=60) as response, ZipFile(BytesIO(response.read())) as archive: + if len(archive.comment) != 40 or not archive.comment.decode("ascii").startswith(revision): + raise RuntimeError("Source archive does not match SDK revision " + revision) + prefix = directory.name + "/" + members = [name for name in archive.namelist() + if any(name == prefix + path or name.startswith(prefix + path + "/") for path in paths)] + archive.extractall(cache, members=members) - # A fresh --no-checkout clone can have the correct HEAD and still lack its files. - subprocess.run(["git", "-C", str(directory), "checkout", "--detach", commit], check=True) + if not all((directory / path).exists() for path in paths): + raise RuntimeError("Source archive is missing required files") + marker.touch() + return directory platform = env.PioPlatform() @@ -53,12 +50,15 @@ for line in (sdk / "versions.txt").read_text(encoding="utf-8").splitlines(): builder_revision = versions["lib-builder"] tinyusb_revision = versions["tinyusb"] + cache = project / ".pio/tinyusb-s2" cache.mkdir(parents=True, exist_ok=True) -builder = cache / ("lib-builder-" + builder_revision) -tinyusb = cache / ("tinyusb-" + tinyusb_revision) -checkout("https://github.com/espressif/esp32-arduino-lib-builder.git", builder_revision, builder) -checkout("https://github.com/hathach/tinyusb.git", tinyusb_revision, tinyusb) +sources = cache / "sources" +sources.mkdir(exist_ok=True) +builder = download_sources("espressif/esp32-arduino-lib-builder", builder_revision, sources, + ("components/arduino_tinyusb/CMakeLists.txt", + "components/arduino_tinyusb/Kconfig.projbuild", "components/arduino_tinyusb/include")) +tinyusb = download_sources("hathach/tinyusb", tinyusb_revision, sources, ("src", "hw/bsp")) original = builder / "components/arduino_tinyusb" component = cache / ("component-" + builder_revision + "-" + tinyusb_revision) / "arduino_tinyusb"