From d475569265574be6e49ebf9a363c7e0325542be2 Mon Sep 17 00:00:00 2001
From: Julian Andres Klode <jak@debian.org>
Date: Sat, 27 Feb 2010 22:19:33 +0100
Subject: [PATCH] Update to new python-apt API.

---
 debian/control  |    2 +-
 src/changes.py  |   13 +++----
 src/commands.py |   92 +++++++++++++++++++++++++++---------------------------
 3 files changed, 53 insertions(+), 54 deletions(-)

diff --git a/debian/control b/debian/control
index 050e85c..1afc24a 100644
--- a/debian/control
+++ b/debian/control
@@ -11,7 +11,7 @@ Build-Depends: debhelper (>= 5.0.38)
 Package: wajig
 Architecture: all
 XB-Python-Version: all
-Depends: ${python:Depends}, apt, python-apt, dselect
+Depends: ${python:Depends}, apt, python-apt (>= 0.7.93.2~), dselect
 Suggests: wget, fping, debconf, reportbug, apt-move, dpkg-repack, alien, fakeroot, gkdebconf, lynx, python-gtk2, python-glade2, python-gnome2, gnome-terminal, locales, gnome-tasksel, deborphan, vrms, sudo, apt-show-versions, apt-listbugs
 Description: simplified Debian package management front end
  Wajig is a single commandline wrapper around apt, apt-cache, dpkg,
diff --git a/src/changes.py b/src/changes.py
index faf272e..1965c8d 100644
--- a/src/changes.py
+++ b/src/changes.py
@@ -382,11 +382,11 @@ def get_new_upgrades():
     list        Newly upgraded packages"""
     load_dictionaries()
     upgraded_list = []
-    apt_pkg.InitSystem(); # Not sure why!
+    apt_pkg.init_system(); # Not sure why!
     for pkg in installed_list.keys():
         if available_list.has_key(pkg) \
            and previous_list.has_key(pkg) \
-           and apt_pkg.VersionCompare(available_list[pkg],
+           and apt_pkg.version_compare(available_list[pkg],
                                       previous_list[pkg]) > 0:
             upgraded_list.append(pkg)
     return upgraded_list
@@ -401,19 +401,18 @@ def get_to_upgrade():
 
     load_dictionaries()
     upgraded_list = []
-    apt_pkg.InitSystem(); # Not sure why!
+    apt_pkg.init_system(); # Not sure why!
     for pkg in installed_list.keys():
         if available_list.has_key(pkg) \
-           and apt_pkg.VersionCompare(available_list[pkg],
+           and apt_pkg.version_compare(available_list[pkg],
                                       installed_list[pkg]) > 0:
             upgraded_list.append(pkg)
     return upgraded_list
 
 def get_status(pkg):
     p = perform.execute("dpkg --status " + pkg, pipe=True)
-    pkginfo = apt_pkg.ParseTagFile(p)
-    pkginfo.Step()
-    return pkginfo.Section.get("Status")
+    pkginfo = apt_pkg.TagFile(p)
+    return pkginfo.next().get("Status")
 
 def get_dependees(pkg):
     """Return a list of other installed pkgs that depend on PKG."""
diff --git a/src/commands.py b/src/commands.py
index de8c62f..06a2fe7 100644
--- a/src/commands.py
+++ b/src/commands.py
@@ -94,12 +94,12 @@ def get_available(command="dumpavail"):
     "Return an apt_pkg object that represents the parsed list of packages."
     
     # This originally ran the apt-cache command as a pipe to
-    # ParseTagFile as in:
+    # TagFile as in:
     #  command = "apt-cache dumpavail"
     #  packages_pipe = perform.execute(command, noquiet=True, pipe=True)
-    #  avail = apt_pkg.ParseTagFile(packages_pipe)
+    #  avail = apt_pkg.TagFile(packages_pipe)
     #    
-    # But as in Bug#366678 ParseTagFile no longer works in a pipe
+    # But as in Bug#366678 TagFile no longer works in a pipe
     # because of changes to apt_pkg by Michael Vogt. He supplied
     # suggested fixes, but they were not tested and I've not had
     # a chance to understand the new APT package for Python. So put
@@ -117,7 +117,7 @@ def get_available(command="dumpavail"):
     command = "cat /var/lib/dpkg/status >> " + tmpcache
     perform.execute(command)
     # 090501 End
-    avail = apt_pkg.ParseTagFile(file(tmpcache))
+    avail = apt_pkg.TagFile(file(tmpcache))
     if os.path.exists(tmpcache): os.remove(tmpcache)
     return(avail)
 
@@ -134,14 +134,14 @@ def do_dependents(package):
     #
     # Check for information in the Available list
     #
-    while avail.Step():
-        nam = avail.Section.get("Package")
+    for section in avail:
+        nam = section.get("Package")
         if (nam in pkgs):
-            dep = avail.Section.get("Depends")
-            sug = avail.Section.get("Suggests")
-            rec = avail.Section.get("Recommends")
-            con = avail.Section.get("Conflicts")
-            rep = avail.Section.get("Replaces")
+            dep = section.get("Depends")
+            sug = section.get("Suggests")
+            rec = section.get("Recommends")
+            con = section.get("Conflicts")
+            rep = section.get("Replaces")
             if not dep: dep = ""
             if not sug: sug = ""
             if not rec: rec = ""
@@ -209,9 +209,9 @@ def do_describe(packages):
     #  apt-cache dumpavail > /tmp/avail
     #  dpkg --update-avail /tmp/avail
     #
-    # avail  = apt_pkg.ParseTagFile(open("/var/cache/apt/available","r"));
-    # avail  = apt_pkg.ParseTagFile(open("/var/cache/apt/pkgcache.bin","r"));
-    # avail  = apt_pkg.ParseTagFile(open("/var/lib/dpkg/available","r"))
+    # avail  = apt_pkg.TagFile(open("/var/cache/apt/available","r"));
+    # avail  = apt_pkg.TagFile(open("/var/cache/apt/pkgcache.bin","r"));
+    # avail  = apt_pkg.TagFile(open("/var/lib/dpkg/available","r"))
     #
     # Simplest solution seems to be to pipe details of all packages
     # from apt-cache (which needs sudo). 23 Aug 2003.
@@ -260,10 +260,10 @@ def do_describe(packages):
     #
     # Check for information in the Available list
     #
-    while avail.Step():
-        if (avail.Section.get("Package") in packages):
-            package_name = avail.Section.get("Package")
-            package_description = avail.Section.get("Description")
+    for section in avail:
+        if (section.get("Package") in packages):
+            package_name = section.get("Package")
+            package_description = section.get("Description")
             if not describe_list.has_key(package_name):
                 describe_list[package_name] = package_description
     # Bug fix for Bug#366678 Part 2 - not working yet???
@@ -600,10 +600,10 @@ def do_install_suggest(packages, type, noauth=""):
     #
     # Check for information in the Available list
     #
-    while avail.Step():
-        if (avail.Section.get("Package") in packages):
-            sug = avail.Section.get("Suggests")
-            rec = avail.Section.get("Recommends")
+    for section in avail:
+        if (section.get("Package") in packages):
+            sug = section.get("Suggests")
+            rec = section.get("Recommends")
             if type == "Suggests" or type == "Both":
                 if sug: suggest_list = suggest_list+" "+sug
             if type == "Recommends" or type == "Both":
@@ -652,17 +652,17 @@ def do_install_suggest(packages, type, noauth=""):
 def do_listsections():
     avail = get_available()
     sections = []
-    while avail.Step():
-        s = avail.Section.get("Section")
+    for section in avail:
+        s = section.get("Section")
         if s not in sections:
             sections.append(s)
     print "\n".join(sections)
 
 def do_listsection(pattern):
     avail = get_available()
-    while avail.Step():
-        if (pattern == avail.Section.get("Section")):
-            print avail.Section.get("Package")
+    for section in avail:
+        if (pattern == section.get("Section")):
+            print section.get("Package")
 
 #------------------------------------------------------------------------
 #
@@ -757,18 +757,18 @@ def map_sources(packages):
     Note that the source could include a version number.
     Only replace with source if package exists (Bug#295455).
 """
-    Parse = apt_pkg.ParseTagFile(open("/var/lib/dpkg/available","r"))
+    tagfile = apt_pkg.TagFile(open("/var/lib/dpkg/available","r"))
     sources = []
-    while Parse.Step():
+    for section in tagfile:
         # May want to look to terminate as soon as all packages
         # have been found - to be more efficient
-        if Parse.Section.get("Package") in packages:
-            if Parse.Section.get("Source"):
-                #pkgname = Parse.Section.get("Source").split()[0]
-                pkgname = Parse.Section.get("Source")
-                sources += [[pkgname, Parse.Section.get("Package")]]
+        if section.get("Package") in packages:
+            if section.get("Source"):
+                #pkgname = section.get("Source").split()[0]
+                pkgname = section.get("Source")
+                sources += [[pkgname, section.get("Package")]]
             else:
-                sources += [[Parse.Section.get("Package"), Parse.Section.get("Package")]]
+                sources += [[section.get("Package"), section.get("Package")]]
     return sources
 
 def do_news(packages):
@@ -835,7 +835,7 @@ def do_news(packages):
 #     #
 #     command = "apt-cache show " + package
 #     packages_pipe = perform.execute(command, noquiet=True, pipe=True)
-#     Parse = apt_pkg.ParseTagFile(packages_pipe)
+#     Parse = apt_pkg.TagFile(packages_pipe)
 #     version = '0'
 #     filename = ''
 #     while Parse.Step():
@@ -1002,7 +1002,7 @@ def do_size(packages, size):
     # Work with the list of installed packages
     # (I think status has more than installed?)
     #
-    status = apt_pkg.ParseTagFile(open("/var/lib/dpkg/status","r"));
+    status = apt_pkg.TagFile(open("/var/lib/dpkg/status","r"));
     #
     # Initialise the sizes dictionary.
     #
@@ -1011,11 +1011,11 @@ def do_size(packages, size):
     #
     # Check for information in the Status list
     #
-    while status.Step():
-        if not packages or status.Section.get("Package") in packages:
-            package_name   = status.Section.get("Package")
-            package_size   = status.Section.get("Installed-Size")
-            package_status = re.split(" ", status.Section.get("Status"))[2]
+    for section in status:
+        if not packages or section.get("Package") in packages:
+            package_name   = section.get("Package")
+            package_size   = section.get("Installed-Size")
+            package_status = re.split(" ", section.get("Status"))[2]
             if package_size and int(package_size) > size:
                 if not size_list.has_key(package_name):
                     size_list[package_name] = package_size
@@ -1489,11 +1489,11 @@ def do_recdownload(packages):
         Author: Juanjo Alvarez <juanjux@yahoo.es>"""
 
     def get_deps(package):
-        Parse = apt_pkg.ParseTagFile(open("/var/lib/dpkg/available","r"))
+        tagfile = apt_pkg.TagFile(open("/var/lib/dpkg/available","r"))
         deplist = []
-        while Parse.Step():
-            if Parse.Section.get("Package")==package:
-                deplist = apt_pkg.ParseDepends(Parse.Section.get("Depends",""))
+        for section in tagfile:
+            if section.get("Package")==package:
+                deplist = apt_pkg.parse_depends(section.get("Depends",""))
                 break
         realdeplist = []
         if deplist != []:
-- 
1.7.0

