如何使用python apt API删除debian软件包 [英] How do I remove debian packages using python apt API
问题描述
#!/ usr / bin / env python
pre>
#aptuninnstall.py
import apt
import sys
def remove():
pkg_name =chrome-browser
cache = apt.cache .Cache()
cache.update()
pkg = cache [pkg_name]
pkg.marked_delete
resolver = apt.cache.ProblemResolver(cache)
for pkg在cache.get_changes()中:
如果pkg.is_installed:
resolver.remove(pkg)
其他:
打印(pkg_name +未安装,因此未删除)
try:
cache.commit()
除了异常,arg:
print>> sys.stderr对不起,程序包删除失败[{err}]格式(err = str(arg))
remove()
解决方案阅读文档并尝试不同的内容后,我或多或少通过下面的代码来解决我的问题。如果有更好的方法,请发表。我还想学习很多
#!/ usr / bin / env python
pre>
#aptremove.py
import apt
import apt_pkg
import sys
def remove():
pkg_name =chrome-browser
cache = apt.cache.Cache()
cache.open(无)
pkg = cache [pkg_name]
cache.update()
pkg.mark_delete(True,
resolver = apt.cache.ProblemResolver(cache)
如果pkg.is_installed为False:
print(pkg_name +未安装,因此未删除)
else:
for pkg in cache.get_changes():
如果pkg.mark_delete:
打印pkg_name +已安装并将被删除
print%d软件包将被删除%cache.delete_count
resolver.remove(pkg)
try:
cache.commit()
cache.close()
除了异常,arg:
print>> sys.stderr对不起,程序包删除失败[{err}]格式(err = str(arg))
remove()
为了从文件中获取包列表,我现在采取了这种方法。
#!/ usr / bin / env python
pre>
#aptremove.py
import apt
import apt_pkg
import sys
def remove():
cache = apt.cache.Cache()
cache.open (无)
resolver = apt.cache.ProblemResolver(cache)
with open(apps-to-remove)作为输入:
for pkg_name in input:
pkg = cache [pkg_name.strip()]
pkg.mark_delete(true,purge = True)
input.close()
cache.update()
如果pkg.is_installed为False:
print(pkg_name +未安装,因此未删除)
else:
在缓存中的pkg .get_changes():
如果pkg
打印pkg_name +已安装并将被删除
打印%d包将被删除%cache.delete_count
resolver.remove(pkg)
try:
cache.commit()
cache.close()
打印开始
除了异常,arg:
print>> sys.stderr对不起,程序包删除失败[{err}]格式(err = str(arg))
remove()
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 happens when I run it. I am trying to remove a single package right now but later I would like to remove a list of packages from a text file. I tried to use the answer found in this post and re-engineered it for removing but my logic does not work. Please give me some input.
#!/usr/bin/env python # aptuninnstall.py import apt import sys def remove(): pkg_name = "chromium-browser" cache = apt.cache.Cache() cache.update() pkg = cache[pkg_name] pkg.marked_delete resolver = apt.cache.ProblemResolver(cache) for pkg in cache.get_changes(): if pkg.is_installed: resolver.remove(pkg) else: print (pkg_name + " not installed so not removed") try: cache.commit() except Exception, arg: print >> sys.stderr, "Sorry, package removal failed [{err}]".format(err=str(arg)) remove()
解决方案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.py import apt import apt_pkg import sys def remove(): 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 is False: 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.py import apt import apt_pkg import sys def remove(): cache = apt.cache.Cache() cache.open(None) resolver = apt.cache.ProblemResolver(cache) with open("apps-to-remove") as input: for pkg_name in input: pkg = cache[pkg_name.strip()] pkg.mark_delete(True, purge=True) input.close() cache.update() if pkg.is_installed is False: 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()
这篇关于如何使用python apt API删除debian软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!