Skip to content

Commit 6364a2c

Browse files
fix pylock.toml export dropping wheels when the sdist sorts between wheels (#379)
1 parent 36ab718 commit 6364a2c

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
@@ -395,24 +394,16 @@ def add_file_info(
395394
package.source_reference or "PyPI",
396395
).files
397396
}
398-
artifacts = {
399-
k: list(v)
400-
for k, v in itertools.groupby(
401-
package.files,
402-
key=(
403-
lambda x: (
404-
"wheel" if x["file"].endswith(".whl") else "sdist"
405-
)
406-
),
407-
)
408-
}
409-
410-
sdist_files = list(artifacts.get("sdist", []))
397+
sdist_files = [
398+
f for f in package.files if not f["file"].endswith(".whl")
399+
]
411400
for sdist in sdist_files:
412401
sdist_table = inline_table()
413402
data["sdist"] = sdist_table
414403
add_file_info(sdist_table, sdist, pool_info[sdist["file"]])
415-
if wheels := list(artifacts.get("wheel", [])):
404+
if wheels := [
405+
f for f in package.files if f["file"].endswith(".whl")
406+
]:
416407
wheel_array = array()
417408
data["wheels"] = wheel_array
418409
wheel_array.multiline(True)

‎tests/test_exporter_pylock_toml.py‎

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

835835

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

0 commit comments

Comments
 (0)