def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, 'pex1') write_simple_pex( pex1_path, exe_contents='', sources=[ ('foo_pkg/__init__.py', ''), ('foo_pkg/foo_module.py', 'def foo_func():\n return "42"') ] ) pex2_path = os.path.join(temp_dir, 'pex2') pex2 = write_simple_pex( pex2_path, 'import sys; from bar_pkg.bar_module import bar_func; ' 'sys.stdout.write(bar_func()); sys.exit(0)', sources=[ ('bar_pkg/bar_module.py', 'from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()') ] ) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={'PEX_PATH': pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'42'
def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, 'pex1') write_simple_pex(pex1_path, sources=[('foo_pkg/__init__.py', ''), ('foo_pkg/foo_module.py', 'def foo_func():\n return "42"')]) pex2_path = os.path.join(temp_dir, 'pex2') pex2 = write_simple_pex( pex2_path, 'import sys; from bar_pkg.bar_module import bar_func; ' 'sys.stdout.write(bar_func()); sys.exit(0)', sources= [('bar_pkg/__init__.py', ''), ('bar_pkg/bar_module.py', 'from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()' )]) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={'PEX_PATH': pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'42'
def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, "pex1") write_simple_pex( pex1_path, sources=[ ("foo_pkg/__init__.py", ""), ("foo_pkg/foo_module.py", 'def foo_func():\n return "42"'), ], ) pex2_path = os.path.join(temp_dir, "pex2") pex2 = write_simple_pex( pex2_path, "import sys; from bar_pkg.bar_module import bar_func; " "sys.stdout.write(bar_func()); sys.exit(0)", sources=[ ("bar_pkg/__init__.py", ""), ( "bar_pkg/bar_module.py", "from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()", ), ], ) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={"PEX_PATH": pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b"42"
def test_pex_run_custom_pex_useable(): old_pex_version = '0.7.0' resolved_dists = resolve( ['pex=={}'.format(old_pex_version), 'setuptools==40.6.3']) dists = [resolved_dist.distribution for resolved_dist in resolved_dists] with temporary_dir() as temp_dir: from pex.version import __version__ pex = write_simple_pex( temp_dir, exe_contents=textwrap.dedent(""" import sys try: # The 0.7.0 release embedded the version directly in setup.py so it should only be # available via distribution metadata. from pex.version import __version__ sys.exit(1) except ImportError: import pkg_resources dist = pkg_resources.working_set.find(pkg_resources.Requirement.parse('pex')) print(dist.version) """), dists=dists, ) process = PEX(pex.path()).run(blocking=False, stdout=subprocess.PIPE) stdout, _ = process.communicate() assert process.returncode == 0 assert old_pex_version == stdout.strip().decode('utf-8') assert old_pex_version != __version__
def test_pex_run_conflicting_custom_setuptools_useable(): # Here we use our vendored, newer setuptools to build the pex which has an older setuptools # requirement. These setuptools dists have different pkg_resources APIs: # $ diff \ # <(zipinfo -1 setuptools-20.3.1-py2.py3-none-any.whl | grep pkg_resources/ | sort) \ # <(zipinfo -1 setuptools-40.6.2-py2.py3-none-any.whl | grep pkg_resources/ | sort) # 2a3,4 # > pkg_resources/py31compat.py # > pkg_resources/_vendor/appdirs.py resolved_dists = resolve(['setuptools==20.3.1']) dists = [resolved_dist.distribution for resolved_dist in resolved_dists] with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, exe_contents=textwrap.dedent(""" import sys import pkg_resources try: from pkg_resources import appdirs sys.exit(1) except ImportError: pass try: from pkg_resources import py31compat sys.exit(2) except ImportError: pass """), dists=dists, ) rc = PEX(pex.path()).run(env={'PEX_VERBOSE': '9'}) assert rc == 0
def test_pex_run_conflicting_custom_setuptools_useable(): # Here we use an older setuptools to build the pex which has a newer setuptools requirement. # These setuptools dists have different pkg_resources APIs: # $ diff \ # <(zipinfo -1 setuptools-20.3.1-py2.py3-none-any.whl | grep pkg_resources/ | sort) \ # <(zipinfo -1 setuptools-40.4.3-py2.py3-none-any.whl | grep pkg_resources/ | sort) # 2a3,4 # > pkg_resources/py31compat.py # > pkg_resources/_vendor/appdirs.py with temporary_dir() as resolve_cache: dists = [resolved_dist.distribution for resolved_dist in resolve(['setuptools==20.3.1'], cache=resolve_cache)] interpreter = PythonInterpreter.from_binary(sys.executable, path_extras=[dist.location for dist in dists], include_site_extras=False) dists = [resolved_dist.distribution for resolved_dist in resolve(['setuptools==40.4.3'], cache=resolve_cache)] with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'from pkg_resources import appdirs, py31compat', dists=dists, interpreter=interpreter ) rc = PEX(pex.path()).run() assert rc == 0
def test_pex_run_custom_setuptools_useable(): resolved_dists = resolve(['setuptools==36.2.7']) dists = [resolved_dist.distribution for resolved_dist in resolved_dists] with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'from setuptools.sandbox import run_setup', dists=dists, ) rc = PEX(pex.path()).run() assert rc == 0
def test_pex_run_custom_setuptools_useable(): with temporary_dir() as resolve_cache: dists = resolve(['setuptools==36.2.7'], cache=resolve_cache) with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'from setuptools.sandbox import run_setup', dists=dists, ) rc = PEX(pex.path()).run() assert rc == 0
def test_pex_run(): with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'import sys; sys.stdout.write("hello"); sys.stderr.write("hello"); sys.exit(0)' ) rc = PEX(pex.path()).run(stdin=None, stdout=fake_stdout, stderr=fake_stdout) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'hellohello'
def test_get_pex_info(): with temporary_dir() as td: pb = write_simple_pex(td, 'print("hello world!")') pex_path = os.path.join(td, 'hello_world.pex') pb.build(pex_path) # from zip pex_info = get_pex_info(pex_path) with temporary_dir() as pex_td: with closing(zipfile.ZipFile(pex_path, 'r')) as zf: zf.extractall(pex_td) # from dir pex_info_2 = get_pex_info(pex_td) # same when encoded assert pex_info.dump() == pex_info_2.dump()
def test_pex_run_extra_sys_path(): # type: () -> None with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'import sys; sys.stdout.write(":".join(sys.path)); sys.exit(0)' ) rc = PEX(pex.path()).run( stdin=None, stdout=fake_stdout, stderr=None, env={"PEX_EXTRA_SYS_PATH": "extra/syspath/entry1:extra/syspath/entry2"}, ) assert rc == 0 fake_stdout.seek(0) syspath = fake_stdout.read().split(b":") assert b"extra/syspath/entry1" in syspath assert b"extra/syspath/entry2" in syspath
def assert_isolation(self, inherit_path, expected_output): env = dict(PYTHONPATH=self.pythonpath) with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex_builder = write_simple_pex( temp_dir, pex_info=self.pex_info(inherit_path), dists=self.dists, exe_contents=self.exe, ) # Test the PEX.run API. rc = PEX(pex_builder.path()).run(stdout=fake_stdout, env=env) assert rc == 0 fake_stdout.seek(0) assert expected_output == fake_stdout.read().decode('utf-8') # Test direct PEX execution. assert expected_output == subprocess.check_output([sys.executable, pex_builder.path()], env=env).decode('utf-8')