apt.cache — The Cache class

The Cache class

class apt.cache.Cache(progress=None, rootdir=None, memonly=False)

Dictionary-like package cache.

This class has all the packages that are available in it’s dictionary.

Keyword arguments: progress – a OpProgress object rootdir – a alternative root directory. if that is given

the system sources.list and system lists/ files are not read, only files relative to the given rootdir

memonly – build the cache in memory only

cache[pkgname]
Return a Package() for the package with the name pkgname.
actiongroup()

Return an ActionGroup() object for the current cache.

Action groups can be used to speedup actions. The action group is active as soon as it is created, and disabled when the object is deleted or when release() is called.

You can use the action group as a context manager, this is the recommended way:

with cache.actiongroup():
    for package in my_selected_packages:
        package.mark_install()

This way, the ActionGroup is automatically released as soon as the with statement block is left. It also has the benefit of making it clear which parts of the code run with a action group and which don’t.

broken_count
Return the number of packages with broken dependencies.
cache_post_change()
called internally if the cache has changed, emit a signal then
cache_pre_change()
called internally if the cache is about to change, emit a signal then
clear()
Unmark all changes
commit(fetch_progress=None, install_progress=None)

Apply the marked changes to the cache.

The first parameter, fetch_progress, refers to a FetchProgress() object as found in apt.progress, the default being apt.progress.FetchProgress().

The second parameter, install_progress, is a apt.progress.InstallProgress() object.

connect(name, callback)
connect to a signal, currently only used for cache_{post,pre}_{changed,open}
delete_count
Return the number of packages marked for deletion.
dpkg_journal_dirty

Return True if the dpkg was interrupted

All dpkg operations will fail until this is fixed, the action to fix the system if dpkg got interrupted is to run ‘dpkg –configure -a’ as root.

get_changes()
Get the marked changes
get_providing_packages(pkgname, candidate_only=True, include_nonvirtual=False)

Return a list of all packages providing a package.

Return a list of packages which provide the virtual package of the specified name.

If ‘candidate_only’ is False, return all packages with at least one version providing the virtual package. Otherwise, return only those packages where the candidate version provides the virtual package.

If ‘include_nonvirtual’ is True then it will search for all packages providing pkgname, even if pkgname is not itself a virtual pkg.

has_key(key)
install_archives(pm, install_progress)

The first parameter pm refers to an object returned by apt_pkg.PackageManager().

The second parameter install_progress refers to an InstallProgress() object of the module apt.progress.

install_count
Return the number of packages marked for installation.
is_virtual_package(pkgname)
Return whether the package is a virtual package.
keep_count
Return the number of packages marked as keep.
keys()
open(progress=None)
Open the package cache, after that it can be used like a dictionary
req_reinstall_pkgs
Return the packages not downloadable packages in reqreinst state.
required_download
Get the size of the packages that are required to download.
required_space
Get the size of the additional required space on the fs.
update(fetch_progress=None, pulse_interval=0, raise_on_error=True)

Run the equivalent of apt-get update.

The first parameter fetch_progress may be set to an instance of apt.progress.FetchProgress, the default is apt.progress.FetchProgress() .

upgrade(dist_upgrade=False)

Upgrade all packages.

If the parameter dist_upgrade is True, new dependencies will be installed as well (and conflicting packages may be removed). The default value is False.

Example

The following example shows how to load the cache, update it, and upgrade all the packages on the system:

import apt
import apt.progress

# First of all, open the cache
cache = apt.Cache()
# Now, lets update the package list
cache.update()
# We need to re-open the cache because it needs to read the package list
cache.open(None)
# Now we can do the same as 'apt-get upgrade' does
cache.upgrade()
# or we can play 'apt-get dist-upgrade'
cache.upgrade(True)
# Q: Why does nothing happen?
# A: You forgot to call commit()!
cache.commit(apt.progress.TextFetchProgress(),
             apt.progress.InstallProgress())

Working with Filters

class apt.cache.Filter

Filter base class

apply(pkg)
Filter function, return True if the package matchs a filter criteria and False otherwise
class apt.cache.MarkedChangesFilter

Filter that returns all marked changes

apply(pkg)
class apt.cache.FilteredCache(cache=None, progress=None)

A package cache that is filtered.

Can work on a existing cache or create a new one

filter_cache_post_change()
Called internally if the cache changes, emit a signal then.
has_key(key)
keys()
set_filter(filter)
Set the current active filter.

Example

This is an example for a filtered cache, which only allows access to the packages whose state has been changed, eg. packages marked for installation:

>>> from apt.cache import FilteredCache, Cache, MarkedChangesFilter
>>> cache = apt.Cache()
>>> changed = apt.FilteredCache(cache)
>>> changed.set_filter(MarkedChangesFilter())
>>> print len(changed) == len(cache.get_changes()) # Both need to have same length
True

The ProblemResolver class

class apt.cache.ProblemResolver(cache)

Resolve problems due to dependencies and conflicts.

The first argument ‘cache’ is an instance of apt.Cache.

clear(package)
Reset the package to the default state.
install_protect()
mark protected packages for install or removal.
protect(package)
Protect a package so it won’t be removed.
remove(package)
Mark a package for removal.
resolve()
Resolve dependencies, try to remove packages where needed.
resolve_by_keep()
Resolve dependencies, do not try to remove packages.

Exceptions

exception apt.cache.FetchCancelledException
Exception that is thrown when the user cancels a fetch operation.
exception apt.cache.FetchFailedException
Exception that is thrown when fetching fails.
exception apt.cache.LockFailedException
Exception that is thrown when locking fails.