Beispiel #1
0
def test_pex_builder_from_requirements_pex():
    # type: () -> None
    def build_from_req_pex(path, req_pex):
        # type: (str, str) -> PEXBuilder
        pb = PEXBuilder(path=path)
        pb.add_from_requirements_pex(req_pex)
        with open(os.path.join(path, "exe.py"), "w") as fp:
            fp.write(exe_main)
        pb.set_executable(os.path.join(path, "exe.py"))
        pb.freeze()
        return pb

    def verify(pb):
        # type: (PEXBuilder) -> None
        success_txt = os.path.join(pb.path(), "success.txt")
        PEX(pb.path(), interpreter=pb.interpreter).run(args=[success_txt])
        assert os.path.exists(success_txt)
        with open(success_txt) as fp:
            assert fp.read() == "success"

    # Build from pex dir.
    with temporary_dir() as td2:
        with temporary_dir() as td1, make_bdist("p1") as p1:
            pb1 = write_pex(td1, dists=[p1])
            pb2 = build_from_req_pex(td2, pb1.path())
        verify(pb2)

    # Build from .pex file.
    with temporary_dir() as td4:
        with temporary_dir() as td3, make_bdist("p1") as p1:
            pb3 = write_pex(td3, dists=[p1])
            target = os.path.join(td3, "foo.pex")
            pb3.build(target)
            pb4 = build_from_req_pex(td4, target)
        verify(pb4)
Beispiel #2
0
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 closing(zipfile.ZipFile(p1.location, 'r')) as zf:
      zf.extractall(target_egg_dir)
    p1 = DistributionHelper.distribution_from_path(target_egg_dir)

    write_pex(td1, exe_main, dists=[p1])

    success_txt = os.path.join(td1, 'success.txt')
    PEX(td1).run(args=[success_txt])
    assert os.path.exists(success_txt)
    with open(success_txt) as fp:
      assert fp.read() == 'success'
Beispiel #3
0
def test_pex_builder_from_requirements_pex():
  def build_from_req_pex(path, req_pex):
    pb = PEXBuilder(path=path)
    pb.add_from_requirements_pex(req_pex)
    with open(os.path.join(path, 'exe.py'), 'w') as fp:
      fp.write(exe_main)
    pb.set_executable(os.path.join(path, 'exe.py'))
    pb.freeze()
    return pb

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

  # Build from pex dir.
  with temporary_dir() as td2:
    with nested(temporary_dir(), make_bdist('p1')) as (td1, p1):
      pb1 = write_pex(td1, dists=[p1])
      pb2 = build_from_req_pex(td2, pb1.path())
    verify(pb2)

  # Build from .pex file.
  with temporary_dir() as td4:
    with nested(temporary_dir(), make_bdist('p1')) as (td3, p1):
      pb3 = write_pex(td3, dists=[p1])
      target = os.path.join(td3, 'foo.pex')
      pb3.build(target)
      pb4 = build_from_req_pex(td4, target)
    verify(pb4)
Beispiel #4
0
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 closing(zipfile.ZipFile(p1.location, 'r')) as zf:
      zf.extractall(target_egg_dir)
    p1 = DistributionHelper.distribution_from_path(target_egg_dir)

    write_pex(td1, exe_main, dists=[p1])

    success_txt = os.path.join(td1, 'success.txt')
    PEX(td1).run(args=[success_txt])
    assert os.path.exists(success_txt)
    with open(success_txt) as fp:
      assert fp.read() == 'success'
Beispiel #5
0
def test_activate_interpreter_different_from_current():
    with temporary_dir() as pex_root:
        interp_version = '3.6.3' if PY2 else '2.7.10'
        custom_interpreter = get_interpreter(
            python_interpreter=ensure_python_interpreter(interp_version),
            interpreter_cache_dir=os.path.join(pex_root, 'interpreters'),
            repos=None,  # Default to PyPI.
            use_wheel=True)
        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(installer_impl=WheelInstaller,
                            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()
                except SystemExit as e:
                    pytest.fail('PEX activation of %s failed with %s' %
                                (pex, e))
Beispiel #6
0
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
Beispiel #7
0
def yield_pex_builder(zip_safe=True, interpreter=None):
    # type: (bool, Optional[PythonInterpreter]) -> Iterator[PEXBuilder]
    with temporary_dir() as td, make_bdist("p1",
                                           zip_safe=zip_safe,
                                           interpreter=interpreter) as p1:
        pb = PEXBuilder(path=td, interpreter=interpreter)
        pb.add_dist_location(p1.location)
        yield pb
Beispiel #8
0
def test_pex_builder_wheeldep():
    """Repeat the pex_builder test, but this time include an import of something from a wheel that
    doesn't come in importable form."""
    with nested(temporary_dir(), make_bdist("p1")) as (td, p1):
        pyparsing_path = "./tests/example_packages/pyparsing-2.1.10-py2.py3-none-any.whl"
        pb = write_pex(td, wheeldeps_exe_main, dists=[p1, pyparsing_path])
        success_txt = os.path.join(td, "success.txt")
        PEX(td, interpreter=pb.interpreter).run(args=[success_txt])
        assert os.path.exists(success_txt)
        with open(success_txt) as fp:
            assert fp.read() == "success"
Beispiel #9
0
def test_pex_builder():
  # test w/ and w/o zipfile dists
  with nested(temporary_dir(), make_bdist('p1')) as (td, p1):
    pb = write_pex(td, exe_main, dists=[p1])

    success_txt = os.path.join(td, 'success.txt')
    PEX(td, interpreter=pb.interpreter).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')) as (td1, td2, p1):
    pb = write_pex(td1, exe_main, dists=[p1])

    success_txt = os.path.join(td1, 'success.txt')
    PEX(td1, interpreter=pb.interpreter).run(args=[success_txt])
    assert os.path.exists(success_txt)
    with open(success_txt) as fp:
      assert fp.read() == 'success'
Beispiel #10
0
def test_pex_builder():
    # type: () -> None
    # test w/ and w/o zipfile dists
    with temporary_dir() as td, make_bdist("p1") as p1:
        pb = write_pex(td, exe_main, dists=[p1])

        success_txt = os.path.join(td, "success.txt")
        PEX(td, interpreter=pb.interpreter).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 temporary_dir() as td1, temporary_dir() as td2, make_bdist("p1") as p1:
        pb = write_pex(td1, exe_main, dists=[p1])

        success_txt = os.path.join(td1, "success.txt")
        PEX(td1, interpreter=pb.interpreter).run(args=[success_txt])
        assert os.path.exists(success_txt)
        with open(success_txt) as fp:
            assert fp.read() == "success"
Beispiel #11
0
def test_pex_builder_wheeldep():
  """Repeat the pex_builder test, but this time include an import of
  something from a wheel that doesn't come in importable form.
  """
  with nested(temporary_dir(), make_bdist('p1', zipped=True)) as (td, p1):
    pyparsing_path = "./tests/example_packages/pyparsing-2.1.10-py2.py3-none-any.whl"
    dist = DistributionHelper.distribution_from_path(pyparsing_path)
    pb = write_pex(td, wheeldeps_exe_main, dists=[p1, dist])
    success_txt = os.path.join(td, 'success.txt')
    PEX(td, interpreter=pb.interpreter).run(args=[success_txt])
    assert os.path.exists(success_txt)
    with open(success_txt) as fp:
      assert fp.read() == 'success'
Beispiel #12
0
def test_pex_builder_wheeldep():
    """Repeat the pex_builder test, but this time include an import of
  something from a wheel that doesn't come in importable form.
  """
    with nested(temporary_dir(), make_bdist('p1', zipped=True)) as (td, p1):
        pyparsing_path = "./tests/example_packages/pyparsing-2.1.10-py2.py3-none-any.whl"
        dist = DistributionHelper.distribution_from_path(pyparsing_path)
        write_pex(td, wheeldeps_exe_main, dists=[p1, dist])
        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'
Beispiel #13
0
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()
        except SystemExit as e:
          pytest.fail('PEX activation of %s failed with %s' % (pex, e))
Beispiel #14
0
def yield_pex_builder(zip_safe=True):
    with nested(temporary_dir(), make_bdist("p1", zipped=True, zip_safe=zip_safe)) as (td, p1):
        pb = PEXBuilder(path=td)
        pb.add_egg(p1.location)
        yield pb
Beispiel #15
0
def yield_pex_builder(zip_safe=True):
    with nested(temporary_dir(),
                make_bdist('p1', zipped=True, zip_safe=zip_safe)) as (td, p1):
        pb = PEXBuilder(path=td)
        pb.add_egg(p1.location)
        yield pb