Skip to content

Commit f6b56e5

Browse files
committed
Add support for wheel build tags
At the moment, pip doesn't support ordering candidate installations while considering build tags. This is meant to start adding that support. According to the PEP, the first contiguous set of integers in the build tag should be sorted numerically. After that, they should be sorted alphabetically.
1 parent a2bf077 commit f6b56e5

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

pip/index.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def _candidate_sort_key(self, candidate):
278278
with the same version, would have to be considered equal
279279
"""
280280
support_num = len(self.valid_tags)
281+
build_tag = tuple()
281282
if candidate.location.is_wheel:
282283
# can raise InvalidWheelFilename
283284
wheel = Wheel(candidate.location.filename)
@@ -287,9 +288,13 @@ def _candidate_sort_key(self, candidate):
287288
"can't be sorted." % wheel.filename
288289
)
289290
pri = -(wheel.support_index_min(self.valid_tags))
291+
if wheel.build_tag is not None:
292+
match = re.match('^(\d+)(.*)$', wheel.build_tag)
293+
build_tag_groups = match.groups()
294+
build_tag = (int(build_tag_groups[0]), build_tag_groups[1])
290295
else: # sdist
291296
pri = -(support_num)
292-
return (candidate.version, pri)
297+
return (candidate.version, build_tag, pri)
293298

294299
def _validate_secure_origin(self, logger, location):
295300
# Determine if this url used a secure transport mechanism

pip/wheel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class Wheel(object):
482482

483483
wheel_file_re = re.compile(
484484
r"""^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))
485-
((-(?P<build>\d.*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
485+
((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
486486
\.whl|\.dist-info)$""",
487487
re.VERBOSE
488488
)
@@ -501,6 +501,7 @@ def __init__(self, filename):
501501
# we'll assume "_" means "-" due to wheel naming scheme
502502
# (https://github.com/pypa/pip/issues/1150)
503503
self.version = wheel_info.group('ver').replace('_', '-')
504+
self.build_tag = wheel_info.group('build')
504505
self.pyversions = wheel_info.group('pyver').split('.')
505506
self.abis = wheel_info.group('abi').split('.')
506507
self.plats = wheel_info.group('plat').split('.')
Binary file not shown.

tests/unit/test_finder.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,31 @@ def test_link_sorting(self):
251251

252252
assert links == results == results2, results2
253253

254+
def test_link_sorting_wheels_with_build_tags(self):
255+
"""Verify build tags affect sorting."""
256+
links = [
257+
InstallationCandidate(
258+
"simplewheel",
259+
"2.0",
260+
Link("simplewheel-2.0-1-py2.py3-none-any.whl"),
261+
),
262+
InstallationCandidate(
263+
"simplewheel",
264+
"2.0",
265+
Link("simplewheel-2.0-py2.py3-none-any.whl"),
266+
),
267+
InstallationCandidate(
268+
"simplewheel",
269+
"1.0",
270+
Link("simplewheel-1.0-py2.py3-none-any.whl"),
271+
),
272+
]
273+
finder = PackageFinder([], [], session=PipSession())
274+
results = sorted(links, key=finder._candidate_sort_key, reverse=True)
275+
results2 = sorted(reversed(links), key=finder._candidate_sort_key,
276+
reverse=True)
277+
assert links == results == results2, results2
278+
254279

255280
def test_finder_priority_file_over_page(data):
256281
"""Test PackageFinder prefers file links over equivalent page links"""

0 commit comments

Comments
 (0)