Python Forum
tar module - how usable is it? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: tar module - how usable is it? (/thread-14413.html)



tar module - how usable is it? - Skaperen - Nov-28-2018

i am wondering how usable the tar module is. i have a use case where i need to archive files in sorted order. i already have a generator that iterates a file tree in the order i need. what i am thinking of is implementing a special tar command that only creates tarballs and does it in sorted order.


RE: tar module - how usable is it? - Gribouillis - Nov-28-2018

Do you mean the tarfile module? What have you tried? Start with pymotw3


RE: tar module - how usable is it? - Skaperen - Nov-29-2018

i have tried nothing. it's too early to try anything. it would take a lot of development just to test. i have other alternatives to investigate, too. i'm trying to narrow down the choices then try something.


RE: tar module - how usable is it? - DeaD_EyE - Nov-29-2018

Why should it not be usable? By the way, doing it as generator, you can learn much about generators.

from tarfile import TarFile
from pathlib import Path


def list_files_gen(root, globbing):
    root = Path(root)
    for path in root.glob(globbing):
        yield path


def tar_gen(filename):
    print('Generator started')
    with TarFile(filename, 'w') as tar:
        while True:
            try:
                print('Before yield')
                file = yield
                print(f'After yield. File: {file}')
            except GeneratorExit:
                print('Generator Exit')
                break
            tar.add(file, recursive=False)
    print('TarFile is closed')


def pack_all(archive, root, globbing):
    tar = tar_gen(archive)
    tar.send(None)
    for file in list_files_gen(root, globbing):
        tar.send(file)
    tar.close()


pack_all('foo.tar.gz', '.', '*.py')
Output:
andre@andre-GP70-2PE:~$ python pack.py Generator started Before yield After yield. File: viruses.py Before yield After yield. File: not_working.py Before yield After yield. File: test.py Before yield After yield. File: tests.py Before yield After yield. File: t.py Before yield After yield. File: pack.py Before yield Generator Exit TarFile is closed
Output:
andre@andre-GP70-2PE:~$ tar --list -f foo.tar.gz viruses.py not_working.py test.py tests.py t.py pack.py



RE: tar module - how usable is it? - Skaperen - Dec-07-2018

my alternative is to implement the low level details of tar myself. i think that would be worse.