def test_pex_executable(): # Tests that pex keeps executable permissions with temporary_dir() as temp_dir: pex_dir = os.path.join(temp_dir, 'pex_dir') safe_mkdir(pex_dir) with open(os.path.join(pex_dir, 'exe.py'), 'w') as fp: fp.write( textwrap.dedent(''' import subprocess import os import sys import my_package path = os.path.join(os.path.dirname(my_package.__file__), 'bin/start.sh') sys.stdout.write(subprocess.check_output([path]).decode('utf-8')) ''')) project_content = { 'setup.py': textwrap.dedent(''' from setuptools import setup setup( name='my_project', version='0.0.0.0', zip_safe=True, packages=['my_package'], package_data={'my_package': ['bin/*']}, install_requires=[], ) '''), 'my_package/__init__.py': 0, 'my_package/bin/start.sh': ("#!/usr/bin/env bash\n" "echo 'hello world from start.sh!'"), 'my_package/my_module.py': 'def do_something():\n print("hello world!")\n', } pex_builder = PEXBuilder(path=pex_dir) with temporary_content(project_content, perms=0o755) as project_dir: installer = WheelInstaller(project_dir) bdist = DistributionHelper.distribution_from_path( installer.bdist()) pex_builder.add_dist_location(bdist.location) pex_builder.set_executable(os.path.join(pex_dir, 'exe.py')) pex_builder.freeze() app_pex = os.path.join(os.path.join(temp_dir, 'out_pex_dir'), 'app.pex') pex_builder.build(app_pex) std_out, rc = run_simple_pex( app_pex, env={'PEX_ROOT': os.path.join(temp_dir, '.pex')}) assert rc == 0 assert std_out.decode('utf-8') == 'hello world from start.sh!\n'
def pythonpath_isolation_test(): with temporary_dir() as temp_dir: pythonpath = os.path.join(temp_dir, 'one') with safe_open(os.path.join(pythonpath, 'foo.py'), 'w') as fp: fp.write('BAR = 42') with safe_open(os.path.join(pythonpath, 'bar.py'), 'w') as fp: fp.write('FOO = 137') dist_content = { 'setup.py': textwrap.dedent(""" from setuptools import setup setup( name='foo', version='0.0.0', zip_safe=True, packages=['foo'], install_requires=[], ) """), 'foo/__init__.py': 'BAR = 137', } with temporary_content(dist_content) as project_dir: installer = WheelInstaller(project_dir) foo_bdist = DistributionHelper.distribution_from_path( installer.bdist()) exe_contents = textwrap.dedent(""" import sys try: import bar except ImportError: import collections bar = collections.namedtuple('bar', ['FOO'])(None) import foo sys.stdout.write("foo.BAR={} bar.FOO={}".format(foo.BAR, bar.FOO)) """) yield PythonpathIsolationTest(pythonpath=pythonpath, dists=[foo_bdist], exe=exe_contents)
def test_unwriteable_contents(): my_app_setup_py = dedent(""" from setuptools import setup setup( name='my_app', version='0.0.0', zip_safe=True, packages=['my_app'], include_package_data=True, package_data={'my_app': ['unwriteable.so']}, ) """) UNWRITEABLE_PERMS = 0o400 with temporary_content( { 'setup.py': my_app_setup_py, 'my_app/__init__.py': '', 'my_app/unwriteable.so': '' }, perms=UNWRITEABLE_PERMS) as my_app_project_dir: my_app_whl = WheelInstaller(my_app_project_dir).bdist() uses_my_app_setup_py = bdist_pex_setup_py(name='uses_my_app', version='0.0.0', zip_safe=True, install_requires=['my_app']) with temporary_content({'setup.py': uses_my_app_setup_py }) as uses_my_app_project_dir: pex_args = '--pex-args=--disable-cache --no-pypi -f {}'.format( os.path.dirname(my_app_whl)) uses_my_app_pex = bdist_pex_installer(uses_my_app_project_dir, bdist_args=[pex_args ]).bdist() with open_zip(uses_my_app_pex) as zf: unwriteable_sos = [ path for path in zf.namelist() if path.endswith('my_app/unwriteable.so') ] assert 1 == len(unwriteable_sos) unwriteable_so = unwriteable_sos.pop() zf.extract(unwriteable_so, path=uses_my_app_project_dir) extract_dest = os.path.join(uses_my_app_project_dir, unwriteable_so) assert UNWRITEABLE_PERMS == stat.S_IMODE( os.stat(extract_dest).st_mode)
def add_wheel(builder, content): with temporary_content(content) as project: dist = WheelInstaller(project, interpreter=builder.interpreter).bdist() builder.add_dist_location(dist)