This module provides useful classes and functions to work with archives, modelled after the tarfile.TarFile class. For working with Debian packages, the DebFile class should be used as it provides easy access to the control.tar.* and data.tar.* members.
The classes are mostly modeled after the tarfile.TarFile class and enhanced with APT-specific methods. Because APT only provides a stream based view on a tar archive, this module’s TarFile class only provides a very small subset of those functions.
An ArArchive object represents an archive in the 4.4 BSD AR format, which is used for e.g. deb packages.
The parameter file may be a string specifying the path of a file, or a file-like object providing the fileno() method. It may also be an int specifying a file descriptor (returned by e.g. os.open()). The recommended way is to pass in the path to the file.
ArArchive (and its subclasses) support the iterator protocol, meaning that an ArArchive object can be iterated over yielding the members in the archive (same as getmembers()).
Return a ArMember object for the member given by key. Raise LookupError if there is no ArMember with the given name.
Return True if a member with the name key is found in the archive, it is the same function as getmember().
Extract the member given by name into the directory given by target. If the extraction failed, an error is raised. Otherwise, the method returns True if the owner could be set or False if the owner could not be changed. It may also raise LookupError if there is no member with the given name.
The parameter target is completely optional. If it is not given, the function extracts into the current directory.
Extract all into the directory given by target or the current directory if target is not given. If the extraction failed, an error is raised. Otherwise, the method returns True if the owner could be set or False if the owner could not be changed.
Return the contents of the member given by name, as a bytes object. Raise LookupError if there is no ArMember with the given name.
Return a ArMember object for the member given by name. Raise LookupError if there is no ArMember with the given name.
Return a list of all members in the AR archive.
Return a list of the names of all members in the AR archive.
Return a TarFile object for the member given by name which will be decompressed using the compression algorithm given by comp. This is almost equal to:
member = arfile.getmember(name)
tarfile = TarFile(file, member.start, member.size, 'gzip')'
It just opens a new TarFile on the given position in the stream.
An ArMember object represents a single file within an AR archive. For Debian packages this can be e.g. control.tar.gz. This class provides information about this file, such as the mode and size. It has no constructor.
The group id of the owner.
The mode of the file.
Last time of modification.
The name of the file.
The size of the files.
The offset in the archive where the file starts.
The user id of the owner.
A TarFile object represents a single .tar file stream.
The parameter file may be a string specifying the path of a file, or a file-like object providing the fileno() method. It may also be an int specifying a file descriptor (returned by e.g. os.open()).
The parameter min describes the offset in the file where the archive begins and the parameter max is the size of the archive.
The compression of the archive is set by the parameter comp. It can be set to any program supporting the -d switch, the default being gzip.
Extract the archive in the current directory. The argument rootdir can be used to change the target directory.
Return the contents of the member, as a bytes object. Raise LookupError if there is no member with the given name.
Go through the archive and call the callable callback for each member with 2 arguments. The first argument is the TarMember and the second one is the data, as bytes.
The optional parameter member can be used to specify the member for which call the callback. If not specified, it will be called for all members. If specified and not found, LookupError will be raised.
Represent a single member of a ‘tar’ archive.
This class which has been modelled after tarfile.TarInfo represents information about a given member in an archive.
Determine whether the member is a block device.
Determine whether the member is a character device.
Determine whether the member is a device (block,character or FIFO).
Determine whether the member is a directory.
Determine whether the member is a FIFO.
Determine whether the member is a regular file.
Determine whether the member is a hardlink.
Determine whether the member is a regular file, same as isfile().
Determine whether the member is a symbolic link.
The owner’s group id
The target of the link.
The major ID of the device.
The minor ID of the device.
The mode (permissions).
Last time of modification.
The name of the file.
The size of the file.
The owner’s user id.
The following functions have been shipped in python-apt for a longer time and are deprecated as of release 0.7.92. They are listed here to help developers to port their applications to the new API which is completely different. For this purpose each function documentation includes an example showing how the function can be replaced.
Check if the member specified by the parameter membername exists in the AR file referenced by the parameter file, which may be a file() object, a file descriptor, or anything implementing a fileno() method.
This function has been replaced by using the in check on an ArArchive object:
member in ArArchive(file)
Call the function referenced by func for each member of the tar file chunk which is contained in the AR file referenced by the parameter file, which may be a file() object, a file descriptor, or anything implementing a fileno() method.
The function func is a callback with the signature (what, name, link, mode, uid, gid, size, mtime, major, minor). The parameter what describes the type of the member. It can be ‘FILE’, ‘DIR’, or ‘HARDLINK’. The parameter name refers to the name of the member. In case of links, link refers to the target of the link.
This function is deprecated and has been replaced by the TarFile.go() method. The following example shows the old code and the new code:
debExtract(open("package.deb"), my_callback, "data.tar.gz") #old
DebFile("package.deb").data.go(my_callback)
Please note that the signature of the callback is different in TarFile.go().
Call the function func for each member of the tar file file.
The parameter comp is a string determining the compressor used. Possible options are “xz”, “lzma”, “bzip2” and “gzip”. The parameter file may be a file() object, a file descriptor, or anything implementing a fileno() method.
The function func is a callback with the signature (what, name, link, mode, uid, gid, size, mtime, major, minor). The parameter what describes the type of the member. It can be ‘FILE’, ‘DIR’, or ‘HARDLINK’. The parameter name refers to the name of the member. In case of links, link refers to the target of the link.
This function is deprecated and has been replaced by the TarFile.go() method. The following example shows the old code and the new code:
tarExtract(open("archive.tar.gz"), my_callback, "gzip") #old
TarFile("archive.tar.gz", 0, 0, "gzip").go(my_callback)
Please note that the signature of the callback is different in TarFile.go().
Extract the archive referenced by the file object file into the directory specified by rootdir.
The parameter file may be a file() object, a file descriptor, or anything implementing a fileno() method.
This function has been replaced by TarFile.extractall() and DebFile.data:
debExtractArchive(open("package.deb"), rootdir) # old
DebFile("package.deb").data.extractall(rootdir) # new
Return the indicated file as a string from the control tar. The default is ‘control’. The parameter file may be a file() object, a file descriptor, or anything implementing a fileno() method.
This function has been replaced by DebFile.control and TarFile.extractdata(). In the following example, both commands return the contents of the control file:
debExtractControl(open("package.deb"))
DebFile("package.deb").control.extractdata("control")