# -*- coding: utf-8 -*- import os 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())
def __iter__(self): '''Returns an iterator over the tar entries (info dicts with offset, too)''' if not 'r' == self.mode: raise ValueError, 'Cannot iterate in append mode' assert 0 == _pylibtar.pytar_iter_reset(self.tar) return self