def assert_access_zipped_assets(distribution_helper_import): test_executable = dedent(""" import os {distribution_helper_import} temp_dir = DistributionHelper.access_zipped_assets('my_package', 'submodule') with open(os.path.join(temp_dir, 'mod.py'), 'r') as fp: for line in fp: print(line) """.format(distribution_helper_import=distribution_helper_import)) with nested(temporary_dir(), temporary_dir()) as (td1, td2): pb = PEXBuilder(path=td1) with open(os.path.join(td1, 'exe.py'), 'w') as fp: fp.write(test_executable) pb.set_executable(fp.name) submodule = os.path.join(td1, 'my_package', 'submodule') safe_mkdir(submodule) mod_path = os.path.join(submodule, 'mod.py') with open(mod_path, 'w') as fp: fp.write('accessed') pb.add_source(fp.name, 'my_package/submodule/mod.py') pb.add_source(None, 'my_package/__init__.py') pb.add_source(None, 'my_package/submodule/__init__.py') pex = os.path.join(td2, 'app.pex') pb.build(pex) process = PEX(pex, interpreter=pb.interpreter).run(blocking=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() assert process.returncode == 0 assert b'accessed\n' == stdout return stderr
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 assert_access_zipped_assets(distribution_helper_import): # type: (str) -> bytes test_executable = dedent(""" import os {distribution_helper_import} temp_dir = DistributionHelper.access_zipped_assets('my_package', 'submodule') with open(os.path.join(temp_dir, 'mod.py'), 'r') as fp: for line in fp: print(line) """.format(distribution_helper_import=distribution_helper_import)) with temporary_dir() as td1, temporary_dir() as td2: pb = PEXBuilder(path=td1) with open(os.path.join(td1, "exe.py"), "w") as fp: fp.write(test_executable) pb.set_executable(fp.name) submodule = os.path.join(td1, "my_package", "submodule") safe_mkdir(submodule) mod_path = os.path.join(submodule, "mod.py") with open(mod_path, "w") as fp: fp.write("accessed") pb.add_source(fp.name, "my_package/submodule/mod.py") pb.add_source(None, "my_package/__init__.py") pb.add_source(None, "my_package/submodule/__init__.py") pex = os.path.join(td2, "app.pex") pb.build(pex) process = PEX(pex, interpreter=pb.interpreter).run(blocking=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() assert process.returncode == 0 assert b"accessed\n" == stdout return cast(bytes, stderr)
def test_execute_interpreter_dashc_program(): with temporary_dir() as pex_chroot: pex_builder = PEXBuilder(path=pex_chroot) pex_builder.freeze() process = PEX(pex_chroot).run(args=['-c', 'import sys; print(" ".join(sys.argv))', 'one'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, blocking=False) stdout, stderr = process.communicate() assert 0 == process.returncode assert b'-c one\n' == stdout assert b'' == stderr
def test_execute_interpreter_file_program(): with temporary_dir() as pex_chroot: pex_builder = PEXBuilder(path=pex_chroot) pex_builder.freeze() with tempfile.NamedTemporaryFile() as fp: fp.write(b'import sys; print(" ".join(sys.argv))') fp.flush() process = PEX(pex_chroot).run(args=[fp.name, 'one', 'two'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, blocking=False) stdout, stderr = process.communicate() assert 0 == process.returncode assert '{} one two\n'.format(fp.name).encode('utf-8') == stdout assert b'' == stderr
def test_execute_interpreter_stdin_program(): with temporary_dir() as pex_chroot: pex_builder = PEXBuilder(path=pex_chroot) pex_builder.freeze() process = PEX(pex_chroot).run( args=["-", "one", "two"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, blocking=False, ) stdout, stderr = process.communicate( input=b'import sys; print(" ".join(sys.argv))') assert 0 == process.returncode assert b"- one two\n" == stdout assert b"" == stderr
def test_activate_extras_issue_615(): with yield_pex_builder() as pb: for resolved_dist in resolver.resolve(['pex[requests]==1.6.3'], interpreter=pb.interpreter): pb.add_requirement(resolved_dist.requirement) pb.add_dist_location(resolved_dist.distribution.location) pb.set_script('pex') pb.freeze() process = PEX(pb.path(), interpreter=pb.interpreter).run(args=['--version'], env={'PEX_VERBOSE': '9'}, blocking=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() assert 0 == process.returncode, ( 'Process failed with exit code {} and output:\n{}'.format(process.returncode, stderr) ) assert to_bytes('{} 1.6.3'.format(os.path.basename(pb.path()))) == stdout.strip()
def test_execute_interpreter_dashm_module(): with temporary_dir() as pex_chroot: pex_builder = PEXBuilder(path=pex_chroot) with temporary_file(root_dir=pex_chroot) as fp: fp.write(b'import sys; print(" ".join(sys.argv))') fp.close() pex_builder.add_source(fp.name, 'foo/bar.py') pex_builder.freeze() process = PEX(pex_chroot).run(args=['-m', 'foo.bar', 'one', 'two'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, blocking=False) stdout, stderr = process.communicate() assert 0 == process.returncode assert b'foo.bar one two\n' == stdout assert b'' == stderr
def test_activate_extras_issue_615(): # type: () -> None with yield_pex_builder() as pb: for resolved_dist in resolver.resolve(["pex[requests]==1.6.3"], interpreter=pb.interpreter): pb.add_requirement(resolved_dist.requirement) pb.add_dist_location(resolved_dist.distribution.location) pb.set_script("pex") pb.freeze() process = PEX(pb.path(), interpreter=pb.interpreter).run( args=["--version"], env={"PEX_VERBOSE": "9"}, blocking=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) stdout, stderr = process.communicate() assert 0 == process.returncode, "Process failed with exit code {} and output:\n{}".format( process.returncode, stderr ) assert to_bytes("{} 1.6.3".format(os.path.basename(pb.path()))) == stdout.strip()
def assert_namespace_packages_warning(distribution, version, expected_warning): # type: (str, str, bool) -> None requirement = "{}=={}".format(distribution, version) pb = PEXBuilder() for resolved_dist in resolver.resolve([requirement]): pb.add_dist_location(resolved_dist.distribution.location) pb.freeze() process = PEX(pb.path()).run(args=["-c", ""], blocking=False, stderr=subprocess.PIPE) _, stderr = process.communicate() stderr_text = stderr.decode("utf8") partial_warning_preamble = "PEXWarning: The `pkg_resources` package was loaded" partial_warning_detail = "{} namespace packages:".format(requirement) if expected_warning: assert partial_warning_preamble in stderr_text assert partial_warning_detail in stderr_text else: assert partial_warning_preamble not in stderr_text assert partial_warning_detail not in stderr_text