Python-apt 0.8 is a new major release of the python bindings for the APT package management libraries. It provides support for Python 3, new language features and an API conforming to PEP 8.
Despite the many changes made in python-apt 0.8, the release still provides backwards compatibility to the 0.7 series. This makes it possible to run your old applications. Applications using the old API should be updated to the new API, because the old one will be removed after some time.
Python-apt is the first Debian package to support the third major release of Python. The port is straight forward and integrates as nicely in Python 3 as the Python 2 builds integrate in Python 2.
Please be aware that python-apt builds for Python 3 are built without the compatibility options enabled for Python 2 builds. They also do not provide methods like has_key() on mapping objects, because it has been removed in Python 3.
The 0.8 release introduces real classes in the apt_pkg extension. This is an important step forward and makes writing code much easier, because you can see the classes without having to create an object first. It also makes it easier to talk about those classes, because they have a real name now.
The 0.7 series shipped many functions for creating new objects, because the classes were not exported. In 0.8, the classes themselves replace those functions, as you can see in the following table.
| Function | Replacing class |
|---|---|
| apt_pkg.GetAcquire() | apt_pkg.Acquire |
| apt_pkg.GetCache() | apt_pkg.Cache |
| apt_pkg.GetCdrom() | apt_pkg.Cdrom |
| apt_pkg.GetDepCache() | apt_pkg.DepCache |
| apt_pkg.GetPackageManager() | apt_pkg.PackageManager |
| apt_pkg.GetPkgAcqFile() | apt_pkg.AcquireFile |
| apt_pkg.GetPkgActionGroup() | apt_pkg.ActionGroup |
| apt_pkg.GetPkgProblemResolver() | apt_pkg.ProblemResolver |
| apt_pkg.GetPkgRecords() | apt_pkg.PackageRecords |
| apt_pkg.GetPkgSourceList() | apt_pkg.SourceList |
| apt_pkg.GetPkgSrcRecords() | apt_pkg.SourceRecords |
| apt_pkg.ParseSection() | apt_pkg.TagSection |
| apt_pkg.ParseTagFile() | apt_pkg.TagFile |
In May 2008, Ben Finney reported bug 481061 against the python-apt package, asking for PEP8 conformant names. With the release of python-apt 0.8, this is finally happening.
This is not a real big change, but it’s good to have it: apt_pkg.ActionGroup can now be used as a context manager for the with statement. This makes it more obvious that you are using an action group, and is just cooler:
with apt_pkg.ActionGroup(depcache):
for package in my_selected_packages:
depcache.mark_install(package)
This also works for apt.Cache:
with cache.actiongroup(): # cache is an Instance of apt.Cache
for package in my_selected_packages:
package.mark_install() # Instance of apt.Package
This release of python-apt also features several other, smaller changes:
- Reduced memory usage by making apt.Cache create apt.Package() object dynamically, instead of creating all of them during the cache initialization.
- Support to set the candidate version in apt.package.Package
Porting your application to python-apt 0.8 may be trivial. You should download the source tarball of python-apt and run the tool utils/migrate-0.8 using Python 2.6 over your code:
python2.6 utils/migrate-0.8.py -c myapp.py mypackage/
This will search your code for places where possibly deprecated names are used. Using the argument -c, you can turn colorized output on.
Now that you know which parts of your code have to be changed, you have to know how to do this. For classes, please look at the table. For all attributes, methods, functions, and their parameters the following rules apply:
- Replace leading [A-Z] with [a-z] (e.g DescURI => descURI)
- Replace multiple [A-Z] with [A-Z][a-z] (e.g. descURI => descUri)
- Replace every [A-Z] with the corresponding [a-z] (descUri => desc_uri)
As an exception, refixes such as ‘de’ (e.g. ‘dequote’) or ‘un’ (e.g. ‘unlock’) are normally not seperated by underscores from the next word. There are also some other exceptions which are listed here, and apply to any name containing this word: filename, filesize, destdir, destfile, dequote, unlock, reinstall, pinfile, REINSTREQ, UNPACKED, parse_commandline.