How Do I Remove Debian Packages Using Python Apt Api
I'm was trying this on Linux mint. I have been researching on how to remove packages using the python-apt API. The piece of code below was all I could come up with but nothing happ
Solution 1:
After reading the docs and trying different things, I more or less fixed my problem by coming up with the code below. If someone has a better way, please post. I still want to learn a lot
#!/usr/bin/env python# aptremove.pyimport apt
import apt_pkg
import sys
defremove():
pkg_name = "chromium-browser"
cache = apt.cache.Cache()
cache.open(None)
pkg = cache[pkg_name]
cache.update()
pkg.mark_delete(True, purge=True)
resolver = apt.cache.ProblemResolver(cache)
if pkg.is_installed isFalse:
print (pkg_name + " not installed so not removed")
else:
for pkg in cache.get_changes():
if pkg.mark_delete:
print pkg_name + " is installed and will be removed"print" %d package(s) will be removed" % cache.delete_count
resolver.remove(pkg)
try:
cache.commit()
cache.close()
except Exception, arg:
print >> sys.stderr, "Sorry, package removal failed [{err}]".format(err=str(arg))
remove()
In order to get the package list from a file, I took this approach for now.
#!/usr/bin/env python# aptremove.pyimport apt
import apt_pkg
import sys
defremove():
cache = apt.cache.Cache()
cache.open(None)
resolver = apt.cache.ProblemResolver(cache)
withopen("apps-to-remove") asinput:
for pkg_name ininput:
pkg = cache[pkg_name.strip()]
pkg.mark_delete(True, purge=True)
input.close()
cache.update()
if pkg.is_installed isFalse:
print (pkg_name + " not installed so not removed")
else:
for pkg in cache.get_changes():
if pkg.mark_delete:
print pkg_name + " is installed and will be removed"print" %d package(s) will be removed" % cache.delete_count
resolver.remove(pkg)
try:
cache.commit()
cache.close()
print"starting"except Exception, arg:
print >> sys.stderr, "Sorry, package removal failed [{err}]".format(err=str(arg))
remove()
Post a Comment for "How Do I Remove Debian Packages Using Python Apt Api"