如何使用 python apt API 删除 debian 软件包
How do I remove debian packages using python apt API
我正在 Linux mint 上尝试这个。我一直在研究如何使用 python-apt API 删除软件包。下面的代码是我所能想到的,但是当我 运行 它时什么也没有发生。我现在正在尝试删除一个包,但稍后我想从文本文件中删除一个包列表。我尝试使用 this post 中找到的答案并重新设计它以进行删除,但我的逻辑不起作用。请给我一些意见。
#!/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()
阅读文档并尝试不同的方法后,我或多或少通过编写以下代码解决了我的问题。如果有人有更好的方法,请post。我还想学很多东西
#!/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()
为了从文件中获取包列表,我暂时采用了这种方法。
#!/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()
我正在 Linux mint 上尝试这个。我一直在研究如何使用 python-apt API 删除软件包。下面的代码是我所能想到的,但是当我 运行 它时什么也没有发生。我现在正在尝试删除一个包,但稍后我想从文本文件中删除一个包列表。我尝试使用 this post 中找到的答案并重新设计它以进行删除,但我的逻辑不起作用。请给我一些意见。
#!/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()
阅读文档并尝试不同的方法后,我或多或少通过编写以下代码解决了我的问题。如果有人有更好的方法,请post。我还想学很多东西
#!/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()
为了从文件中获取包列表,我暂时采用了这种方法。
#!/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()