Effort free video editing!
Publish the release binaries to PyPI as wheels
WyattBlue728680fa4ad4
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex b418029e..c584e0f5 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -92,6 +92,12 @@ jobs:- name: Testrun: nimble test+ - name: Keep for wheels+ uses: actions/upload-artifact@v7+ with:+ name: bin-${{ matrix.binary_name }}+ path: ${{ matrix.binary_name }}+ if-no-files-found: error- name: Upload to Releaseif: github.event_name == 'release'uses: softprops/action-gh-release@v3@@ -142,6 +148,14 @@ jobs:with:name: unsigned-windows-x86_64path: auto-editor-windows-x86_64.exe+ - name: Keep for wheels+ # A release that signs puts the signed copy in the wheel instead.+ if: github.event_name != 'release' || vars.AZURE_SIGNING_ACCOUNT == ''+ uses: actions/upload-artifact@v7+ with:+ name: bin-windows-x86_64+ path: auto-editor-windows-x86_64.exe+ if-no-files-found: errorwindows-arm:runs-on: ubuntu-24.04-arm@@ -185,6 +199,14 @@ jobs:with:name: unsigned-windows-aarch64path: auto-editor-windows-aarch64.exe+ - name: Keep for wheels+ # A release that signs puts the signed copy in the wheel instead.+ if: github.event_name != 'release' || vars.AZURE_SIGNING_ACCOUNT == ''+ uses: actions/upload-artifact@v7+ with:+ name: bin-windows-aarch64+ path: auto-editor-windows-aarch64.exe+ if-no-files-found: errorsign-windows:# Only runs once the Azure Artifact Signing account is set up; until then@@ -238,6 +260,12 @@ jobs:--alias "${{ vars.AZURE_SIGNING_ACCOUNT }}/${{ vars.AZURE_SIGNING_PROFILE }}" \--tsaurl http://timestamp.acs.microsoft.com --tsmode RFC3161 \auto-editor-windows-x86_64.exe auto-editor-windows-aarch64.exe+ - name: Keep for wheels+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2+ with:+ name: bin-windows-signed+ path: auto-editor-windows-*.exe+ if-no-files-found: error- name: Upload to Releaseuses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3with:@@ -277,6 +305,12 @@ jobs:run: nimble makeffarmv7- name: Compilerun: nimble makearmv7 && mv auto-editor auto-editor-linux-armv7+ - name: Keep for wheels+ uses: actions/upload-artifact@v7+ with:+ name: bin-linux-armv7+ path: auto-editor-linux-armv7+ if-no-files-found: error- name: Upload to Releaseif: github.event_name == 'release'uses: softprops/action-gh-release@v3@@ -285,3 +319,63 @@ jobs:files: auto-editor-linux-armv7env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}++ wheels:+ # Runs when sign-windows is skipped, which it is unless a release is+ # being signed, but never with a platform missing: pip would quietly give+ # that platform's users an older version instead.+ if: >-+ ${{ !cancelled() && needs.main.result == 'success'+ && needs.windows.result == 'success'+ && needs.windows-arm.result == 'success'+ && needs.linux-armv7.result == 'success'+ && needs.sign-windows.result != 'failure' }}+ runs-on: ubuntu-latest+ needs: [main, windows, windows-arm, sign-windows, linux-armv7]+ permissions:+ contents: read+ steps:+ - name: Checkout+ uses: actions/checkout@v7+ - name: Get binaries+ uses: actions/download-artifact@v8+ with:+ pattern: bin-*+ path: bin+ merge-multiple: true+ - name: Check version+ if: github.event_name == 'release'+ env:+ TAG: ${{ github.event.release.tag_name }}+ run: grep -qF "\"${TAG#v}\"" src/about.nim+ - name: Build wheels+ run: python3 scripts/wheel.py -o dist bin/*+ - name: Upload artifact+ uses: actions/upload-artifact@v7+ with:+ name: wheels+ path: dist/*.whl+ if-no-files-found: error++ pypi:+ if: ${{ !cancelled() && needs.wheels.result == 'success' && github.event_name == 'release' }}+ runs-on: ubuntu-latest+ needs: wheels+ # PyPI's trusted publisher names this environment, so only this job can+ # upload, and it runs nothing from the repository. Actions are pinned by+ # SHA because any of them could use that identity.+ environment: pypi+ permissions:+ id-token: write+ steps:+ - name: Get wheels+ uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2+ with:+ name: wheels+ path: dist+ - name: Publish+ uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3+ with:+ # Signs each wheel with this workflow's identity, which PyPI shows+ # and serves as the file's provenance.+ attestations: truediff --git a/scripts/wheel.py b/scripts/wheel.pynew file mode 100644index 00000000..64d33cd8--- /dev/null+++ b/scripts/wheel.py@@ -0,0 +1,181 @@+"""Wrap release binaries in wheels so `pip install auto-editor` gets one.++usage: python3 scripts/wheel.py [-o DIR] BINARY...++Each binary becomes one wheel. Its platform tag is read out of the binary+because that is the only place the real requirement lives: the glibc a Linux+build needs depends on the machine that linked it.+"""++import argparse+import base64+import hashlib+import re+import struct+import sys+import zipfile+from pathlib import Path++ROOT = Path(__file__).resolve().parent.parent+++def elf_tag(data: bytes) -> str:+ wide = data[4] == 2+ end = "<" if data[5] == 1 else ">"+ arch = {62: "x86_64", 183: "aarch64", 40: "armv7l"}.get(+ struct.unpack_from(end + "H", data, 18)[0]+ )+ if arch is None:+ raise ValueError("unsupported ELF machine")++ if wide:+ phoff = struct.unpack_from(end + "Q", data, 32)[0]+ phentsize, phnum = struct.unpack_from(end + "HH", data, 54)+ else:+ phoff = struct.unpack_from(end + "I", data, 28)[0]+ phentsize, phnum = struct.unpack_from(end + "HH", data, 42)+ PT_INTERP = 3+ dynamic = any(+ struct.unpack_from(end + "I", data, phoff + i * phentsize)[0] == PT_INTERP+ for i in range(phnum)+ )++ if not dynamic:+ # No libc is loaded at run time, so either kind of system can run it.+ return f"manylinux_2_17_{arch}.manylinux2014_{arch}.musllinux_1_1_{arch}"+ if b"ld-musl" in data:+ raise ValueError("dynamic musl builds are not handled")++ # The symbol versions a dynamic binary asks for are the only place these+ # strings appear, and the newest one is the oldest glibc that satisfies it.+ need = max(+ ((int(a), int(b)) for a, b in re.findall(rb"GLIBC_(\d+)\.(\d+)", data)),+ default=(2, 17),+ )+ need = max(need, (2, 17))+ return f"manylinux_{need[0]}_{need[1]}_{arch}"+++def macho_tag(data: bytes) -> str:+ cputype, _, _, ncmds = struct.unpack_from("<IIII", data, 4)+ arch = {0x0100000C: "arm64", 0x01000007: "x86_64"}.get(cputype)+ if arch is None:+ raise ValueError("unsupported Mach-O cpu")++ LC_VERSION_MIN_MACOSX, LC_BUILD_VERSION = 0x24, 0x32+ off = 32+ for _ in range(ncmds):+ cmd, size = struct.unpack_from("<II", data, off)+ minos = None+ if cmd == LC_BUILD_VERSION:+ minos = struct.unpack_from("<I", data, off + 12)[0]+ elif cmd == LC_VERSION_MIN_MACOSX:+ minos = struct.unpack_from("<I", data, off + 8)[0]+ if minos is not None:+ major, minor = minos >> 16, (minos >> 8) & 0xFF+ # Installers only match a zero minor from macOS 11 on.+ return f"macosx_{major}_{0 if major >= 11 else minor}_{arch}"+ off += size+ raise ValueError("Mach-O has no minimum OS version")+++def pe_tag(data: bytes) -> str:+ pe = struct.unpack_from("<I", data, 0x3C)[0]+ if data[pe : pe + 4] != b"PE\0\0":+ raise ValueError("not a PE file")+ machine = struct.unpack_from("<H", data, pe + 4)[0]+ arch = {0x8664: "win_amd64", 0xAA64: "win_arm64"}.get(machine)+ if arch is None:+ raise ValueError("unsupported PE machine")+ return arch+++def platform_tag(data: bytes) -> tuple[str, str]:+ """Return the wheel platform tag and the installed script's name."""+ if data[:4] == b"\x7fELF":+ return elf_tag(data), "auto-editor"+ if data[:4] == b"\xcf\xfa\xed\xfe":+ return macho_tag(data), "auto-editor"+ if data[:2] == b"MZ":+ return pe_tag(data), "auto-editor.exe"+ raise ValueError("not an ELF, Mach-O, or PE binary")+++def build(binary: Path, version: str, out: Path) -> Path:+ data = binary.read_bytes()+ plat, script = platform_tag(data)+ tag = f"py3-none-{plat}"+ name = f"auto_editor-{version}"++ metadata = (+ "Metadata-Version: 2.1\n"+ "Name: auto-editor\n"+ f"Version: {version}\n"+ "Summary: Auto-Editor: Effort free video editing!\n"+ "Home-page: https://auto-editor.com\n"+ "Author: WyattBlue\n"+ "License: Unlicense\n"+ "Project-URL: Source Code, https://github.com/WyattBlue/auto-editor\n"+ "Project-URL: Bug Tracker, https://github.com/WyattBlue/auto-editor/issues\n"+ "Keywords: video,audio,media,editor,editing,silence-removal\n"+ "Requires-Python: >=3.8\n"+ "Description-Content-Type: text/markdown\n"+ "\n" + (ROOT / "README.md").read_text(encoding="utf-8")+ ).encode()+ wheel = (+ "Wheel-Version: 1.0\n"+ "Generator: auto-editor scripts/wheel.py\n"+ "Root-Is-Purelib: false\n"+ + "".join(f"Tag: py3-none-{p}\n" for p in plat.split("."))+ ).encode()++ files = [+ # Installers copy anything under data/scripts into the environment's+ # bin directory as is, which is all a prebuilt executable needs.+ (f"{name}.data/scripts/{script}", data, 0o755),+ (f"{name}.dist-info/METADATA", metadata, 0o644),+ (f"{name}.dist-info/WHEEL", wheel, 0o644),+ (f"{name}.dist-info/LICENSE", (ROOT / "LICENSE").read_bytes(), 0o644),+ ]+ record = ""+ for path, body, _ in files:+ digest = base64.urlsafe_b64encode(hashlib.sha256(body).digest())+ record += f"{path},sha256={digest.rstrip(b'=').decode()},{len(body)}\n"+ record += f"{name}.dist-info/RECORD,,\n"+ files.append((f"{name}.dist-info/RECORD", record.encode(), 0o644))++ dest = out / f"{name}-{tag}.whl"+ with zipfile.ZipFile(dest, "w", zipfile.ZIP_DEFLATED) as zf:+ for path, body, mode in files:+ # A fixed date keeps the wheel a pure function of its inputs.+ info = zipfile.ZipInfo(path, (1980, 1, 1, 0, 0, 0))+ info.compress_type = zipfile.ZIP_DEFLATED+ info.external_attr = (0o100000 | mode) << 16+ zf.writestr(info, body)+ return dest+++def main() -> None:+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])+ parser.add_argument("binaries", nargs="+", type=Path)+ parser.add_argument("-o", "--out", type=Path, default=Path("dist"))+ args = parser.parse_args()++ about = (ROOT / "src" / "about.nim").read_text()+ version = re.search(r'version\* = "([^"]+)"', about).group(1)++ args.out.mkdir(parents=True, exist_ok=True)+ seen = {}+ for binary in args.binaries:+ try:+ dest = build(binary, version, args.out)+ except ValueError as e:+ sys.exit(f"{binary}: {e}")+ if dest.name in seen:+ sys.exit(f"{binary} and {seen[dest.name]} both make {dest.name}")+ seen[dest.name] = binary+ print(dest)+++if __name__ == "__main__":+ main()