apt_pkg — The low-level bindings for apt-pkg

The apt_pkg extensions provides a more low-level way to work with apt. It can do everything apt can, and is written in C++. It has been in python-apt since the beginning.

Module Initialization

apt_pkg.initConfig()
Initialize the configuration of apt. This is needed for most operations.
apt_pkg.initSystem()
Initialize the system.
apt_pkg.init()
Deprecated function. Use initConfig() and initSystem() instead.

Object initialization

apt_pkg.GetCache([progress])
Return a pkgCache object. The optional parameter progress specifies an instance of apt.progress.OpProgress() which will display the open progress.
apt_pkg.GetCdrom()

Return a Cdrom object with the following methods:

Cdrom.Ident(progress)
Identify the cdrom. The parameter progress refers to an apt.progress.CdromProgress() object.
Cdrom.Add(progress)
Add the cdrom to the sources.list file. The parameter progress refers to an apt.progress.CdromProgress() object.
apt_pkg.GetDepCache(cache)
Return a pkgDepCache object. The parameter cache specifies an instance of pkgCache (see GetCache()).
apt_pkg.GetPkgSourceList()
Return a pkgSourceList object.

The Acquire interface

apt_pkg.GetAcquire([progress])

Return an Acquire object. This is a class which allows you to fetch files, or archive contents. The parameter progress refers to an apt.progress.FetchProgress() object.

Acquire items have multiple methods:

Acquire.Run()
Fetch all the items which have been added by GetPkgAcqFile().
Acquire.Shutdown()
Shut the fetcher down.
Acquire.TotalNeeded
The total amount of bytes needed (including those of files which are already present)
Acquire.FetchNeeded
The total amount of bytes which need to be fetched.
Acquire.PartialPresent
Whether some files have been acquired already. (???)
apt_pkg.GetPkgAcqFile(aquire, uri[, md5, size, descr, shortDescr, destDir, destFile])

The parameter acquire refers to an Acquire() object as returned by GetAcquire(). The file will be added to the Acquire queue automatically.

The parameter uri refers to the location of the file, any protocol of apt is supported.

The parameter md5 refers to the md5sum of the file. This can be used for checking the file.

The parameter size can be used to specify the size of the package, which can then be used to calculate the progress and validate the download.

The parameter descr is a descripition of the download. It may be used to describe the item in the progress class. shortDescr is the short form of it.

You can use destDir to manipulate the directory where the file will be saved in. Together with destFile you can specify the complete target path.

Hash functions

The apt_pkg module also provides several hash functions. If you develop applications with python-apt it is often easier to use these functions instead of the ones provides in Python’s hashlib module.

apt_pkg.md5sum(object)
Return the md5sum of the object. object may either be a string, in which case the md5sum of the string is returned, or a file() object, in which case the md5sum of its contents is returned.
apt_pkg.sha1sum(object)
Return the sha1sum of the object. object may either be a string, in which case the sha1sum of the string is returned, or a file() object, in which case the sha1sum of its contents is returned.
apt_pkg.sha256sum(object)
Return the sha256sum of the object. object may either be a string, in which case the sha256sum of the string is returned, or a file() object, in which case the sha256sum of its contents is returned.

Other functions

Note

This documentation is created automatically and should be rewritten.

apt_pkg.Base64Encode()
Base64Encode(String) -> String
apt_pkg.CheckDep()
CheckDep(PkgVer,DepOp,DepVer) -> int
apt_pkg.CheckDomainList()
CheckDomainList(String,String) -> Bool
apt_pkg.DeQuoteString()
DeQuoteString(String) -> String
apt_pkg.GetLock()
GetLock(string) -> int This will create an empty file of the given name and lock it. Once this is done all other calls to GetLock in any other process will fail with -1. The return result is the fd of the file, the call should call close at some time
apt_pkg.GetPackageManager()
GetPackageManager(DepCache) -> PackageManager
apt_pkg.GetPkgActionGroup()
GetPkgActionGroup(DepCache) -> PkgActionGroup
apt_pkg.GetPkgProblemResolver()
GetDepProblemResolver(DepCache) -> PkgProblemResolver
apt_pkg.GetPkgRecords()
GetPkgRecords(Cache) -> PkgRecords
apt_pkg.GetPkgSrcRecords()
GetPkgSrcRecords() -> PkgSrcRecords
apt_pkg.newConfiguration()
Construct a configuration instance
apt_pkg.ParseCommandLine()

ParseCommandLine(Configuration,ListOfOptions,List-argv) -> List

This function is like getopt except it manipulates a configuration space. output is a list of non-option arguments (filenames, etc). ListOfOptions is a list of tuples of the form:

(‘c’,”long-opt or None”,”Configuration::Variable”,”optional type”)

Where type may be one of HasArg, IntLevel, Boolean, InvBoolean, ConfigFile, or ArbItem. The default is Boolean.

apt_pkg.ParseDepends()

ParseDepends(s) -> list of tuples

The resulting tuples are (Pkg,Ver,Operation). Each anded dependency is a list of or’d dependencies Source depends are evaluated against the curernt arch and only those that Match are returned.

apt_pkg.ParseSection()
ParseSection(Text) -> SectionObject
apt_pkg.ParseSrcDepends()

ParseDepends(s) -> list of tuples

The resulting tuples are (Pkg,Ver,Operation). Each anded dependency is a list of or’d dependencies Source depends are evaluated against the curernt arch and only those that Match are returned.

apt_pkg.ParseTagFile()
ParseTagFile(File) -> TagFile
apt_pkg.PkgSystemLock()
PkgSystemLock() -> boolean Get the global pkgsystem lock
apt_pkg.PkgSystemUnLock()
PkgSystemUnLock() -> boolean Unset the global pkgsystem lock
apt_pkg.QuoteString()
QuoteString(String,String) -> String
apt_pkg.ReadConfigFile()
LoadConfig(Configuration,FileName) -> None
apt_pkg.ReadConfigFileISC()
LoadConfig(Configuration,FileName) -> None
apt_pkg.RewriteSection()

RewriteSection(Section,Order,RewriteList) -> String

The section rewriter allows a section to be taken in, have fields added, removed or changed and then put back out. During this process the fields within the section are sorted to corrispond to a proper order. Order is a list of field names with their proper capitialization. apt_pkg.RewritePackageOrder and apt_pkg.RewriteSourceOrder are two predefined orders. RewriteList is a list of tuples. Each tuple is of the form:

(Tag,NewValue[,RenamedTo])

Tag specifies the tag in the source section. NewValue is the new value of that tag and the optional RenamedTo field can cause the tag to be changed. If NewValue is None then the tag is removed Ex. (‘Source’,’apt’,’Package’) is used for .dsc files.

apt_pkg.SizeToStr()
SizeToStr(int) -> String
apt_pkg.StringToBool()
StringToBool(String) -> int
apt_pkg.StrToTime()
StrToTime(String) -> Int
apt_pkg.TimeRFC1123()
TimeRFC1123(int) -> String
apt_pkg.TimeToStr()
TimeToStr(int) -> String
apt_pkg.UpstreamVersion()
UpstreamVersion(a) -> string
apt_pkg.URItoFileName()
URItoFileName(String) -> String
apt_pkg.VersionCompare()
VersionCompare(a,b) -> int

Data

apt_pkg.Config
An Configuration() object with the default configuration. Actually, this is a bit different object, but it is compatible.
apt_pkg.RewritePackageOrder
apt_pkg.RewriteSourceOrder

Package States

apt_pkg.CurStateConfigFiles
apt_pkg.CurStateHalfConfigured
apt_pkg.CurStateHalfInstalled
apt_pkg.CurStateInstalled
apt_pkg.CurStateNotInstalled
apt_pkg.CurStateUnPacked

Dependency types

apt_pkg.DepConflicts
apt_pkg.DepDepends
apt_pkg.DepObsoletes
apt_pkg.DepPreDepends
apt_pkg.DepRecommends
apt_pkg.DepReplaces
apt_pkg.DepSuggests

Installed states

apt_pkg.InstStateHold
apt_pkg.InstStateHoldReInstReq
apt_pkg.InstStateOk
apt_pkg.InstStateReInstReq

Priorities

apt_pkg.PriExtra
apt_pkg.PriImportant
apt_pkg.PriOptional
apt_pkg.PriRequired
apt_pkg.PriStandard

Select states

apt_pkg.SelStateDeInstall
apt_pkg.SelStateHold
apt_pkg.SelStateInstall
apt_pkg.SelStatePurge
apt_pkg.SelStateUnknown

Build information

apt_pkg.Date
apt_pkg.LibVersion
apt_pkg.Time
apt_pkg.Version