Python Forum
tar module - how usable is it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tar module - how usable is it?
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Do you mean the tarfile module? What have you tried? Start with pymotw3
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
my alternative is to implement the low level details of tar myself. i think that would be worse.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020