duplicity.dup_tarfile module

Read from and write to tar format archives.

exception duplicity.dup_tarfile.CompressionError[source]

Bases: TarError

Exception for unavailable compression methods.

exception duplicity.dup_tarfile.ExtractError[source]

Bases: TarError

General exception for extract errors.

exception duplicity.dup_tarfile.HeaderError[source]

Bases: TarError

Base exception for header errors.

exception duplicity.dup_tarfile.ReadError[source]

Bases: TarError

Exception for unreadable tar archives.

exception duplicity.dup_tarfile.StreamError[source]

Bases: TarError

Exception for unsupported operations on stream-like TarFiles.

exception duplicity.dup_tarfile.TarError[source]

Bases: Exception

Base exception.

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

Bases: object

The TarFile Class provides an interface to tar archives.

OPEN_METH = {'bz2': 'bz2open', 'gz': 'gzopen', 'tar': 'taropen', 'xz': 'xzopen'}
__init__(name=None, mode='r', fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, errors='surrogateescape', pax_headers=None, debug=None, errorlevel=None, copybufsize=None)[source]

Open an (uncompressed) tar archive `name’. `mode’ is either ‘r’ to read from an existing archive, ‘a’ to append data to an existing file or ‘w’ to create a new file overwriting an existing one. `mode’ defaults to ‘r’. If `fileobj’ is given, it is used for reading or writing data. If it can be determined, `mode’ is overridden by `fileobj’s mode. `fileobj’ is not closed, when TarFile is closed.

_check(mode=None)[source]

Check if TarFile is still open, and if the operation’s mode corresponds to TarFile’s mode.

_dbg(level, msg)[source]

Write debugging output to sys.stderr.

_extract_member(tarinfo, targetpath, set_attrs=True, numeric_owner=False)[source]

Extract the TarInfo object tarinfo to a physical file called targetpath.

Find the target member of a symlink or hardlink member in the archive.

_getmember(name, tarinfo=None, normalize=False)[source]

Find an archive member by name from bottom to top. If tarinfo is given, it is used as the starting point.

_load()[source]

Read through the entire archive file and look for readable members.

add(name, arcname=None, recursive=True, *, 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. `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, it should be a binary file, and tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects directly, or by using gettarinfo().

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, numeric_owner)[source]

Set owner of targetpath according to tarinfo. If numeric_owner is True, use .gid/.uid instead of .gname/.uname. If numeric_owner is False, fall back to .gid/.uid when the search based on name fails.

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='', set_attrs=True, *, numeric_owner=False)[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’. File attributes (owner, mtime, mode) are set unless `set_attrs’ is False. If `numeric_owner is True, only the numbers for user/group names are used and not the names.

extractall(path='.', members=None, *, numeric_owner=False)[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(). If `numeric_owner is True, only the numbers for user/group names are used and not the names.

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 or a link, an io.BufferedReader object is returned. Otherwise, None is returned.

fileobject

alias of ExFileObject

format = 2
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. The name should be a text string.

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, *, members=None)[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. `members’ is optional and must be a subset of the list returned by getmembers().

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 ‘r:xz’ open for reading with lzma 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 ‘w:xz’ open for writing with lzma compression

‘x’ or ‘x:’ create a tarfile exclusively without compression, raise

an exception if the file is already created

‘x:gz’ create a gzip compressed tarfile, raise an exception

if the file is already created

‘x:bz2’ create a bzip2 compressed tarfile, raise an exception

if the file is already created

‘x:xz’ create an lzma compressed tarfile, raise an exception

if the file is already created

‘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 ‘r|xz’ open an lzma 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 ‘w|xz’ open an lzma compressed stream for writing

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.

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

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

class duplicity.dup_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.

__init__(name='')[source]

Construct a TarInfo object. name is the optional name of the member.

_apply_pax_info(pax_headers, encoding, errors)[source]

Replace fields with supplemental information from a previous pax extended or global header.

_block(count)[source]

Round up a byte count by BLOCKSIZE and return it, e.g. _block(834) => 1024.

classmethod _create_gnu_long_header(name, type, encoding, errors)[source]

Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence for name.

static _create_header(info, format, encoding, errors)[source]

Return a header block. info is a dictionary with file information, format must be one of the *_FORMAT constants.

classmethod _create_pax_generic_header(pax_headers, type, encoding)[source]

Return a POSIX.1-2008 extended or global header sequence that contains a list of keyword, value pairs. The values must be strings.

static _create_payload(payload)[source]

Return the string payload filled with zero bytes up to the next 512 byte border.

_decode_pax_field(value, encoding, fallback_encoding, fallback_errors)[source]

Decode a single field from a pax record.

_posix_split_name(name, encoding, errors)[source]

Split a name longer than 100 chars into a prefix and a name part.

_proc_builtin(tarfile)[source]

Process a builtin type or an unknown type which will be treated as a regular file.

_proc_gnulong(tarfile)[source]

Process the blocks that hold a GNU longname or longlink member.

_proc_gnusparse_00(next, pax_headers, buf)[source]

Process a GNU tar extended sparse header, version 0.0.

_proc_gnusparse_01(next, pax_headers)[source]

Process a GNU tar extended sparse header, version 0.1.

_proc_gnusparse_10(next, pax_headers, tarfile)[source]

Process a GNU tar extended sparse header, version 1.0.

_proc_member(tarfile)[source]

Choose the right processing method depending on the type and call it.

_proc_pax(tarfile)[source]

Process an extended or global header as described in POSIX.1-2008.

_proc_sparse(tarfile)[source]

Process a GNU sparse header plus extra headers.

_sparse_structs
chksum

Header checksum.

create_gnu_header(info, encoding, errors)[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)[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, encoding, errors)[source]

Return the object as a ustar header block.

devmajor

Device major number.

devminor

Device minor number.

classmethod frombuf(buf, encoding, errors)[source]

Construct a TarInfo object from a 512 byte bytes object.

classmethod fromtarfile(tarfile)[source]

Return the next TarInfo object from TarFile object tarfile.

get_info()[source]

Return the TarInfo’s attributes as a dictionary.

gid

Group ID of the user who originally stored this member.

gname

Group name.

isblk()[source]

Return True if it is a block device.

ischr()[source]

Return True if it is a character device.

isdev()[source]

Return True if it is one of character device, block device or FIFO.

isdir()[source]

Return True if it is a directory.

isfifo()[source]

Return True if it is a FIFO.

isfile()[source]

Return True if the Tarinfo object is a regular file.

islnk()[source]

Return True if it is a hard link.

isreg()[source]

Return True if the Tarinfo object is a regular file.

issparse()[source]
issym()[source]

Return True if it is a symbolic link.

linkname

Name of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.

property linkpath

In pax headers, “linkname” is called “linkpath”.

mode

Permission bits.

mtime

Time of last modification.

name

Name of the archive member.

offset

The tar header starts here.

offset_data

The file’s data starts here.

property path

In pax headers, “name” is called “path”.

pax_headers

A dictionary containing key-value pairs of an associated pax extended header.

size

Size in bytes.

sparse

Sparse member information.

tarfile
tobuf(format=2, encoding='utf-8', errors='surrogateescape')[source]

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

type

File type. type is usually one of these constants: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE.

uid

User ID of the user who originally stored this member.

uname

User name.

duplicity.dup_tarfile.is_tarfile(name)[source]

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

duplicity.dup_tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)

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 ‘r:xz’ open for reading with lzma 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 ‘w:xz’ open for writing with lzma compression

‘x’ or ‘x:’ create a tarfile exclusively without compression, raise

an exception if the file is already created

‘x:gz’ create a gzip compressed tarfile, raise an exception

if the file is already created

‘x:bz2’ create a bzip2 compressed tarfile, raise an exception

if the file is already created

‘x:xz’ create an lzma compressed tarfile, raise an exception

if the file is already created

‘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 ‘r|xz’ open an lzma 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 ‘w|xz’ open an lzma compressed stream for writing