def check_tarball(resdir, tfilter=None, rootmod=None):
    test_res = os.path.join(resources, resdir)
    man = parse_manifest(os.path.join(test_res, "manifest"))
    tarroot = os.path.join(test_res, "root")
    if tfilter is None:
        tfilter = _tar_filter

    with tmpdir() as staging:
        if rootmod is not None:
            newroot = os.path.join(staging, "root")
            rsync(tarroot, newroot)
            tarroot = newroot
            rootmod(tarroot)
        tname = os.path.join(staging, "test.tar.gz")
        tf = tarfile.open(tname, mode="w:gz")
        tf.add(tarroot, arcname=".", filter=tfilter)

        tf.close()
        with open_compressed_tarball(tname) as tar:
            man.check_tarball(tar)
def make_and_check_tarball(testname, rundir, upname, upversion, compression,
                           visitor, visit_open=True):
    """Create, check and clean up a tarball (test utility)

    Utility for setting up a dir, compile a tarball from a resource path,
    opening the tarball and passing it to vistor.

    testname is used to create a unique name for the test.  The
    compression is also used for this purpose, so multiple tests can
    share the same "testname" as long as the compression differs.

    rundir, upname, upversion and compression are passed (as is) to
    make_orig_tarball.

    The tarball passed to visitor may not be seekable and should be
    checked inorder.
    """

    testdir = "%s-%s" % (testname, compression)
    xtn = compression
    if xtn == "gzip":
        xtn = "gz"
    elif xtn == "bzip2":
        xtn = "bz2"

    rundir = abspath(rundir)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(testdir)

            make_orig_tarball(rundir, upname, upversion,
                              compression=compression, outputdir=testdir)
            tarname = "%s_%s.orig.tar.%s" % (upname, upversion, xtn)
            path = os.path.join(testdir, tarname)
            if visit_open:
                with open_compressed_tarball(path, compression) as tar:
                    visitor(tar)
            else:
                visitor(path)
def visit_tarball_path(tarpath, compression=None):
    with open(tarpath, "rb") as f:
        with open_compressed_tarball(tarpath, compression, fd=f) as tar:
            visit_open_tarball(tar)