I want to remove a package using python-apt and also remove all dependendencies. Basically the equivalent to
apt purge PACKAGE
apt autoremove
However I can’t figure out the dependency part.
I guess this has to be done using the ProblemResolver class?
Here is my code, that removes a package but does not uninstall dependencies:
import apt
cache = apt.Cache()
resolver = apt.cache.ProblemResolver(cache)
pkg = cache['PACKAGE_NAME']
if pkg.is_installed:
pkg.mark_delete(True, purge=True)
# does nothing
resolver.remove(pkg)
resolver.resolve()
else:
raise InvalidUsage(package + " is not installed", status_code=400)
try:
result = cache.commit(allow_unauthenticated=True)
cache.update()
cache.open()
except Exception as arg:
raise Exception(
"Sorry, purging package failed [{err}]".format(err=str(arg)))
I have implemented a workaround and will share it here.
If anybody could provide a proper solution, I will be very interested :-)
I tried iterating over the dependencies and purging those manually. But this will only remove the direct dependencies. Doing so recursively will remove more packages but far less then what
apt autoremovewould.Workaround: ask
apt-getshell command to provide a listSo what I did is start a shell command that will provide a list of orphaned packages and iterate over this: