Skip to content

Commit 44e5e7d

Browse files
committed
refactor: tags run on one wheel each (simpler impl)
1 parent b5f19bc commit 44e5e7d

File tree

3 files changed

+110
-114
lines changed

3 files changed

+110
-114
lines changed

src/wheel/cli/__init__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ def convert_f(args):
3434
def tags_f(args):
3535
from .tags import tags
3636

37-
for name in tags(
38-
args.wheel,
39-
args.python_tag and args.python_tag.split("."),
40-
args.abi_tag and args.abi_tag.split("."),
41-
args.platform_tag and args.platform_tag.split("."),
42-
args.build,
43-
args.remove,
44-
):
37+
names = (
38+
tags(
39+
wheel,
40+
args.python_tag and args.python_tag.split("."),
41+
args.abi_tag and args.abi_tag.split("."),
42+
args.platform_tag and args.platform_tag.split("."),
43+
args.build,
44+
args.remove,
45+
)
46+
for wheel in args.wheel
47+
)
48+
49+
for name in names:
4550
print(name)
4651

4752

src/wheel/cli/tags.py

Lines changed: 82 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
from ..wheelfile import WheelFile
77
from .pack import read_tags, set_build_number
88

9-
try:
10-
from typing import Iterator
11-
except ImportError:
12-
pass
13-
149

1510
def compute_tags(original_tags: list[str], new_tags: list[str] | None) -> list[str]:
1611
"""Add or replace tags."""
@@ -25,13 +20,13 @@ def compute_tags(original_tags: list[str], new_tags: list[str] | None) -> list[s
2520

2621

2722
def tags(
28-
wheels: list[str],
23+
wheel: str,
2924
python_tags: list[str] | None = None,
3025
abi_tags: list[str] | None = None,
3126
platform_tags: list[str] | None = None,
3227
build_number: int | None = None,
3328
remove: bool = False,
34-
) -> Iterator[str]:
29+
) -> str:
3530
"""Change the tags on a wheel file.
3631
3732
The tags are left unchanged if they are not specified. To specify "none",
@@ -44,93 +39,89 @@ def tags(
4439
:param build_number: The build number to set.
4540
:param remove: Remove the original wheel.
4641
"""
47-
48-
for wheel in wheels:
49-
with WheelFile(wheel, "r") as f:
50-
wheel_info = f.read(f.dist_info_path + "/WHEEL")
51-
52-
original_wheel_name = os.path.basename(f.filename)
53-
namever = f.parsed_filename.group("namever")
54-
build = f.parsed_filename.group("build")
55-
original_python_tags = f.parsed_filename.group("pyver").split(".")
56-
original_abi_tags = f.parsed_filename.group("abi").split(".")
57-
orignial_plat_tags = f.parsed_filename.group("plat").split(".")
58-
59-
tags, existing_build_number = read_tags(wheel_info)
60-
61-
impls = {tag.split("-")[0] for tag in tags}
62-
abivers = {tag.split("-")[1] for tag in tags}
63-
platforms = {tag.split("-")[2] for tag in tags}
64-
65-
if impls != set(original_python_tags):
66-
raise AssertionError(f"{impls} != {original_python_tags}")
67-
68-
if abivers != set(original_abi_tags):
69-
raise AssertionError(f"{abivers} != {original_abi_tags}")
70-
71-
if platforms != set(orignial_plat_tags):
72-
raise AssertionError(f"{platforms} != {orignial_plat_tags}")
73-
74-
if existing_build_number != build:
75-
raise AssertionError(
76-
f"Incorrect filename '{build}' & "
77-
f"*.dist-info/WHEEL '{existing_build_number}' build numbers"
42+
with WheelFile(wheel, "r") as f:
43+
wheel_info = f.read(f.dist_info_path + "/WHEEL")
44+
45+
original_wheel_name = os.path.basename(f.filename)
46+
namever = f.parsed_filename.group("namever")
47+
build = f.parsed_filename.group("build")
48+
original_python_tags = f.parsed_filename.group("pyver").split(".")
49+
original_abi_tags = f.parsed_filename.group("abi").split(".")
50+
orignial_plat_tags = f.parsed_filename.group("plat").split(".")
51+
52+
tags, existing_build_number = read_tags(wheel_info)
53+
54+
impls = {tag.split("-")[0] for tag in tags}
55+
abivers = {tag.split("-")[1] for tag in tags}
56+
platforms = {tag.split("-")[2] for tag in tags}
57+
58+
if impls != set(original_python_tags):
59+
raise AssertionError(f"{impls} != {original_python_tags}")
60+
61+
if abivers != set(original_abi_tags):
62+
raise AssertionError(f"{abivers} != {original_abi_tags}")
63+
64+
if platforms != set(orignial_plat_tags):
65+
raise AssertionError(f"{platforms} != {orignial_plat_tags}")
66+
67+
if existing_build_number != build:
68+
raise AssertionError(
69+
f"Incorrect filename '{build}' & "
70+
f"*.dist-info/WHEEL '{existing_build_number}' build numbers"
71+
)
72+
73+
# Start changing as needed
74+
if build_number is not None:
75+
build = str(build_number)
76+
77+
final_python_tags = compute_tags(original_python_tags, python_tags)
78+
final_abi_tags = compute_tags(original_abi_tags, abi_tags)
79+
final_plat_tags = compute_tags(orignial_plat_tags, platform_tags)
80+
81+
final_tags = [
82+
".".join(sorted(final_python_tags)),
83+
".".join(sorted(final_abi_tags)),
84+
".".join(sorted(final_plat_tags)),
85+
]
86+
87+
if build:
88+
final_tags.insert(0, build)
89+
final_tags.insert(0, namever)
90+
91+
final_wheel_name = "-".join(final_tags) + ".whl"
92+
93+
if original_wheel_name != final_wheel_name:
94+
tags = [
95+
f"{a}-{b}-{c}"
96+
for a, b, c in itertools.product(
97+
final_python_tags, final_abi_tags, final_plat_tags
7898
)
79-
80-
# Start changing as needed
81-
if build_number is not None:
82-
build = str(build_number)
83-
84-
final_python_tags = compute_tags(original_python_tags, python_tags)
85-
final_abi_tags = compute_tags(original_abi_tags, abi_tags)
86-
final_plat_tags = compute_tags(orignial_plat_tags, platform_tags)
87-
88-
final_tags = [
89-
".".join(sorted(final_python_tags)),
90-
".".join(sorted(final_abi_tags)),
91-
".".join(sorted(final_plat_tags)),
9299
]
93100

94-
if build:
95-
final_tags.insert(0, build)
96-
final_tags.insert(0, namever)
97-
98-
final_wheel_name = "-".join(final_tags) + ".whl"
99-
100-
if original_wheel_name != final_wheel_name:
101-
tags = [
102-
f"{a}-{b}-{c}"
103-
for a, b, c in itertools.product(
104-
final_python_tags, final_abi_tags, final_plat_tags
105-
)
106-
]
107-
108-
original_wheel_path = os.path.join(
109-
os.path.dirname(f.filename), original_wheel_name
110-
)
111-
final_wheel_path = os.path.join(
112-
os.path.dirname(f.filename), final_wheel_name
113-
)
114-
115-
with WheelFile(original_wheel_path, "r") as fin, WheelFile(
116-
final_wheel_path, "w"
117-
) as fout:
118-
fout.comment = fin.comment # preserve the comment
119-
for item in fin.infolist():
120-
if item.filename == f.dist_info_path + "/RECORD":
121-
continue
122-
if item.filename == f.dist_info_path + "/WHEEL":
123-
content = fin.read(item)
124-
content = set_tags(content, tags)
125-
content = set_build_number(content, build)
126-
fout.writestr(item, content)
127-
else:
128-
fout.writestr(item, fin.read(item))
129-
130-
if remove:
131-
os.remove(original_wheel_path)
132-
133-
yield final_wheel_name
101+
original_wheel_path = os.path.join(
102+
os.path.dirname(f.filename), original_wheel_name
103+
)
104+
final_wheel_path = os.path.join(os.path.dirname(f.filename), final_wheel_name)
105+
106+
with WheelFile(original_wheel_path, "r") as fin, WheelFile(
107+
final_wheel_path, "w"
108+
) as fout:
109+
fout.comment = fin.comment # preserve the comment
110+
for item in fin.infolist():
111+
if item.filename == f.dist_info_path + "/RECORD":
112+
continue
113+
if item.filename == f.dist_info_path + "/WHEEL":
114+
content = fin.read(item)
115+
content = set_tags(content, tags)
116+
content = set_build_number(content, build)
117+
fout.writestr(item, content)
118+
else:
119+
fout.writestr(item, fin.read(item))
120+
121+
if remove:
122+
os.remove(original_wheel_path)
123+
124+
return final_wheel_name
134125

135126

136127
def set_tags(in_string: bytes, tags: list[str]) -> bytes:

tests/cli/test_tags.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def wheelpath(tmpdir):
2323

2424

2525
def test_tags_no_args(wheelpath):
26-
(newname,) = tags([str(wheelpath)])
26+
newname = tags(str(wheelpath))
2727
assert TESTWHEEL_NAME == newname
2828
assert wheelpath.exists()
2929

3030

3131
def test_python_tags(wheelpath):
32-
(newname,) = tags([str(wheelpath)], python_tags=["py3"])
32+
newname = tags(str(wheelpath), python_tags=["py3"])
3333
assert TESTWHEEL_NAME.replace("py2.py3", "py3") == newname
3434
output_file = wheelpath.dirpath(newname)
3535
with WheelFile(str(output_file)) as f:
@@ -40,71 +40,71 @@ def test_python_tags(wheelpath):
4040
)
4141
output_file.remove()
4242

43-
(newname,) = tags([str(wheelpath)], python_tags=["py2.py3"])
43+
newname = tags(str(wheelpath), python_tags=["py2.py3"])
4444
assert TESTWHEEL_NAME == newname
4545

46-
(newname,) = tags([str(wheelpath)], python_tags=["", "py4"], remove=True)
46+
newname = tags(str(wheelpath), python_tags=["", "py4"], remove=True)
4747
assert not wheelpath.exists()
4848
assert TESTWHEEL_NAME.replace("py2.py3", "py2.py3.py4") == newname
4949
output_file = wheelpath.dirpath(newname)
5050
output_file.remove()
5151

5252

5353
def test_abi_tags(wheelpath):
54-
(newname,) = tags([str(wheelpath)], abi_tags=["cp33m"])
54+
newname = tags(str(wheelpath), abi_tags=["cp33m"])
5555
assert TESTWHEEL_NAME.replace("none", "cp33m") == newname
5656
output_file = wheelpath.dirpath(newname)
5757
output_file.remove()
5858

59-
(newname,) = tags([str(wheelpath)], abi_tags=["abi3", "cp33m"])
59+
newname = tags(str(wheelpath), abi_tags=["abi3", "cp33m"])
6060
assert TESTWHEEL_NAME.replace("none", "abi3.cp33m") == newname
6161
output_file = wheelpath.dirpath(newname)
6262
output_file.remove()
6363

64-
(newname,) = tags([str(wheelpath)], abi_tags=["none"])
64+
newname = tags(str(wheelpath), abi_tags=["none"])
6565
assert TESTWHEEL_NAME == newname
6666

67-
(newname,) = tags([str(wheelpath)], abi_tags=["", "abi3", "cp33m"], remove=True)
67+
newname = tags(str(wheelpath), abi_tags=["", "abi3", "cp33m"], remove=True)
6868
assert not wheelpath.exists()
6969
assert TESTWHEEL_NAME.replace("none", "abi3.cp33m.none") == newname
7070
output_file = wheelpath.dirpath(newname)
7171
output_file.remove()
7272

7373

7474
def test_plat_tags(wheelpath):
75-
(newname,) = tags([str(wheelpath)], platform_tags=["linux_x86_64"])
75+
newname = tags(str(wheelpath), platform_tags=["linux_x86_64"])
7676
assert TESTWHEEL_NAME.replace("any", "linux_x86_64") == newname
7777
output_file = wheelpath.dirpath(newname)
7878
assert output_file.exists()
7979
output_file.remove()
8080

81-
(newname,) = tags([str(wheelpath)], platform_tags=["linux_x86_64", "win32"])
81+
newname = tags(str(wheelpath), platform_tags=["linux_x86_64", "win32"])
8282
assert TESTWHEEL_NAME.replace("any", "linux_x86_64.win32") == newname
8383
output_file = wheelpath.dirpath(newname)
8484
assert output_file.exists()
8585
output_file.remove()
8686

87-
(newname,) = tags([str(wheelpath)], platform_tags=["", "linux_x86_64", "win32"])
87+
newname = tags(str(wheelpath), platform_tags=["", "linux_x86_64", "win32"])
8888
assert TESTWHEEL_NAME.replace("any", "any.linux_x86_64.win32") == newname
8989
output_file = wheelpath.dirpath(newname)
9090
assert output_file.exists()
9191
output_file.remove()
9292

93-
(newname,) = tags([str(wheelpath)], platform_tags=["any"])
93+
newname = tags(str(wheelpath), platform_tags=["any"])
9494
assert TESTWHEEL_NAME == newname
9595

9696

9797
def test_build_number(wheelpath):
98-
(newname,) = tags([str(wheelpath)], build_number=1)
98+
newname = tags(str(wheelpath), build_number=1)
9999
assert TESTWHEEL_NAME.replace("-py2", "-1-py2") == newname
100100
output_file = wheelpath.dirpath(newname)
101101
assert output_file.exists()
102102
output_file.remove()
103103

104104

105105
def test_multi_tags(wheelpath):
106-
(newname,) = tags(
107-
[str(wheelpath)],
106+
newname = tags(
107+
str(wheelpath),
108108
platform_tags=["linux_x86_64"],
109109
python_tags=["", "py4"],
110110
build_number=1,

0 commit comments

Comments
 (0)