Balint Balint - 2 months ago 15
Python Question

Python tarfile: how to use tar+gzip compression with follow symbolic link?

How could I use tar+gzip compression with "follow symbolic link" feature in Python 3.4? The problem is:


  • tarfile.open() supports "w:gz" mode but does not support "dereference" option

  • tarfile.tarfile() supports "dereference" but does not support "w:gz" mode



Code:

...
mode = ""
if bckentry['method'] == "tar":
mode = "w"
elif bckentry['method'] == "targz":
mode = "w:gz"

archive = tarfile.TarFile(name=filepath, mode=mode)
archive.dereference = True if bckentry['followsym'] == "yes" else False
# archive = tarfile.open(filepath, mode=mode)

if bckentry['withpath'] == 'yes':
for entry in bckentry['include_dirs']:
archive.add(entry, filter=self.filter_tar)
elif bckentry['withpath'] == 'no':
for entry in bckentry['include_dirs']:
archive.add(entry, arcname=os.path.basename(entry), filter=self.filter_tar)
...

Answer

tarfile.open is a shortcut for the TarFile.open classmethod, that in turn calls the TarFile constructor. The documentation is a bit vague, but from the code it is apparent that the first two will pass the dereference keyword argument and all other unconsumed kwargs to the TarFile constructor.

Thus you can use dereference with any of them, if you just pass it as a keyword argument:

archive = tarfile.open(name='foo.tar.gz', mode='w:gz', dereference=True)
Comments