apt_inst - Working with local Debian packages

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.

Exceptions

class apt_inst.Error

This is the same class as apt_pkg.Error, provided here for convenience.

AR Archives

class apt_inst.ArArchive(file)

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

archive[key]

Return a ArMember object for the member given by key. Raise LookupError if there is no ArMember with the given name.

key in archive

Return True if a member with the name key is found in the archive, it is the same function as getmember().

extract(name[, target: str]) → bool

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.

extractall([target: str]) → bool

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.

extractdata(name: str) → bytes

Return the contents of the member given by name, as a bytes object. Raise LookupError if there is no ArMember with the given name.

getmember(name: str) → ArMember

Return a ArMember object for the member given by name. Raise LookupError if there is no ArMember with the given name.

getmembers() → list

Return a list of all members in the AR archive.

getnames() → list

Return a list of the names of all members in the AR archive.

gettar(name: str, comp: str) → TarFile

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.

class apt_inst.ArMember

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.

gid

The group id of the owner.

mode

The mode of the file.

mtime

Last time of modification.

name

The name of the file.

size

The size of the files.

start

The offset in the archive where the file starts.

uid

The user id of the owner.

Debian Packages

class apt_inst.DebFile(file)

A DebFile object represents a file in the .deb package format. It inherits ArArchive. In addition to the attributes and methods from ArArchive, DebFile provides the following methods:

control

The TarFile object associated with the control.tar.gz member.

data

The TarFile object associated with the data.tar.{gz,bz2,lzma,xz} member.

debian_binary

The package version, as contained in debian-binary.

Tar Archives

class apt_inst.TarFile(file[, min: int, max: int, comp: str])

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.

extractall([rootdir: str]) → True

Extract the archive in the current directory. The argument rootdir can be used to change the target directory.

extractdata(member: str) → bytes

Return the contents of the member, as a bytes object. Raise LookupError if there is no member with the given name.

go(callback: callable[, member: str]) → True

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.

class apt_inst.TarMember

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.

isblk()

Determine whether the member is a block device.

ischr()

Determine whether the member is a character device.

isdev()

Determine whether the member is a device (block,character or FIFO).

isdir()

Determine whether the member is a directory.

isfifo()

Determine whether the member is a FIFO.

isfile()

Determine whether the member is a regular file.

islnk()

Determine whether the member is a hardlink.

isreg()

Determine whether the member is a regular file, same as isfile().

issym()

Determine whether the member is a symbolic link.

gid

The owner’s group id

linkname

The target of the link.

major

The major ID of the device.

minor

The minor ID of the device.

mode

The mode (permissions).

mtime

Last time of modification.

name

The name of the file.

size

The size of the file.

uid

The owner’s user id.

Removed functions

The following functions have been removed in python-apt 0.8. They are listed here to help developers 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.

apt_inst.arCheckMember(file, membername)

This function has been replaced by using the in check on an ArArchive object:

member in ArArchive(file)
apt_inst.debExtract(file, func, chunk)

This function 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().

apt_inst.tarExtract(file, func, comp)

This function 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(), it now expects a TarMember and a bytestring of the data.

apt_inst.debExtractArchive(file, rootdir)

This function has been replaced by TarFile.extractall() and DebFile.data:

debExtractArchive(open("package.deb"), rootdir) # old
DebFile("package.deb").data.extractall(rootdir) # new
apt_inst.debExtractControl(file[, member='control'])

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