How to use the pex.common.temporary_dir 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_bdist_pex.py View on Github external
def bdist_pex(project_dir, bdist_args=None):
  with temporary_dir() as dist_dir:
    cmd = ['setup.py', 'bdist_pex', '--bdist-dir={}'.format(dist_dir)]
    if bdist_args:
      cmd.extend(bdist_args)

    spawn_python_job(args=cmd, cwd=project_dir).wait()
    dists = os.listdir(dist_dir)
    assert len(dists) == 1
    yield os.path.join(dist_dir, dists[0])
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_issues_539_abi3_resolution():
  # The cryptography team releases the following relevant pre-built wheels for version 2.6.1:
  # cryptography-2.6.1-cp27-cp27m-macosx_10_6_intel.whl
  # cryptography-2.6.1-cp27-cp27m-manylinux1_x86_64.whl
  # cryptography-2.6.1-cp27-cp27mu-manylinux1_x86_64.whl
  # cryptography-2.6.1-cp34-abi3-macosx_10_6_intel.whl
  # cryptography-2.6.1-cp34-abi3-manylinux1_x86_64.whl
  # With pex in --no-build mode, we force a test that pex abi3 resolution works when this test is
  # run under CPython>3.4,<4 on OSX and linux.

  with temporary_dir() as td:
    # The dependency graph for cryptography-2.6.1 includes pycparser which is only released as an
    # sdist. Since we want to test in --no-build, we pre-resolve/build the pycparser wheel here and
    # add the resulting wheelhouse to the --no-build pex command.
    download_dir = os.path.join(td, '.downloads')
    get_pip().spawn_download_distributions(
      download_dir=download_dir,
      requirements=['pycparser']
    ).wait()
    wheel_dir = os.path.join(td, '.wheels')
    get_pip().spawn_build_wheels(
      wheel_dir=wheel_dir,
      distributions=glob.glob(os.path.join(download_dir, '*'))
    ).wait()

    cryptography_pex = os.path.join(td, 'cryptography.pex')
    res = run_pex_command(['-f', wheel_dir,
github pantsbuild / pex / tests / test_common.py View on Github external
def test_atomic_directory_empty_workdir_finalize():
  with temporary_dir() as sandbox:
    target_dir = os.path.join(sandbox, 'target_dir')
    assert not os.path.exists(target_dir)

    with atomic_directory(target_dir) as work_dir:
      assert work_dir is not None
      assert os.path.exists(work_dir)
      assert os.path.isdir(work_dir)
      assert [] == os.listdir(work_dir)

      touch(os.path.join(work_dir, 'created'))

      assert not os.path.exists(target_dir)

    assert not os.path.exists(work_dir), 'The work_dir should always be cleaned up.'
    assert os.path.exists(os.path.join(target_dir, 'created'))
github pantsbuild / pex / tests / test_pex.py View on Github external
def test_activate_interpreter_different_from_current():
  with temporary_dir() as pex_root:
    interp_version = PY36 if PY2 else PY27
    custom_interpreter = PythonInterpreter.from_binary(ensure_python_interpreter(interp_version))
    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(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()
github pantsbuild / pex / tests / test_environment.py View on Github external
def yield_pex_builder(zip_safe=True, interpreter=None):
  with nested(temporary_dir(),
              make_bdist('p1', zip_safe=zip_safe, interpreter=interpreter)) as (td, p1):
    pb = PEXBuilder(path=td, interpreter=interpreter)
    pb.add_dist_location(p1.location)
    yield pb
github pantsbuild / pex / tests / test_common.py View on Github external
touch(one)

    two = os.path.join(src, 'two')
    touch(two)
    chmod_plus_x(two)

    with temporary_dir() as dst:
      chroot = Chroot(dst)
      copyfn(chroot, one, 'one')
      copyfn(chroot, two, 'two')
      assert extract_perms(one) == extract_perms(os.path.join(chroot.path(), 'one'))
      assert extract_perms(two) == extract_perms(os.path.join(chroot.path(), 'two'))

      zip_path = os.path.join(src, 'chroot.zip')
      chroot.zip(zip_path)
      with temporary_dir() as extract_dir:
        with contextlib.closing(PermPreservingZipFile(zip_path)) as zf:
          zf.extractall(extract_dir)

          assert extract_perms(one) == extract_perms(os.path.join(extract_dir, 'one'))
          assert extract_perms(two) == extract_perms(os.path.join(extract_dir, 'two'))
github pantsbuild / pex / tests / test_pex.py View on Github external
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
github pantsbuild / pex / tests / test_common.py View on Github external
def zip_fixture():
  with temporary_dir() as target_dir:
    one = os.path.join(target_dir, 'one')
    touch(one)

    two = os.path.join(target_dir, 'two')
    touch(two)
    chmod_plus_x(two)

    assert extract_perms(one) != extract_perms(two)

    zip_file = os.path.join(target_dir, 'test.zip')
    with contextlib.closing(PermPreservingZipFile(zip_file, 'w')) as zf:
      zf.write(one, 'one')
      zf.write(two, 'two')

    yield zip_file, os.path.join(target_dir, 'extract'), one, two
github pantsbuild / pex / pex / pex_builder.py View on Github external
def _add_dist_wheel_file(self, path, dist_name):
    with temporary_dir() as install_dir:
      get_pip().spawn_install_wheel(
        wheel=path,
        install_dir=install_dir,
        target=DistributionTarget.for_interpreter(self.interpreter)
      ).wait()
      return self._add_dist_dir(install_dir, dist_name)