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.
Do you mean the tarfile module? What have you tried? Start with pymotw3
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.
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
my alternative is to implement the low level details of tar myself. i think that would be worse.