python-apt and downgrades
I'm fairly new to using the apt python modules and I'm really hoping
someone can help me with a script I need for downgrading packages.
I have development machines where I regularly manually install new
packages (using dpkg) for testing. It's easy to lose track of what has
been manually changed so once I'm done with the testing I'd like to
run a script which reverts all those packages to the best version
available in the repository.
I'm finding that my script works fine if the local version is less
than the repository version (i.e. it's like a normal 'upgrade') but
whenever I attempt a downgrade the package goes into the 'keep' state
and nothing changes. I have enabled the APT::Get::Allow-Downgrades
option but I wonder if there is something else I need to do? I've
tried to reduce it down to the smallest possible working example that
demonstrates this (see below).
#!/usr/bin/python3
import apt_pkg
import apt.progress
progress_acquire = apt.progress.text.AcquireProgress()
progress_install = apt.progress.base.InstallProgress()
progress_op = apt.progress.text.OpProgress()
apt_pkg.init()
cache = apt_pkg.Cache(progress_op)
cache.update(progress_acquire,sources)
depcache = apt_pkg.DepCache(cache)
for pkg in cache.packages:
if pkg.selected_state != apt_pkg.SELSTATE_INSTALL:
continue
best_match = None
for v in pkg.version_list:
if v.downloadable and ( best_match is None or v > best_match ):
best_match = v
if best_match is not None and best_match != pkg.current_ver:
depcache.mark_install(pkg)
depcache.set_candidate_ver(pkg,best_match)
depcache.commit(progress_acquire, progress_install)
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
Reply to: