Unpack a tar archive, optionally compressed
3930
def extract_tarfile(archive_name, destpath):
    ""
    archive = tarfile.open(archive_name)
    archive.extractall(destpath)
942599
def extract_zip(archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    try:
        with zipfile.ZipFile(archive) as zfile:
            zfile.extractall(outdir)
    except Exception as err:
        msg = "error extracting %s: %s" % (archive, err)
        raise util.PatoolError(msg)
    return None
90237
def extract_shn (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    cmdlist = [util.shell_quote(cmd)]
    outfile = util.get_single_outfile(outdir, archive, extension=".wav")
    cmdlist.extend(['-x', '-', util.shell_quote(outfile), '<',
        util.shell_quote(archive)])
    return (cmdlist, {'shell': True})
1103659
def compress_delete_outdir(outdir):
    """"""
    # Compress output in .tar.gz file and remove raw output
    tarfn = outdir + ".tar.gz"
    logger.info("\tCompressing output from %s to %s", outdir, tarfn)
    with tarfile.open(tarfn, "w:gz") as fh:
        fh.add(outdir)
    logger.info("\tRemoving output directory %s", outdir)
    shutil.rmtree(outdir)
785195
def list_tar (archive, compression, cmd, verbosity, interactive):
    """"""
    try:
        with tarfile.open(archive) as tfile:
            tfile.list(verbose=verbosity>1)
    except Exception as err:
        msg = "error listing %s: %s" % (archive, err)
        raise util.PatoolError(msg)
    return None
736109
def tar_gzip_dir(directory, destination, base=None):
    """"""
    dest_file = tarfile.open(destination, 'w:gz')
    abs_dir_path = os.path.abspath(directory)
    base_name = abs_dir_path + "/"
    base = base or os.path.basename(directory)
    for path, dirnames, filenames in os.walk(abs_dir_path):
        rel_path = path[len(base_name):]
        dir_norm_path = os.path.join(base, rel_path)
        dir_tar_info = dest_file.gettarinfo(name=path)
        dir_tar_info.name = dir_norm_path
        dest_file.addfile(dir_tar_info)
        for filename in filenames:
            actual_path = os.path.join(path, filename)
            norm_path = os.path.join(base, rel_path, filename)
            tar_info = dest_file.gettarinfo(name=actual_path)
            tar_info.name = norm_path
            new_file = open(actual_path, 'rb')
            dest_file.addfile(tar_info, new_file)
    dest_file.close()
83755
def load_tar_lzma_data(tlfile):
    """"""
    tmpname = extract_lzma(tlfile)

    # open tar file
    fields_real = []
    fields_imag = []
    phantom = []
    parms = {}

    with tarfile.open(tmpname, "r") as t:
        members = t.getmembers()
        members.sort(key=lambda x: x.name)

        for m in members:
            n = m.name
            f = t.extractfile(m)
            if n.startswith("fdtd_info"):
                for ln in f.readlines():
                    ln = ln.decode()
                    if ln.count("=") == 1:
                        key, val = ln.split("=")
                        parms[key.strip()] = float(val.strip())
            elif n.startswith("phantom"):
                phantom.append(np.loadtxt(f))
            elif n.startswith("field"):
                if n.endswith("imag.txt"):
                    fields_imag.append(np.loadtxt(f))
                elif n.endswith("real.txt"):
                    fields_real.append(np.loadtxt(f))

    try:
        os.remove(tmpname)
    except OSError:
        pass

    phantom = np.array(phantom)
    sino = np.array(fields_real) + 1j * np.array(fields_imag)
    angles = np.linspace(0, 2 * np.pi, sino.shape[0], endpoint=False)

    return sino, angles, phantom, parms
653719
def _extract(archive, compression, cmd, format, verbosity, outdir):
    """"""
    targetname = util.get_single_outfile(outdir, archive)
    try:
        with lzma.LZMAFile(archive, **_get_lzma_options(format)) as lzmafile:
            with open(targetname, 'wb') as targetfile:
                data = lzmafile.read(READ_SIZE_BYTES)
                while data:
                    targetfile.write(data)
                    data = lzmafile.read(READ_SIZE_BYTES)
    except Exception as err:
        msg = "error extracting %s to %s: %s" % (archive, targetname, err)
        raise util.PatoolError(msg)
    return None
75282
def get_memory_tar(image_path):
    '''
    '''
    byte_array = Client.image.export(image_path)
    file_object = io.BytesIO(byte_array)
    tar = tarfile.open(mode="r|*", fileobj=file_object)
    return (file_object,tar)
851402
def extract_rzip (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    cmdlist = [cmd, '-d', '-k']
    if verbosity > 1:
        cmdlist.append('-v')
    outfile = util.get_single_outfile(outdir, archive)
    cmdlist.extend(["-o", outfile, archive])
    return cmdlist
1347254
def unpack_tarball(tgz)
      Dir.chdir(config.ruby_toplevel_dir) do
        each_compressed_file(tgz) do |path, entry|
          debug "Unpacking file: #{path}"
          File.open(path, 'wb') do |f|
            f.write(entry.read)
          end
        end
      end
    end
824893
def unpack(name, dest=None, path=None, pack_format='tar', compress='bz2'):
    '''
    
    '''
    if pack_format == 'tar':
        _untar(name, dest, path, compress)
1165537
def untar_file(filename, location):
    """"""
    if not os.path.exists(location):
        os.makedirs(location)
    if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'):
        mode = 'r:gz'
    elif filename.lower().endswith('.bz2') or filename.lower().endswith('.tbz'):
        mode = 'r:bz2'
    elif filename.lower().endswith('.tar'):
        mode = 'r'
    else:
        logger.warn('Cannot determine compression type for file %s' % filename)
        mode = 'r:*'
    tar = tarfile.open(filename, mode)
    try:
        # note: python<=2.5 doesnt seem to know about pax headers, filter them
        leading = has_leading_dir([
            member.name for member in tar.getmembers()
            if member.name != 'pax_global_header'
        ])
        for member in tar.getmembers():
            fn = member.name
            if fn == 'pax_global_header':
                continue
            if leading:
                fn = split_leading_dir(fn)[1]
            path = os.path.join(location, fn)
            if member.isdir():
                if not os.path.exists(path):
                    os.makedirs(path)
            else:
                try:
                    fp = tar.extractfile(member)
                except (KeyError, AttributeError):
                    e = sys.exc_info()[1]
                    # Some corrupt tar files seem to produce this
                    # (specifically bad symlinks)
                    logger.warn(
                        'In the tar file %s the member %s is invalid: %s'
                        % (filename, member.name, e))
                    continue
                if not os.path.exists(os.path.dirname(path)):
                    os.makedirs(os.path.dirname(path))
                destfp = open(path, 'wb')
                try:
                    shutil.copyfileobj(fp, destfp)
                finally:
                    destfp.close()
                fp.close()
    finally:
        tar.close()
245962
def _unpack_zipfile(filename, extract_dir):
    
    try:
        import zipfile
    except ImportError:
        raise ReadError('zlib not supported, cannot unpack this archive.')

    if not zipfile.is_zipfile(filename):
        raise ReadError("%s is not a zip file" % filename)

    zip = zipfile.ZipFile(filename)
    try:
        for info in zip.infolist():
            name = info.filename

            # don't extract absolute paths or ones with .. in them
            if name.startswith('/') or '..' in name:
                continue

            target = os.path.join(extract_dir, *name.split('/'))
            if not target:
                continue

            _ensure_directory(target)
            if not name.endswith('/'):
                # file
                data = zip.read(info.filename)
                f = open(target, 'wb')
                try:
                    f.write(data)
                finally:
                    f.close()
                    del data
    finally:
        zip.close()
290275
def extract_ar (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    opts = 'x'
    if verbosity > 1:
        opts += 'v'
    cmdlist = [cmd, opts, os.path.abspath(archive)]
    return (cmdlist, {'cwd': outdir})
213396
def decompress(compressed_data):
    """"""
    raw_data = []

    index = 0

    while index < len(compressed_data):
        current = compressed_data[index]
        index += 1

        if current == RLE_BYTE:
            directive = compressed_data[index]
            index += 1

            if directive == RLE_BYTE:
                raw_data.append(RLE_BYTE)
            else:
                count = compressed_data[index]
                index += 1

                raw_data.extend([directive] * count)
        elif current == SPECIAL_BYTE:
            directive = compressed_data[index]
            index += 1

            if directive == SPECIAL_BYTE:
                raw_data.append(SPECIAL_BYTE)
            elif directive == DEFAULT_WAVE_BYTE:
                count = compressed_data[index]
                index += 1

                raw_data.extend(DEFAULT_WAVE * count)
            elif directive == DEFAULT_INSTR_BYTE:
                count = compressed_data[index]
                index += 1

                raw_data.extend(DEFAULT_INSTRUMENT_FILEPACK * count)
            elif directive == EOF_BYTE:
                assert False, ("Unexpected EOF command encountered while "
                               "decompressing")
            else:
                assert False, "Countered unexpected sequence 0x%02x 0x%02x" % (
                    current, directive)
        else:
            raw_data.append(current)

    return raw_data
572817
def _unpack(self, content):
        """"""
        if self.compressor:
            try:
                content = self.compressor.decompress(content)
            except CompressError:
                pass
        if self.serializer:
            content = self.serializer.deserialize(content)
        return content