Python tarfile progress

Is there any library to display progress when adding files to the tar archive in python or alternativly, is it possible to extend the capabilities of the tarfile module for this?

In an ideal world, I would like to show the general progress in creating tar, as well as ETA as to when it will be completed.

Any help on this would be truly appreciated.

+3
source share
5 answers

Unfortunately, it doesn't seem like there is an easy way to get bytes from byte numbers.

Are you adding really large files to this tar file? If not, I would update the progress on a file basis, since as you add files to tar, the progress will be updated depending on the size of each file.

, toadd tarfile tarfile. ,

from itertools import imap
from operator import attrgetter
# you may want to change this depending on how you want to update the
# file info for your tarobjs
tarobjs = imap(tarfile.getattrinfo, toadd)
total = sum(imap(attrgetter('size'), tarobjs))
complete = 0.0
for tarobj in tarobjs:
    sys.stdout.write("\rPercent Complete: {0:2.0d}%".format(complete))
    tarfile.add(tarobj)
    complete += tarobj.size / total * 100
sys.stdout.write("\rPercent Complete: {0:2.0d}%\n".format(complete))
sys.stdout.write("Job Done!")
+2

-, , , Tarfile.addfile(), , . , / , Tarfile .

+2

, filter tarfile.add()

with tarfile.open(<tarball path>, 'w') as tarball:
   tarball.add(<some file>, filter = my_filter)

def my_filter(tarinfo):
   #increment some count
   #add tarinfo.size to some byte counter
   return tarinfo

, TarInfo.

+1

How do you add files to a tar file? Is there a "add" with recursive = true? You can create a list of files yourself and call "add" one by one, showing progress when you go. If you are building from a stream / file, then it looks like you can wrap this fileobj to see the read status and pass it to the file.

It doesn't seem like you need to modify tarfile.py at all.

0
source

Source: https://habr.com/ru/post/1786008/


All Articles