Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow PEP 425 suggestions on distribution preference. #640

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pex/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def raw_version(self):
def version(self):
return parse_version(self.raw_version)

@property
def supported_tags(self):
return NotImplementedError

def satisfies(self, requirement, allow_prereleases=None):
"""Determine whether this package matches the requirement.

Expand Down Expand Up @@ -138,6 +142,10 @@ def name(self):
def raw_version(self):
return safe_version(self._raw_version)

@property
def supported_tags(self):
return None

# SourcePackages are always compatible as they can be translated to a distribution.
def compatible(self, supported_tags):
return True
Expand Down
11 changes: 11 additions & 0 deletions pex/sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ def package_type_precedence(cls, package, precedence=DEFAULT_PACKAGE_PRECEDENCE)
# If we do not recognize the package, it gets lowest precedence
return -1

@classmethod
def package_platform_tag_precedence(cls, package):
# Give all non 'any' tags higher precedence
supported_tags = package.supported_tags or []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - this all looks great. The existing supported_tags property for BinaryPackage is unsuitable for complete sorting, but this is a step forward - at least platform specific packages now win over non. I've filed #642 to track fixing the rest.

platforms = set([tags[2] for tags in supported_tags])
if 'any' in platforms:
return -1
else:
return 0

@classmethod
def package_precedence(cls, package, precedence=DEFAULT_PACKAGE_PRECEDENCE):
return (
package.version, # highest version
cls.package_type_precedence(package, precedence=precedence), # type preference
cls.package_platform_tag_precedence(package), # platform preference
package.local) # prefer not fetching over the wire

def __init__(self, precedence=None):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from pex.third_party.pkg_resources import get_build_platform


def test_wheel_precedence():
linux_whl = WheelPackage('protobuf-3.6.1-cp27-cp27mu-manylinux1_x86_64.whl')
none_whl = WheelPackage('protobuf-3.6.1-py2.py3-none-any.whl')

assert Sorter().sort([linux_whl, none_whl]) == [linux_whl, none_whl]
assert Sorter().sort([none_whl, linux_whl]) == [linux_whl, none_whl]


def test_package_precedence():
source = SourcePackage('psutil-0.6.1.tar.gz')
egg = EggPackage('psutil-0.6.1-py2.6.egg')
Expand All @@ -20,7 +28,7 @@ def test_package_precedence():
# overridden precedence
PRECEDENCE = (EggPackage, WheelPackage)
assert Sorter.package_precedence(source, PRECEDENCE) == (
source.version, -1, True) # unknown rank
source.version, -1, 0, True) # unknown rank
assert Sorter.package_precedence(whl, PRECEDENCE) > Sorter.package_precedence(
source, PRECEDENCE)
assert Sorter.package_precedence(egg, PRECEDENCE) > Sorter.package_precedence(
Expand Down