Skip to content

Commit

Permalink
Fix PEXEnvironment platform determination.
Browse files Browse the repository at this point in the history
Have this use pex intrinsics instead of pkg_resources which is known
to report bad values on Apple-shipped inerpreters.

Fixes pex-tool#523
  • Loading branch information
jsirois committed Oct 1, 2018
1 parent 6d2dc8e commit 4c8120c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
7 changes: 4 additions & 3 deletions pex/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ def __init__(self, pex, pex_info, interpreter=None, **kw):
self._interpreter = interpreter or PythonInterpreter.get()
self._inherit_path = pex_info.inherit_path
self._supported_tags = []

platform = Platform.current()
super(PEXEnvironment, self).__init__(
search_path=[] if pex_info.inherit_path == 'false' else sys.path,
platform=platform.platform,
**kw
)
self._supported_tags.extend(
Platform.create(self.platform).supported_tags(self._interpreter)
)
self._supported_tags.extend(platform.supported_tags(self._interpreter))
TRACER.log(
'E: tags for %r x %r -> %s' % (self.platform, self._interpreter, self._supported_tags),
V=9
Expand Down
48 changes: 43 additions & 5 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
import platform
import subprocess
from contextlib import contextmanager

import pytest
from twitter.common.contextutil import temporary_dir

from pex import resolver
from pex.compatibility import nested
from pex.environment import PEXEnvironment
from pex.installer import EggInstaller, WheelInstaller
from pex.interpreter import PythonInterpreter
from pex.package import SourcePackage
from pex.pex import PEX
from pex.pex_builder import PEXBuilder
from pex.pex_info import PexInfo
from pex.testing import make_bdist, temporary_filename


@contextmanager
def yield_pex_builder(zip_safe=True):
with nested(temporary_dir(), make_bdist('p1', zipped=True, zip_safe=zip_safe)) as (td, p1):
pb = PEXBuilder(path=td)
pb.add_egg(p1.location)
def yield_pex_builder(zip_safe=True, installer_impl=EggInstaller, interpreter=None):
with nested(temporary_dir(),
make_bdist('p1',
zipped=True,
zip_safe=zip_safe,
installer_impl=installer_impl)) as (td, p1):
pb = PEXBuilder(path=td, interpreter=interpreter)
pb.add_dist_location(p1.location)
yield pb


Expand Down Expand Up @@ -95,3 +106,30 @@ def test_load_internal_cache_unzipped():
assert len(dists) == 1
assert normalize(dists[0].location).startswith(
normalize(os.path.join(pb.path(), pb.info.internal_cache)))


_KNOWN_BAD_APPLE_INTERPRETER = ('/System/Library/Frameworks/Python.framework/Versions/'
'2.7/Resources/Python.app/Contents/MacOS/Python')


@pytest.mark.skipif(not os.path.exists(_KNOWN_BAD_APPLE_INTERPRETER),
reason='Test requires known bad Apple interpreter {}'
.format(_KNOWN_BAD_APPLE_INTERPRETER))
def test_osx_platform_intel_issue_523():
interpreter = PythonInterpreter.from_binary(_KNOWN_BAD_APPLE_INTERPRETER)
with nested(yield_pex_builder(installer_impl=WheelInstaller, interpreter=interpreter),
temporary_filename()) as (pb, pex_file):
for dist in resolver.resolve(['psutil==5.4.7'], precedence=(SourcePackage,)):
pb.add_dist_location(dist.location)
pb.build(pex_file)

pex = PEX(pex_file, interpreter=interpreter)
args = ['-c', 'import pkg_resources; print(pkg_resources.get_supported_platform())']
process = pex.run(args=args, stdout=subprocess.PIPE, blocking=False)
stdout, _ = process.communicate()
assert 0 == process.returncode

# Verify this all worked under the previously problematic pkg_resources-reported platform.
release, _, _ = platform.mac_ver()
major_minor = '.'.join(release.split('.')[:2])
assert 'macosx-{}-intel'.format(major_minor) == stdout.strip()

0 comments on commit 4c8120c

Please sign in to comment.