Skip to content

Instantly share code, notes, and snippets.

@danielknell
Last active May 30, 2022 14:46
Show Gist options
  • Save danielknell/7d1160d0fe41a3380182b235335a1b59 to your computer and use it in GitHub Desktop.
Save danielknell/7d1160d0fe41a3380182b235335a1b59 to your computer and use it in GitHub Desktop.
Fix Poetry Wheels

🚨 🚨 this script is not suitable for published wheels 🚨 🚨

fix_poetry_wheels.py

assuming dependencies such as the following:

[tool.poetry.dependencies]
mydep = { path = "../mydep/", develop = false }

when you create wheels with poetry from projects using relative path dependencies, those path dependencies are persisted into the wheels METADATA file as follows:

Requires-Dist: mydep @ ../mydep

this script iterates through wheel files in ../dist and updates the line to be

Requires-Dist: mydep

meaning that ../dist can be used as a wheelhouse directory when making installs inside docker containers or generating pex files.

pex example

poetry workspace run -- poetry build --format=wheel
mv packages/*/dist/*.whl dist
python tools/fix_poetry_wheels.py
pex \
  --find-links=dist \
  --script=gunicorn \
  --output-file=myapp.pex \
  myapp

⚖️ Licence

This code is licensed under the [MIT licence][mit_licence].

import pathlib
import zipfile
import tempfile
import os
import re
RE = re.compile(r"^Requires-Dist: (.*) @ \..*$", re.MULTILINE)
path = pathlib.Path(__file__).parent.parent / "dist"
for filename in path.glob("*.whl"):
tmpfd, tmpname = tempfile.mkstemp(dir=filename.parent)
with zipfile.ZipFile(filename, "r") as inzip:
with zipfile.ZipFile(tmpname, 'w') as outzip:
outzip.comment = inzip.comment
for file in inzip.infolist():
if not file.filename.endswith(".dist-info/METADATA"):
outzip.writestr(file.filename, inzip.read(file.filename))
continue
metadata = RE.sub(r"Requires-Dist: \1", inzip.read(file.filename).decode())
outzip.writestr(file.filename, metadata)
os.remove(filename)
os.rename(tmpname, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment