Files
microduck/scripts/homebrew-formula.py
T
Pierre Rouanet 910b411b4b duckctl: push the Homebrew formula on each release
After the PyPI publish, a homebrew job renders the formula for the version
just released from PyPI's wheels and pushes it to pierre-rouanet/homebrew-tools,
so brew upgrade duckctl follows the releases. It writes with a deploy key
scoped to the tap, held by a homebrew environment.

Assisted-by: Claude:claude-opus-5-5
2026-10-15 12:47:13 +02:00

117 lines
3.6 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Print the Homebrew formula for a released `duckctl`, from the wheels PyPI holds for it.
scripts/homebrew-formula.py 0.18.0 > Formula/duckctl.rb
`duckctl-pypi.yml` runs it after publishing and pushes the result to the tap,
`pierre-rouanet/homebrew-tools`, so `brew upgrade duckctl` follows the releases. The formula
installs the binary out of the wheel, so nothing is compiled. It is rendered whole rather than
patched: this file is its source, and an edit made in the tap is overwritten by the next release.
PyPI can take a moment to list files it has just accepted, so a missing wheel is retried before
it is an error.
"""
import json
import sys
import time
import urllib.error
import urllib.request
# Homebrew's `on_macos`/`on_linux` × `on_arm`/`on_intel`, and the wheel tag each one installs.
PLATFORMS = {
("macos", "arm"): "macosx_11_0_arm64",
("macos", "intel"): "macosx_10_12_x86_64",
("linux", "arm"): "manylinux_2_28_aarch64",
("linux", "intel"): "manylinux_2_28_x86_64",
}
ATTEMPTS = 20
DELAY_S = 15
def wheels(version):
"""The URL and sha256 of each platform's wheel, keyed like `PLATFORMS`."""
url = f"https://pypi.org/pypi/duckctl/{version}/json"
for attempt in range(1, ATTEMPTS + 1):
try:
with urllib.request.urlopen(url) as response:
files = json.load(response)["urls"]
except urllib.error.HTTPError as e:
if e.code != 404:
raise
files = []
by_name = {f["filename"]: f for f in files}
found, missing = {}, []
for key, tag in PLATFORMS.items():
name = f"duckctl-{version}-py3-none-{tag}.whl"
if name in by_name:
found[key] = (by_name[name]["url"], by_name[name]["digests"]["sha256"])
else:
missing.append(name)
if not missing:
return found
print(f"attempt {attempt}/{ATTEMPTS}: PyPI does not list {', '.join(missing)} yet", file=sys.stderr)
if attempt < ATTEMPTS:
time.sleep(DELAY_S)
sys.exit(f"duckctl {version}: PyPI never listed {', '.join(missing)}")
def render(version, found):
def block(os):
lines = [f" on_{os} do"]
for arch in ("arm", "intel"):
url, sha = found[(os, arch)]
lines += [
f" on_{arch} do",
f' url "{url}",',
" using: :nounzip",
f' sha256 "{sha}"',
" end",
]
return "\n".join(lines + [" end"])
return f"""\
# Generated by pollen-robotics/microduck's scripts/homebrew-formula.py on each release. Edit it there.
class Duckctl < Formula
desc "Talk to a Microduck robot from a laptop over Bluetooth LE"
homepage "https://github.com/pollen-robotics/microduck"
version "{version}"
license "Apache-2.0"
# PyPI gets each stable `daemon-vX.Y.Z` release, so the latest release names the version.
livecheck do
url :homepage
regex(/^daemon-v?(\\d+(?:\\.\\d+)+)$/i)
strategy :github_latest
end
uses_from_macos "unzip" => :build
# The binary CI builds for each release, taken out of the PyPI wheel it ships in.
{block("macos")}
{block("linux")}
def install
system "unzip", "-q", Dir["*.whl"].first
bin.install "duckctl-#{{version}}.data/scripts/duckctl"
end
test do
assert_match version.to_s, shell_output("#{{bin}}/duckctl --version")
end
end
"""
def main():
if len(sys.argv) != 2:
sys.exit(f"usage: {sys.argv[0]} <version>")
version = sys.argv[1]
sys.stdout.write(render(version, wheels(version)))
if __name__ == "__main__":
main()