Balint
Balint

Reputation: 55

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:

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)
...

Upvotes: 5

Views: 3069

Answers (1)

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)

Upvotes: 7

Related Questions