Пример #1
0
def test_pex_builder():
  # test w/ and w/o zipfile dists
  with nested(temporary_dir(), make_distribution('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_distribution('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'
Пример #2
0
def test_pex_builder():
    # test w/ and w/o zipfile dists
    with nested(temporary_dir(), make_distribution('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_distribution('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'
Пример #3
0
def yield_pex_builder(zip_safe=True):
    with nested(temporary_dir(),
                make_distribution('p1', zipped=True,
                                  zip_safe=zip_safe)) as (td, p1):
        pb = PEXBuilder(path=td)
        pb.add_egg(p1.location)
        yield pb
Пример #4
0
def test_write_zipped_internal_cache():
    # zip_safe pex will not be written to install cache unless always_write_cache
    with nested(yield_pex_builder(zip_safe=True), temporary_dir(),
                temporary_file()) as (pb, pex_root, pex_file):

        pex = pb.path()
        pb.info.pex_root = pex_root
        pb.build(pex_file.name)

        dists = PEXEnvironment.write_zipped_internal_cache(
            pex_file.name, pb.info)
        assert len(dists) == 1
        assert normalize(dists[0].location).startswith(
            normalize(os.path.join(pex_file.name, pb.info.internal_cache))), (
                'loc: %s, cache: %s' %
                (normalize(dists[0].location),
                 normalize(os.path.join(pex_file.name,
                                        pb.info.internal_cache))))

        pb.info.always_write_cache = True
        dists = PEXEnvironment.write_zipped_internal_cache(
            pex_file.name, pb.info)
        assert len(dists) == 1
        assert normalize(dists[0].location).startswith(
            normalize(pb.info.install_cache))

    # zip_safe pex will not be written to install cache unless always_write_cache
    with nested(yield_pex_builder(zip_safe=False), temporary_dir(),
                temporary_file()) as (pb, pex_root, pex_file):

        pex = pb.path()
        pb.info.pex_root = pex_root
        pb.build(pex_file.name)

        dists = PEXEnvironment.write_zipped_internal_cache(
            pex_file.name, pb.info)
        assert len(dists) == 1
        assert normalize(dists[0].location).startswith(
            normalize(pb.info.install_cache))
        original_location = normalize(dists[0].location)

        # do the second time to validate idempotence of caching
        dists = PEXEnvironment.write_zipped_internal_cache(
            pex_file.name, pb.info)
        assert len(dists) == 1
        assert normalize(dists[0].location) == original_location
Пример #5
0
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):
    pex = pb.path()
    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)))
Пример #6
0
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):
        pex = pb.path()
        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)))
Пример #7
0
def test_write_zipped_internal_cache():
  # zip_safe pex will not be written to install cache unless always_write_cache
  with nested(yield_pex_builder(zip_safe=True), temporary_dir(), temporary_file()) as (
      pb, pex_root, pex_file):

    pex = pb.path()
    pb.info.pex_root = pex_root
    pb.build(pex_file.name)

    dists = PEXEnvironment.write_zipped_internal_cache(pex_file.name, pb.info)
    assert len(dists) == 1
    assert normalize(dists[0].location).startswith(
        normalize(os.path.join(pex_file.name, pb.info.internal_cache))), (
        'loc: %s, cache: %s' % (
            normalize(dists[0].location),
            normalize(os.path.join(pex_file.name, pb.info.internal_cache))))

    pb.info.always_write_cache = True
    dists = PEXEnvironment.write_zipped_internal_cache(pex_file.name, pb.info)
    assert len(dists) == 1
    assert normalize(dists[0].location).startswith(normalize(pb.info.install_cache))

  # zip_safe pex will not be written to install cache unless always_write_cache
  with nested(yield_pex_builder(zip_safe=False), temporary_dir(), temporary_file()) as (
      pb, pex_root, pex_file):

    pex = pb.path()
    pb.info.pex_root = pex_root
    pb.build(pex_file.name)

    dists = PEXEnvironment.write_zipped_internal_cache(pex_file.name, pb.info)
    assert len(dists) == 1
    assert normalize(dists[0].location).startswith(normalize(pb.info.install_cache))
    original_location = normalize(dists[0].location)

    # do the second time to validate idempotence of caching
    dists = PEXEnvironment.write_zipped_internal_cache(pex_file.name, pb.info)
    assert len(dists) == 1
    assert normalize(dists[0].location) == original_location
Пример #8
0
def test_force_local():
  with nested(yield_pex_builder(), temporary_dir(), temporary_file()) as (pb, pex_root, pex_file):
    pex = pb.path()
    pb.info.pex_root = pex_root
    pb.build(pex_file.name)

    code_cache = PEXEnvironment.force_local(pex_file.name, pb.info)
    assert os.path.exists(pb.info.zip_unsafe_cache)
    assert len(os.listdir(pb.info.zip_unsafe_cache)) == 1
    assert [os.path.basename(code_cache)] == os.listdir(pb.info.zip_unsafe_cache)
    assert set(os.listdir(code_cache)) == set([PexInfo.PATH, '__main__.py'])

    # idempotence
    assert PEXEnvironment.force_local(pex_file.name, pb.info) == code_cache
Пример #9
0
def test_force_local():
    with nested(yield_pex_builder(), temporary_dir(),
                temporary_file()) as (pb, pex_root, pex_file):
        pex = pb.path()
        pb.info.pex_root = pex_root
        pb.build(pex_file.name)

        code_cache = PEXEnvironment.force_local(pex_file.name, pb.info)
        assert os.path.exists(pb.info.zip_unsafe_cache)
        assert len(os.listdir(pb.info.zip_unsafe_cache)) == 1
        assert [os.path.basename(code_cache)
                ] == os.listdir(pb.info.zip_unsafe_cache)
        assert set(os.listdir(code_cache)) == set(
            [PexInfo.PATH, '__main__.py'])

        # idempotence
        assert PEXEnvironment.force_local(pex_file.name, pb.info) == code_cache
Пример #10
0
def yield_pex_builder(zip_safe=True):
  with nested(temporary_dir(), make_distribution('p1', zipped=True, zip_safe=zip_safe)) as (td, p1):
    pb = PEXBuilder(path=td)
    pb.add_egg(p1.location)
    yield pb
Пример #11
0
def run_test(body, env=None):
  with nested(temporary_dir(), temporary_dir()) as (td1, td2):
    pb = write_pex(td1, body)
    pex = os.path.join(td2, 'app.pex')
    pb.build(pex)
    return run_pex(pex, env=env)
Пример #12
0
def run_test(body, env=None):
    with nested(temporary_dir(), temporary_dir()) as (td1, td2):
        pb = write_pex(td1, body)
        pex = os.path.join(td2, 'app.pex')
        pb.build(pex)
        return run_pex(pex, env=env)