Skip to content

Commit

Permalink
Fix PEX environment setup.
Browse files Browse the repository at this point in the history
Previously, if a PEX had a custom interpreter, it was not threaded
through to the environments it activated. Add a test that the
designated interpreter is used.

Fixes pex-tool#522
  • Loading branch information
jsirois committed Jul 27, 2018
1 parent d1f946c commit 0f4a76e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _activate(self):
pex_info = self._pex_info.copy()
pex_info.update(self._pex_info_overrides)
pex_info.merge_pex_path(self._vars.PEX_PATH)
self._envs.append(PEXEnvironment(self._pex, pex_info))
self._envs.append(PEXEnvironment(self._pex, pex_info, interpreter=self._interpreter))
# N.B. by this point, `pex_info.pex_path` will contain a single pex path
# merged from pex_path in `PEX-INFO` and `PEX_PATH` set in the environment.
# `PEX_PATH` entries written into `PEX-INFO` take precedence over those set
Expand All @@ -83,7 +83,7 @@ def _activate(self):
for pex_path in filter(None, pex_info.pex_path.split(os.pathsep)):
pex_info = PexInfo.from_pex(pex_path)
pex_info.update(self._pex_info_overrides)
self._envs.append(PEXEnvironment(pex_path, pex_info))
self._envs.append(PEXEnvironment(pex_path, pex_info, interpreter=self._interpreter))

# activate all of them
for env in self._envs:
Expand Down
13 changes: 7 additions & 6 deletions pex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def write_zipfile(directory, dest, reverse=False):

@contextlib.contextmanager
def make_installer(name='my_project', version='0.0.0', installer_impl=EggInstaller, zip_safe=True,
install_reqs=None):
install_reqs=None, **kwargs):
interp = {'project_name': name,
'version': version,
'zip_safe': zip_safe,
'install_requires': install_reqs or []}
with temporary_content(PROJECT_CONTENT, interp=interp) as td:
yield installer_impl(td)
yield installer_impl(td, **kwargs)


@contextlib.contextmanager
Expand All @@ -145,19 +145,20 @@ def make_source_dir(name='my_project', version='0.0.0', install_reqs=None):
yield td


def make_sdist(name='my_project', version='0.0.0', zip_safe=True, install_reqs=None):
def make_sdist(name='my_project', version='0.0.0', zip_safe=True, install_reqs=None, **kwargs):
with make_installer(name=name, version=version, installer_impl=Packager, zip_safe=zip_safe,
install_reqs=install_reqs) as packager:
install_reqs=install_reqs, **kwargs) as packager:
return packager.sdist()


@contextlib.contextmanager
def make_bdist(name='my_project', version='0.0.0', installer_impl=EggInstaller, zipped=False,
zip_safe=True):
zip_safe=True, **kwargs):
with make_installer(name=name,
version=version,
installer_impl=installer_impl,
zip_safe=zip_safe) as installer:
zip_safe=zip_safe,
**kwargs) as installer:
dist_location = installer.bdist()
if zipped:
yield DistributionHelper.distribution_from_path(dist_location)
Expand Down
46 changes: 39 additions & 7 deletions tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
import pytest
from twitter.common.contextutil import temporary_file

from pex.compatibility import WINDOWS, nested, to_bytes
from pex.bin.pex import get_interpreter
from pex.compatibility import PY2, WINDOWS, nested, to_bytes
from pex.installer import EggInstaller, WheelInstaller
from pex.pex import PEX
from pex.pex_builder import PEXBuilder
from pex.pex_info import PexInfo
from pex.testing import (
make_installer,
named_temporary_file,
run_simple_pex_test,
temporary_dir,
write_simple_pex
)
IS_PYPY,
ensure_python_interpreter,
make_installer,
named_temporary_file,
run_simple_pex_test,
temporary_dir,
write_simple_pex,
make_bdist)
from pex.util import DistributionHelper

try:
Expand Down Expand Up @@ -309,3 +313,31 @@ def test_pex_verify_entry_point_module_should_fail():
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)


@pytest.mark.skipif(IS_PYPY)
def test_activate_interpreter_different_from_current():
with temporary_dir() as pex_root:
interp_version = '3.6.3' if PY2 else '2.7.10'
custom_interpreter = get_interpreter(
python_interpreter=ensure_python_interpreter(interp_version),
interpreter_cache_dir=os.path.join(pex_root, 'interpreters'),
repos=None, # Default to PyPI.
use_wheel=True
)
pex_info = PexInfo.default(custom_interpreter)
pex_info.pex_root = pex_root
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot,
interpreter=custom_interpreter,
pex_info=pex_info)
with make_bdist(installer_impl=WheelInstaller, interpreter=custom_interpreter) as bdist:
pex_builder.add_distribution(bdist)
pex_builder.set_entry_point('sys:exit')
pex_builder.freeze()

pex = PEX(pex_builder.path(), interpreter=custom_interpreter)
try:
pex._activate()
except SystemExit as e:
pytest.fail('PEX activation of %s failed with %s' % (pex, e))

0 comments on commit 0f4a76e

Please sign in to comment.