Skip to content

Commit

Permalink
Simplify and clarify resolver platform expansion.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Oct 4, 2018
1 parent c9f0618 commit f72cc11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
87 changes: 46 additions & 41 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,52 +165,57 @@ class Resolver(object):
class Error(Exception): pass

@staticmethod
def _expand_and_maybe_adjust_platform(interpreter, platform=None):
# Adjusts `platform` if it is 'current' and does not match the given `interpreter` platform.
cur_plat = Platform.current()

given_platform = Platform.create(platform or 'current')
if cur_plat.platform != given_platform.platform:
# IE: Say we're on OSX and platform was 'linux-x86_64' or 'linux_x86_64-cp-27-cp27mu'.
return given_platform

if (interpreter.identity.abbr_impl,
interpreter.identity.impl_ver,
interpreter.identity.abi_tag) == (cur_plat.impl, cur_plat.version, cur_plat.abi):
# IE: Say we're on Linux and platform was 'current' or 'linux-x86_64' or
# 'linux_x86_64-cp-27-cp27mu'and the current extended platform info matches the given
# interpreter exactly.
return cur_plat

# Otherwise we need to adjust the platform to match a local interpreter different from the
# currently executing interpreter.
adjusted_platform = Platform(platform=cur_plat.platform,
impl=interpreter.identity.abbr_impl,
version=interpreter.identity.impl_ver,
abi=interpreter.identity.abi_tag)

TRACER.log("""
Modifying given platform of {given_platform!r}:
Using the current platform of {current_platform!r}
Under current interpreter {current_interpreter!r}
To match given interpreter {given_interpreter!r}.
Calculated platform: {calculated_platform!r}""".format(
given_platform=given_platform,
current_platform=cur_plat,
current_interpreter=PythonInterpreter.get(),
given_interpreter=interpreter,
calculated_platform=adjusted_platform),
V=9
)
def _maybe_expand_platform(interpreter, platform=None):
# Expands `platform` if it is 'current' and abbreviated.
#
# IE: If we're on linux and handed a platform of `None`, 'current', or 'linux_x86_64', we expand
# the platform to an extended platform matching the given interpreter's abi info, eg:
# 'linux_x86_64-cp-27-cp27mu'.

return adjusted_platform
cur_plat = Platform.current()
def expand_platform():
expanded_platform = Platform(platform=cur_plat.platform,
impl=interpreter.identity.abbr_impl,
version=interpreter.identity.impl_ver,
abi=interpreter.identity.abi_tag)
TRACER.log("""
Modifying given platform of {given_platform!r}:
Using the current platform of {current_platform!r}
Under current interpreter {current_interpreter!r}
To match given interpreter {given_interpreter!r}.
Calculated platform: {calculated_platform!r}""".format(
given_platform=platform,
current_platform=cur_plat,
current_interpreter=PythonInterpreter.get(),
given_interpreter=interpreter,
calculated_platform=expanded_platform),
V=9
)
return expanded_platform

if platform in (None, 'current'):
# Always expand the default local (abbreviated) platform to the given interpreter.
return expand_platform()
else:
given_platform = Platform.create(platform)
if given_platform.is_extended:
# Always respect an explicit extended platform.
return given_platform
elif given_platform.platform != cur_plat.platform:
# IE: Say we're on OSX and platform was 'linux-x86_64'; we can't expand a non-local
# platform so we leave as-is.
return given_platform
else:
# IE: Say we're on 64 bit linux and platform was 'linux-x86_64'; ie: the abbreviated local
# platform.
return expand_platform()

def __init__(self, allow_prereleases=None, interpreter=None, platform=None,
pkg_blacklist=None, use_manylinux=None):
self._interpreter = interpreter or PythonInterpreter.get()
self._platform = self._expand_and_maybe_adjust_platform(self._interpreter, platform)
self._platform = self._maybe_expand_platform(self._interpreter, platform)
self._allow_prereleases = allow_prereleases
self._blacklist = pkg_blacklist.copy() if pkg_blacklist else {}
self._supported_tags = self._platform.supported_tags(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from twitter.common.contextutil import temporary_dir

from pex import resolver
from pex.compatibility import nested
from pex.compatibility import nested, to_bytes
from pex.environment import PEXEnvironment
from pex.installer import EggInstaller, WheelInstaller
from pex.interpreter import PythonInterpreter
Expand Down Expand Up @@ -161,4 +161,4 @@ def bad_interpreter(include_site_extras=True):
# Verify this all worked under the previously problematic pkg_resources-reported platform.
release, _, _ = platform.mac_ver()
major_minor = '.'.join(release.split('.')[:2])
assert b'macosx-{}-intel'.format(major_minor) == stdout.strip()
assert to_bytes('macosx-{}-intel'.format(major_minor)) == stdout.strip()

0 comments on commit f72cc11

Please sign in to comment.