duplicity.tarfile module

Read from and write to tar format archives.

class duplicity.tarfile.TarFile(name=None, mode='r', fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, errors=None, pax_headers=None, debug=None, errorlevel=None)[source]

Bases: object

The TarFile Class provides an interface to tar archives.

OPEN_METH = {'gz': 'gzopen', 'tar': 'taropen', 'bz2': 'bz2open'}
add(name, arcname=None, recursive=True, exclude=None, filter=None)[source]

Add the file `name’ to the archive. `name’ may be any type of file (directory, fifo, symbolic link, etc.). If given, `arcname’ specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting `recursive’ to False. `exclude’ is a function that should return True for each filename to be excluded. `filter’ is a function that expects a TarInfo object argument and returns the changed TarInfo object, if it returns None the TarInfo object will be excluded from the archive.

addfile(tarinfo, fileobj=None)[source]

Add the TarInfo object `tarinfo’ to the archive. If `fileobj’ is given, tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects directly, or by using gettarinfo(). On Windows platforms, `fileobj’ should always be opened with mode ‘rb’ to avoid irritation about the file size.

classmethod bz2open(name, mode='r', fileobj=None, compresslevel=9, **kwargs)[source]

Open bzip2 compressed tar archive name for reading or writing. Appending is not allowed.

chmod(tarinfo, targetpath)[source]

Set file permissions of targetpath according to tarinfo.

chown(tarinfo, targetpath)[source]

Set owner of targetpath according to tarinfo.

close()[source]

Close the TarFile. In write-mode, two finishing zero blocks are appended to the archive.

debug = 0
dereference = False
encoding = 'UTF-8'
errorlevel = 1
errors = None
extract(member, path='')[source]

Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member’ may be a filename or a TarInfo object. You can specify a different directory using `path’.

extractall(path='.', members=None)[source]

Extract all members from the archive to the current working directory and set owner, modification time and permissions on directories afterwards. `path’ specifies a different directory to extract to. `members’ is optional and must be a subset of the list returned by getmembers().

extractfile(member)[source]

Extract a member from the archive as a file object. `member’ may be a filename or a TarInfo object. If `member’ is a regular file, a file-like object is returned. If `member’ is a link, a file-like object is constructed from the link’s target. If `member’ is none of the above, None is returned. The file-like object is read-only and provides the following methods: read(), readline(), readlines(), seek() and tell()

fileobject

alias of ExFileObject

format = 1
getmember(name)[source]

Return a TarInfo object for member `name’. If `name’ can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version.

getmembers()[source]

Return the members of the archive as a list of TarInfo objects. The list has the same order as the members in the archive.

getnames()[source]

Return the members of the archive as a list of their names. It has the same order as the list returned by getmembers().

gettarinfo(name=None, arcname=None, fileobj=None)[source]

Create a TarInfo object from the result of os.stat or equivalent on an existing file. The file is either named by `name’, or specified as a file object `fileobj’ with a file descriptor. If given, `arcname’ specifies an alternative name for the file in the archive, otherwise, the name is taken from the ‘name’ attribute of ‘fileobj’, or the ‘name’ argument.

classmethod gzopen(name, mode='r', fileobj=None, compresslevel=9, **kwargs)[source]

Open gzip compressed tar archive name for reading or writing. Appending is not allowed.

ignore_zeros = False
list(verbose=True)[source]

Print a table of contents to sys.stdout. If `verbose’ is False, only the names of the members are printed. If it is True, an `ls -l’-like output is produced.

makedev(tarinfo, targetpath)[source]

Make a character or block device called targetpath.

makedir(tarinfo, targetpath)[source]

Make a directory called targetpath.

makefifo(tarinfo, targetpath)[source]

Make a fifo called targetpath.

makefile(tarinfo, targetpath)[source]

Make a file called targetpath.

Make a (symbolic) link called targetpath. If it cannot be created (platform limitation), we try to make a copy of the referenced file instead of a link.

makeunknown(tarinfo, targetpath)[source]

Make a file from a TarInfo object with an unknown type at targetpath.

next()[source]

Return the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available.

classmethod open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)[source]

Open a tar archive for reading, writing or appending. Return an appropriate TarFile class.

mode: ‘r’ or ‘r:*‘ open for reading with transparent compression ‘r:’ open for reading exclusively uncompressed ‘r:gz’ open for reading with gzip compression ‘r:bz2’ open for reading with bzip2 compression ‘a’ or ‘a:’ open for appending, creating the file if necessary ‘w’ or ‘w:’ open for writing without compression ‘w:gz’ open for writing with gzip compression ‘w:bz2’ open for writing with bzip2 compression

‘r|*’ open a stream of tar blocks with transparent compression ‘r|’ open an uncompressed stream of tar blocks for reading ‘r|gz’ open a gzip compressed stream of tar blocks ‘r|bz2’ open a bzip2 compressed stream of tar blocks ‘w|’ open an uncompressed stream for writing ‘w|gz’ open a gzip compressed stream for writing ‘w|bz2’ open a bzip2 compressed stream for writing

posix
tarinfo

alias of TarInfo

classmethod taropen(name, mode='r', fileobj=None, **kwargs)[source]

Open uncompressed tar archive name for reading or writing.

utime(tarinfo, targetpath)[source]

Set modification time of targetpath according to tarinfo.

class duplicity.tarfile.TarInfo(name='')[source]

Bases: object

Informational class which holds the details about an archive member given by a tar header block. TarInfo objects are returned by TarFile.getmember(), TarFile.getmembers() and TarFile.gettarinfo() and are usually created internally.

create_gnu_header(info)[source]

Return the object as a GNU header block sequence.

classmethod create_pax_global_header(pax_headers)[source]

Return the object as a pax global header block sequence.

create_pax_header(info, encoding, errors)[source]

Return the object as a ustar header block. If it cannot be represented this way, prepend a pax extended header sequence with supplement information.

create_ustar_header(info)[source]

Return the object as a ustar header block.

classmethod frombuf(buf)[source]

Construct a TarInfo object from a 512 byte string buffer.

classmethod fromtarfile(tarfile)[source]

Return the next TarInfo object from TarFile object tarfile.

get_info(encoding, errors)[source]

Return the TarInfo’s attributes as a dictionary.

isblk()[source]
ischr()[source]
isdev()[source]
isdir()[source]
isfifo()[source]
isfile()[source]
islnk()[source]
isreg()[source]
issparse()[source]
issym()[source]
linkpath
path
tobuf(format=1, encoding='UTF-8', errors='strict')[source]

Return a tar header as a string of 512 byte blocks.

duplicity.tarfile.is_tarfile(name)[source]

Return True if name points to a tar archive that we are able to handle, else return False.

exception duplicity.tarfile.TarError[source]

Bases: exceptions.Exception

Base exception.