apt.cache — The Cache class

The Cache class

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

Dictionary-like package cache.

The APT cache file contains a hash table mapping names of binary packages to their metadata. A Cache object is the in-core representation of the same. It provides access to APTs idea of the list of available packages.

The cache can be used like a mapping from package names to Package objects (although only getting items is supported).

Keyword arguments: progress – a OpProgress object, rootdir – an alternative root directory. if that is given the system sources.list and system lists/files are not read, only file relative to the given rootdir, memonly – build the cache in memory only.

Changed in version 1.0: The cache now supports package names with special architecture qualifiers such as :all and :native. It does not export them in keys(), though, to keep keys() a unique set.

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 action group 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

close()

Close the package cache

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.

Deprecated since version 1.0: Please use connect2() instead, as this function is very likely to cause a memory leak.

connect2(name, callback, *args, **kwds)

Connect to a signal.

The callback will be passed the cache as an argument, and any arguments passed to this function. Make sure that, if you pass a method of a class as your callback, your class does not contain a reference to the cache.

Cyclic references to the cache can cause issues if the Cache object is replaced by a new one, because the cache keeps a lot of objects and tens of open file descriptors.

currently only used for cache_{post,pre}_{changed,open}.

New in version 1.0.

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.

fetch_archives(progress=None, fetcher=None)

Fetch the archives for all packages marked for install/upgrade.

You can specify either an apt.progress.base.AcquireProgress() object for the parameter progress, or specify an already existing apt_pkg.Acquire object for the parameter fetcher.

The return value of the function is undefined. If an error occured, an exception of type FetchFailedException or FetchCancelledException is raised.

New in version 0.8.0.

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, sources_list=None)

Run the equivalent of apt-get update.

You probably want to call open() afterwards, in order to utilise the new cache. Otherwise, the old cache will be used which can lead to strange bugs.

The first parameter fetch_progress may be set to an instance of apt.progress.FetchProgress, the default is apt.progress.FetchProgress() . sources_list – Update a alternative sources.list than the default. Note that the sources.list.d directory is ignored in this case

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.