A fast, helpful, and open-source document parser
fix: python wheel did not properly record binaries
Logan Markewich2edf7ee90b83
diff --git a/.github/workflows/release-python.yml b/.github/workflows/release-python.ymlindex e9080cb..e9e3c99 100644--- a/.github/workflows/release-python.yml+++ b/.github/workflows/release-python.yml@@ -158,22 +158,13 @@ jobs:export PDFIUM_LIB_PATH="$(dirname "$PDFIUM_LIB")"fibash scripts/copy-pdfium.sh+ if [ "$(uname -s)" = "Darwin" ]; then+ DYLIB="libpdfium.dylib"+ else+ DYLIB="libpdfium.so"+ fifor whl in dist/*.whl; do- python -c "- import zipfile, os, sys- whl = sys.argv[1]- if sys.platform == 'darwin':- dylib = 'libpdfium.dylib'- else:- dylib = 'libpdfium.so'- dylib_path = os.path.join('liteparse', dylib)- if not os.path.exists(dylib_path):- print(f'Warning: {dylib_path} not found, skipping')- sys.exit(0)- with zipfile.ZipFile(whl, 'a') as z:- z.write(dylib_path, f'liteparse/{dylib}')- print(f'Added {dylib} to {whl}')- " "$whl"+ python scripts/inject_dylib.py "$whl" "liteparse/$DYLIB" "liteparse/$DYLIB"done- name: Copy pdfium dylib into wheel (Windows)@@ -193,15 +184,7 @@ jobs:Copy-Item $dll.FullName -Destination "liteparse\pdfium.dll"Write-Host "Copied $($dll.FullName) -> liteparse\pdfium.dll"foreach ($whl in Get-ChildItem dist\*.whl) {- python -c @"- import zipfile, sys, os- whl = sys.argv[1]- dll = os.path.join('liteparse', 'pdfium.dll')- if os.path.exists(dll):- with zipfile.ZipFile(whl, 'a') as z:- z.write(dll, 'liteparse/pdfium.dll')- print(f'Added pdfium.dll to {whl}')- "@ $whl.FullName+ python scripts/inject_dylib.py $whl.FullName "liteparse/pdfium.dll" "liteparse/pdfium.dll"}} else {Write-Error "Could not find pdfium.dll"diff --git a/packages/python/scripts/inject_dylib.py b/packages/python/scripts/inject_dylib.pynew file mode 100644index 0000000..c8dd9e9--- /dev/null+++ b/packages/python/scripts/inject_dylib.py@@ -0,0 +1,81 @@+"""Inject the pdfium shared library into a built wheel and update its RECORD.++Maturin's `[tool.maturin] include` directive only bundles files that exist at+build time. Because libpdfium is produced as a side effect of compiling+pdfium-sys and copied in afterwards by CI, we have to splice it into the+finished wheel ourselves. Appending with `zipfile.ZipFile(..., 'a')` works for+the bytes, but leaves the `*.dist-info/RECORD` manifest out of sync — Poetry+and other strict installers warn about that. This script rewrites the wheel so+the dylib is present in both the archive and RECORD.+"""++from __future__ import annotations++import base64+import hashlib+import sys+import zipfile+from pathlib import Path+++def _record_hash(data: bytes) -> str:+ digest = hashlib.sha256(data).digest()+ return "sha256=" + base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")+++def inject(wheel_path: Path, dylib_path: Path, arcname: str) -> None:+ if not dylib_path.exists():+ print(f"Warning: {dylib_path} not found, skipping {wheel_path}")+ return++ dylib_bytes = dylib_path.read_bytes()+ dylib_hash = _record_hash(dylib_bytes)+ dylib_size = str(len(dylib_bytes))++ tmp_path = wheel_path.with_suffix(wheel_path.suffix + ".tmp")++ with zipfile.ZipFile(wheel_path, "r") as zin:+ record_name = next(+ (n for n in zin.namelist() if n.endswith(".dist-info/RECORD")), None+ )+ if record_name is None:+ raise RuntimeError(f"No RECORD file found in {wheel_path}")++ record_text = zin.read(record_name).decode("utf-8")+ record_lines = [ln for ln in record_text.splitlines() if ln.strip()]+ # Drop any pre-existing entries for this dylib or the RECORD itself+ # (RECORD's own line always carries empty hash/size).+ record_lines = [+ ln+ for ln in record_lines+ if not ln.startswith(arcname + ",")+ and not ln.startswith(record_name + ",")+ ]+ record_lines.append(f"{arcname},{dylib_hash},{dylib_size}")+ record_lines.append(f"{record_name},,")+ new_record = ("\n".join(record_lines) + "\n").encode("utf-8")++ with zipfile.ZipFile(+ tmp_path, "w", compression=zipfile.ZIP_DEFLATED+ ) as zout:+ for item in zin.infolist():+ if item.filename == record_name or item.filename == arcname:+ continue+ zout.writestr(item, zin.read(item.filename))+ zout.writestr(arcname, dylib_bytes)+ zout.writestr(record_name, new_record)++ tmp_path.replace(wheel_path)+ print(f"Injected {arcname} ({dylib_size} bytes) into {wheel_path}")+++def main() -> int:+ if len(sys.argv) != 4:+ print("usage: inject_dylib.py <wheel> <dylib> <arcname>", file=sys.stderr)+ return 2+ inject(Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3])+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())