Skip to content

Commit 2b403a5

Browse files
authored
Post-#99 updates (#102)
* Fix .dist-info checks in unmake-variant * Harmonize .dist-info file checks across commands * Use modern entry point API in commands
1 parent af8fa79 commit 2b403a5

File tree

5 files changed

+42
-47
lines changed

5 files changed

+42
-47
lines changed

variantlib/commands/analyze_wheel.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def analyze_wheel(args: list[str]) -> None:
6969

7070
with zipfile.ZipFile(input_file, "r") as zip_file:
7171
for name in zip_file.namelist():
72-
if name.endswith(f".dist-info/{VARIANT_DIST_INFO_FILENAME}"):
72+
components = name.split("/", 2)
73+
if (
74+
len(components) == 2
75+
and components[0].endswith(".dist-info")
76+
and components[1] == VARIANT_DIST_INFO_FILENAME
77+
):
7378
variant_dist_info = VariantDistInfo(zip_file.read(name), variant_hash)
7479
break
7580
else:

variantlib/commands/generate_index_json.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ def generate_index_json(args: list[str]) -> None:
6767
with zipfile.ZipFile(wheel, "r") as zip_file:
6868
# Find the variant dist-info file
6969
for name in zip_file.namelist():
70-
if name.endswith(f".dist-info/{VARIANT_DIST_INFO_FILENAME}"):
70+
components = name.split("/", 2)
71+
if (
72+
len(components) == 2
73+
and components[0].endswith(".dist-info")
74+
and components[1] == VARIANT_DIST_INFO_FILENAME
75+
):
7176
variant_dist_info = VariantDistInfo(zip_file.read(name), vhash)
7277
break
7378
else:

variantlib/commands/make_variant.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,15 @@ def _make_variant(
163163
):
164164
for file_info in input_zip.infolist():
165165
components = file_info.filename.split("/", 2)
166-
with (
167-
input_zip.open(file_info, "r") as input_file,
168-
):
169-
if (
170-
len(components) != 2
171-
or not components[0].endswith(".dist-info")
172-
or components[1] not in ("RECORD", VARIANT_DIST_INFO_FILENAME)
173-
):
174-
with output_zip.open(file_info, "w") as output_file:
175-
shutil.copyfileobj(input_file, output_file)
166+
is_dist_info = len(components) == 2 and components[0].endswith(".dist-info")
176167

177-
elif components[1] == VARIANT_DIST_INFO_FILENAME:
178-
# If a wheel dist-info file exists already, discard the existing
179-
# copy.
180-
continue
168+
if is_dist_info and components[1] == VARIANT_DIST_INFO_FILENAME:
169+
# If a wheel dist-info file exists already, discard the existing
170+
# copy.
171+
continue
181172

182-
else: # RECORD
183-
assert components[1] == "RECORD"
173+
with input_zip.open(file_info, "r") as input_file:
174+
if is_dist_info and components[1] == "RECORD":
184175
# First, add new dist-info file prior to RECORD (not strictly
185176
# required, but a nice convention).
186177
dist_info_path = f"{components[0]}/{VARIANT_DIST_INFO_FILENAME}"
@@ -208,5 +199,8 @@ def _make_variant(
208199
f"{len(dist_info_data)}\n"
209200
).encode()
210201
)
202+
else:
203+
with output_zip.open(file_info, "w") as output_file:
204+
shutil.copyfileobj(input_file, output_file)
211205

212206
logger.info("Variant Wheel Created: `%s`", output_filepath.resolve())

variantlib/commands/unmake_variant.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,26 @@ def _unmake_variant(
9494
):
9595
for file_info in input_zip.infolist():
9696
components = file_info.filename.split("/", 2)
97+
is_dist_info = len(components) == 2 and components[0].endswith(".dist-info")
9798

98-
if (
99-
".dist-info/" in file_info.filename
100-
and components[1] == VARIANT_DIST_INFO_FILENAME
101-
):
99+
if is_dist_info and components[1] == VARIANT_DIST_INFO_FILENAME:
102100
# This is the variant.json file, we skip it.
103101
continue
104102

105-
with input_zip.open(file_info, "r") as input_file:
106-
if (
107-
len(components) != 2
108-
or not components[0].endswith(".dist-info")
109-
or components[1] not in ("RECORD", VARIANT_DIST_INFO_FILENAME)
110-
):
111-
with output_zip.open(file_info, "w") as output_file:
112-
shutil.copyfileobj(input_file, output_file)
113-
114-
else:
115-
assert components[1] == "RECORD"
103+
with (
104+
input_zip.open(file_info, "r") as input_file,
105+
output_zip.open(file_info, "w") as output_file,
106+
):
107+
if is_dist_info and components[1] == "RECORD":
116108
# Update RECORD to remove the checksum.
117-
with output_zip.open(file_info, "w") as output_file:
118-
variant_dist_info_path = (
119-
f"{components[0]}/{VARIANT_DIST_INFO_FILENAME}"
120-
).encode()
121-
for line in input_file:
122-
rec_filename, sha256, size = line.split(b",")
123-
if rec_filename != variant_dist_info_path:
124-
output_file.write(line)
109+
variant_dist_info_path = (
110+
f"{components[0]}/{VARIANT_DIST_INFO_FILENAME}"
111+
).encode()
112+
for line in input_file:
113+
rec_filename, sha256, size = line.split(b",")
114+
if rec_filename != variant_dist_info_path:
115+
output_file.write(line)
116+
else:
117+
shutil.copyfileobj(input_file, output_file)
125118

126119
logger.info("Variant Wheel Created: `%s`", output_filepath.resolve())

variantlib/commands/utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66

77
if sys.version_info >= (3, 10):
88
from importlib.metadata import EntryPoint
9-
from importlib.metadata import distributions
9+
from importlib.metadata import entry_points
1010
else:
1111
from importlib_metadata import EntryPoint
12-
from importlib_metadata import distributions
12+
from importlib_metadata import entry_points
1313

1414

1515
def get_registered_commands(group: str) -> dict[str, EntryPoint]:
1616
"""Collect entry points for the given group, preserving original names."""
1717
commands = {}
18-
for dist in distributions():
19-
for ep in dist.entry_points:
20-
if ep.group == group:
21-
commands[ep.name] = ep
18+
for ep in entry_points().select(group=group):
19+
commands[ep.name] = ep
2220
return commands

0 commit comments

Comments
 (0)