How to use the pex.compatibility.nested function in pex

To help you get started, we’ve selected a few pex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pantsbuild / pex / tests / test_util.py View on Github external
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)
github pantsbuild / pex / tests / test_environment.py View on Github external
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,
                         interpreter=interpreter)) as (td, p1):
    pb = PEXBuilder(path=td, interpreter=interpreter)
    pb.add_dist_location(p1.location)
    yield pb
github pantsbuild / pex / tests / test_environment.py View on Github external
return PythonInterpreter.from_binary(_KNOWN_BAD_APPLE_INTERPRETER,
                                         include_site_extras=include_site_extras)

  interpreter = bad_interpreter(include_site_extras=False)
  with temporary_dir() as cache:
    # We need to run the bad interpreter with a modern, non-Apple-Extras setuptools in order to
    # successfully install psutil.
    for requirement in (SETUPTOOLS_REQUIREMENT, WHEEL_REQUIREMENT):
      for dist in resolver.resolve([requirement],
                                   cache=cache,
                                   # We can't use wheels since we're bootstrapping them.
                                   precedence=(SourcePackage, EggPackage),
                                   interpreter=interpreter):
        interpreter = interpreter.with_extra(dist.key, dist.version, dist.location)

    with nested(yield_pex_builder(installer_impl=WheelInstaller, interpreter=interpreter),
                temporary_filename()) as (pb, pex_file):
      for dist in resolver.resolve(['psutil==5.4.3'],
                                   cache=cache,
                                   precedence=(SourcePackage, WheelPackage),
                                   interpreter=interpreter):
        pb.add_dist_location(dist.location)
      pb.build(pex_file)

      # NB: We want PEX to find the bare bad interpreter at runtime.
      pex = PEX(pex_file, interpreter=bad_interpreter())
      args = ['-c', 'import pkg_resources; print(pkg_resources.get_supported_platform())']
      env = os.environ.copy()
      env['PEX_VERBOSE'] = '1'
      process = pex.run(args=args,
                        env=env,
                        blocking=False,
github pantsbuild / pex / tests / test_environment.py View on Github external
def add_requirements(builder, cache):
    for resolved_dist in resolve(requirements, cache=cache, interpreter=builder.interpreter):
      builder.add_requirement(resolved_dist.requirement)
      builder.add_distribution(resolved_dist.distribution)

  def add_wheel(builder, content):
    with temporary_content(content) as project:
      dist = WheelBuilder(project, interpreter=builder.interpreter).bdist()
      builder.add_dist_location(dist)

  def add_sources(builder, content):
    with temporary_content(content) as project:
      for path in content.keys():
        builder.add_source(os.path.join(project, path), path)

  with nested(temporary_dir(), temporary_dir()) as (root, cache):
    pex_info1 = PexInfo.default()
    pex_info1.zip_safe = False
    pex1 = os.path.join(root, 'pex1.pex')
    builder1 = PEXBuilder(interpreter=interpreter, pex_info=pex_info1)
    add_requirements(builder1, cache)
    add_wheel(builder1, content1)
    add_sources(builder1, content2)
    builder1.build(pex1)

    pex_info2 = PexInfo.default()
    pex_info2.pex_path = pex1
    pex2 = os.path.join(root, 'pex2')
    builder2 = PEXBuilder(path=pex2, interpreter=interpreter, pex_info=pex_info2)
    add_requirements(builder2, cache)
    add_wheel(builder2, content3)
    builder2.set_script('foobaz')
github pantsbuild / pex / tests / test_pex_builder.py View on Github external
def test_pex_builder():
  # test w/ and w/o zipfile dists
  with nested(temporary_dir(), make_bdist('p1', zipped=True)) as (td, p1):
    write_pex(td, exe_main, dists=[p1])

    success_txt = os.path.join(td, 'success.txt')
    PEX(td).run(args=[success_txt])
    assert os.path.exists(success_txt)
    with open(success_txt) as fp:
      assert fp.read() == 'success'

  # test w/ and w/o zipfile dists
  with nested(temporary_dir(), temporary_dir(), make_bdist('p1', zipped=True)) as (
      td1, td2, p1):
    target_egg_dir = os.path.join(td2, os.path.basename(p1.location))
    safe_mkdir(target_egg_dir)
    with open_zip(p1.location, 'r') as zf:
      zf.extractall(target_egg_dir)
    p1 = DistributionHelper.distribution_from_path(target_egg_dir)
github pantsbuild / pex / tests / test_environment.py View on Github external
def test_load_internal_cache_unzipped():
  # zip_safe pex will not be written to install cache unless always_write_cache
  with nested(yield_pex_builder(zip_safe=True), temporary_dir()) as (pb, pex_root):
    pb.info.pex_root = pex_root
    pb.freeze()

    dists = list(PEXEnvironment.load_internal_cache(pb.path(), pb.info))
    assert len(dists) == 1
    assert normalize(dists[0].location).startswith(
        normalize(os.path.join(pb.path(), pb.info.internal_cache)))
github pantsbuild / pex / tests / test_pex.py View on Github external
def test_site_libs_symlink():
  with nested(mock.patch.object(PEX, '_get_site_packages'), temporary_dir()) as (
          mock_site_packages, tempdir):
    site_packages = os.path.join(tempdir, 'site-packages')
    os.mkdir(site_packages)
    site_packages_link = os.path.join(tempdir, 'site-packages-link')
    os.symlink(site_packages, site_packages_link)
    mock_site_packages.return_value = set([site_packages_link])

    site_libs = PEX.site_libs()
    assert os.path.realpath(site_packages) in site_libs
    assert site_packages_link in site_libs
github pantsbuild / pex / tests / test_pex_builder.py View on Github external
def test_pex_builder_compilation():
  with nested(temporary_dir(), temporary_dir(), temporary_dir()) as (td1, td2, td3):
    src = os.path.join(td1, 'src.py')
    with open(src, 'w') as fp:
      fp.write(exe_main)

    exe = os.path.join(td1, 'exe.py')
    with open(exe, 'w') as fp:
      fp.write(exe_main)

    def build_and_check(path, precompile):
      pb = PEXBuilder(path)
      pb.add_source(src, 'lib/src.py')
      pb.set_executable(exe, 'exe.py')
      pb.freeze(bytecode_compile=precompile)
      for pyc_file in ('exe.pyc', 'lib/src.pyc', '__main__.pyc'):
        pyc_exists = os.path.exists(os.path.join(path, pyc_file))
        if precompile: