2016-12-13 19 views
0

私はLinux mintでこれを試していました。私はpython-apt APIを使ってパッケージを削除する方法を研究してきました。以下のコードは私が思いつくことができたものでしたが、実行すると何も起こりません。私は今、単一のパッケージを削除しようとしていますが、後で、テキストファイルからパッケージのリストを削除したいと思います。 this postにある答えを使用しようとしましたが、削除のために再設計しましたが、ロジックが機能しません。入力をお願いします。python apt APIを使ってdebianパッケージを削除するには

#!/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() 

答えて

0

ドキュメントを読んでさまざまなことを試した後、以下のコードを使って問題を多かれ少なかれ解決しました。誰かがより良い方法を持っている場合は、投稿してください。私はまだたくさんのことを学びたい

#!/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() 
関連する問題