Skip to content

Commit 16dd63d

Browse files
committed
fix pylock.toml export dropping wheels when the sdist sorts between wheels
1 parent cc06037 commit 16dd63d

2 files changed

Lines changed: 88 additions & 15 deletions

File tree

‎src/poetry_plugin_export/exporter.py‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import contextlib
4-
import itertools
54
import urllib.parse
65

76
from datetime import datetime
@@ -387,24 +386,16 @@ def add_file_info(
387386
package.source_reference or "PyPI",
388387
).files
389388
}
390-
artifacts = {
391-
k: list(v)
392-
for k, v in itertools.groupby(
393-
package.files,
394-
key=(
395-
lambda x: (
396-
"wheel" if x["file"].endswith(".whl") else "sdist"
397-
)
398-
),
399-
)
400-
}
401-
402-
sdist_files = list(artifacts.get("sdist", []))
389+
sdist_files = [
390+
f for f in package.files if not f["file"].endswith(".whl")
391+
]
403392
for sdist in sdist_files:
404393
sdist_table = inline_table()
405394
data["sdist"] = sdist_table
406395
add_file_info(sdist_table, sdist, pool_info[sdist["file"]])
407-
if wheels := list(artifacts.get("wheel", [])):
396+
if wheels := [
397+
f for f in package.files if f["file"].endswith(".whl")
398+
]:
408399
wheel_array = array()
409400
data["wheels"] = wheel_array
410401
wheel_array.multiline(True)

‎tests/test_exporter_pylock_toml.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,88 @@ def test_export_pypi_dependencies_multiple_wheels(
832832
assert content == expected
833833

834834

835+
def test_export_pypi_dependencies_sdist_sorted_between_wheels(
836+
tmp_path: Path, poetry: Poetry
837+
) -> None:
838+
# The lock file sorts files by name, so the sdist can end up between wheels,
839+
# e.g. if the distribution name of some wheels is normalized differently.
840+
lock_data = {
841+
"package": [
842+
{
843+
"name": "foo.bar",
844+
"version": "1.0",
845+
"optional": False,
846+
"python-versions": "*",
847+
"groups": [MAIN_GROUP],
848+
"files": [
849+
{
850+
"file": "foo.bar-1.0-py2-none-any.whl",
851+
"hash": "sha256:abcdef1234567891",
852+
},
853+
{
854+
"file": "foo.bar-1.0.tar.gz",
855+
"hash": "sha256:0123456789abcdef",
856+
},
857+
{
858+
"file": "foo_bar-1.0-py3-none-any.whl",
859+
"hash": "sha256:abcdef1234567890",
860+
},
861+
],
862+
},
863+
],
864+
"metadata": {"lock-version": "2.1"},
865+
}
866+
poetry.locker.mock_lock_data(lock_data) # type: ignore[attr-defined]
867+
868+
foo_bar = Package("foo.bar", "1.0")
869+
foo_bar.files = [
870+
{
871+
"file": "foo.bar-1.0-py2-none-any.whl",
872+
"hash": "sha256:abcdef1234567891",
873+
"url": "https://example.org/foo.bar-1.0-py2-none-any.whl",
874+
},
875+
{
876+
"file": "foo.bar-1.0.tar.gz",
877+
"hash": "sha256:0123456789abcdef",
878+
"url": "https://example.org/foo.bar-1.0.tar.gz",
879+
},
880+
{
881+
"file": "foo_bar-1.0-py3-none-any.whl",
882+
"hash": "sha256:abcdef1234567890",
883+
"url": "https://example.org/foo_bar-1.0-py3-none-any.whl",
884+
},
885+
]
886+
poetry.pool.repository("PyPI").add_package(foo_bar)
887+
888+
exporter = Exporter(poetry, NullIO())
889+
890+
exporter.export("pylock.toml", tmp_path, "pylock.toml")
891+
892+
with (tmp_path / "pylock.toml").open(encoding="utf-8") as f:
893+
content = f.read()
894+
895+
expected = """\
896+
lock-version = "1.0"
897+
created-by = "poetry-plugin-export"
898+
899+
[[packages]]
900+
name = "foo-bar"
901+
version = "1.0"
902+
index = "https://pypi.org/simple"
903+
sdist = {name = "foo.bar-1.0.tar.gz", url = "https://example.org/foo.bar-1.0.tar.gz", hashes = {sha256 = "0123456789abcdef"}}
904+
wheels = [
905+
{name = "foo.bar-1.0-py2-none-any.whl", url = "https://example.org/foo.bar-1.0-py2-none-any.whl", hashes = {sha256 = "abcdef1234567891"}},
906+
{name = "foo_bar-1.0-py3-none-any.whl", url = "https://example.org/foo_bar-1.0-py3-none-any.whl", hashes = {sha256 = "abcdef1234567890"}},
907+
]
908+
909+
[tool.poetry-plugin-export]
910+
groups = ["main"]
911+
extras = []
912+
"""
913+
914+
assert content == expected
915+
916+
835917
def test_export_legacy_repo_dependencies(tmp_path: Path, poetry: Poetry) -> None:
836918
lock_data = {
837919
"package": [

0 commit comments

Comments
 (0)