changeset 538:6fb5e8b74414

contrib: Do a more conservative approach (full sorting) to figure out the youngest debs. This _much_ slower but it works correctly. TODO: make the heapq.nsmallest() work correct.
author Sascha Teichmann <teichmann@intevation.de>
date Mon, 10 Jan 2011 14:58:51 +0000
parents aeccb5774939
children 94ff51f691a2
files contrib/bin/delete-old-debs.py
diffstat 1 files changed, 23 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/bin/delete-old-debs.py	Mon Jan 10 11:36:55 2011 +0000
+++ b/contrib/bin/delete-old-debs.py	Mon Jan 10 14:58:51 2011 +0000
@@ -15,7 +15,7 @@
 import subprocess
 import logging
 
-from heapq import nsmallest
+#from heapq import nsmallest
 
 from optparse import OptionParser
 
@@ -27,18 +27,6 @@
 
 FIELD = re.compile("([a-zA-Z]+):\s*(.+)")
 
-# map rich comparison to 'dpkg --compare-versions'
-# map == to !=, < to >= and so on to reverse order in heap. 
-RICH_CMP = dict([
-    ("__%s__" % a, lambda se, ot:
-        subprocess.call([
-            "dpkg", "--compare-versions", 
-            se.version, b, ot.version]) == 0)
-    for a, b in (("eq", "ne"), ("ne", "eq"),
-                 ("lt", "ge"), ("gt", "le"),
-                 ("le", "gt"), ("ge", "lt"))])
-
-
 class DebCmp(object):
     """Helper class to make deb files comparable
        by there versions.
@@ -48,7 +36,19 @@
         self.version = version
         self.path    = path
 
-        self.__dict__.update(RICH_CMP)
+    def __cmp__(self, other):
+        if self.version == other.version:
+            return 0
+        # switch lt and gt to reverse order in heap
+        if (subprocess.call([
+            "dpkg", "--compare-versions", 
+            self.version, "gt", other.version]) == 0):
+            return -1
+        if (subprocess.call([
+            "dpkg", "--compare-versions", 
+            self.version, "lt", other.version]) == 0):
+            return +1
+        return 0
 
 
 def deb_info(deb, fields=["Package", "Version"]):
@@ -89,12 +89,16 @@
 
     for package, debs in packages.iteritems():
         if len(debs) > keep:
-            # full sorting is not required
-            stay = frozenset([d.path for d in nsmallest(keep, debs)])
+            debs.sort()
+            for deb in debs[keep:]:
+                yield deb.path
 
-            for deb in debs:
-                if deb.path not in stay:
-                    yield deb.path
+            ## full sorting is not required
+            #stay = frozenset([d.path for d in nsmallest(keep, debs)])
+
+            #for deb in debs:
+            #    if deb.path not in stay:
+            #        yield deb.path
 
 
 def main():
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)