Ejemplo n.º 1
0
    def extract(self, offset, out=None):
        '''Extracts the tar item starting at offset to out'''
        if not 'r' == self.mode:
            raise ValueError, 'Cannot extract in append mode'
        assert offset % BLOCKSIZE == 0, 'Bad offset (%d)' % offset

        if out is None:
            return _pylibtar.pytar_extract_tobuf(self.tar, offset)
        else:
            if isinstance(out, basestring):
                fdout = open(out, 'w+b').fileno()
            elif hasattr(out, 'fileno'):
                fdout = out.fileno()
            else:
                fdout = out
            _pylibtar.pytar_extract(self.tar, offset, fdout)
            if hasattr(out, 'flush'):
                out.flush()
            return out
Ejemplo n.º 2
0
    def extract_iter(self, offset):
        '''Extracts the tar item starting at offset and returns an iterable'''
        if not 'r' == self.mode:
            raise ValueError, 'Cannot extract in append mode'
        assert offset % BLOCKSIZE == 0, 'Bad offset (%d)' % offset

        info = _pylibtar.pytar_info(self.tar, offset)
        if info['size'] <= self.CHUNKSIZE:
            yield _pylibtar.pytar_extract_tobuf(self.tar, offset)
        else:
            import tempfile
            tempfd, tempfn = tempfile.mkstemp()
            _pylibtar.pytar_extract(self.tar, offset, tempfd)
            os.close(tempfd)
            fh = open(tempfn, 'rb')
            os.unlink(tempfn)
            while 1:
                chunk = fh.read(self.CHUNKSIZE)
                if not chunk: break
                yield chunk
Ejemplo n.º 3
0
import _pylibtar
TESTTAR = '/tmp/_pylibtar-test.tar'

print 'opening %s for append' % TESTTAR
tar = _pylibtar.pytar_open(TESTTAR, 'a')
print 'tar:', tar
print 'append:', _pylibtar.pytar_append_file(tar, __file__,
    os.path.basename(__file__))
print 'close:', _pylibtar.pytar_close(tar)

print 'ITER'
tar = _pylibtar.pytar_open(TESTTAR, 'r')
print 'tar:', tar, 'ITER_RESET'
print _pylibtar.pytar_iter_reset(tar)
info = None
while 1:
    try:
        print 'ITER_NEXT', os.lseek(_pylibtar.tar_fd(tar), 0, 1)
        info = _pylibtar.pytar_iter_next(tar)
    except StopIteration:
        break
    print 'INFO: %r' % info
    if not info: break

if info:
    import sys
    print 'EXTRACTING %s' % info
    _pylibtar.pytar_extract(tar, info['offset'], sys.stdout.fileno())

print 'close:', _pylibtar.pytar_close(tar)