Mercurial > treepkg > treepkg
comparison contrib/bin/delete-old-debs.py @ 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 |
comparison
equal
deleted
inserted
replaced
537:aeccb5774939 | 538:6fb5e8b74414 |
---|---|
13 import re | 13 import re |
14 | 14 |
15 import subprocess | 15 import subprocess |
16 import logging | 16 import logging |
17 | 17 |
18 from heapq import nsmallest | 18 #from heapq import nsmallest |
19 | 19 |
20 from optparse import OptionParser | 20 from optparse import OptionParser |
21 | 21 |
22 log = logging.getLogger(__name__) | 22 log = logging.getLogger(__name__) |
23 log.setLevel(logging.WARNING) | 23 log.setLevel(logging.WARNING) |
25 | 25 |
26 DEFAULT_KEEP = 3 | 26 DEFAULT_KEEP = 3 |
27 | 27 |
28 FIELD = re.compile("([a-zA-Z]+):\s*(.+)") | 28 FIELD = re.compile("([a-zA-Z]+):\s*(.+)") |
29 | 29 |
30 # map rich comparison to 'dpkg --compare-versions' | |
31 # map == to !=, < to >= and so on to reverse order in heap. | |
32 RICH_CMP = dict([ | |
33 ("__%s__" % a, lambda se, ot: | |
34 subprocess.call([ | |
35 "dpkg", "--compare-versions", | |
36 se.version, b, ot.version]) == 0) | |
37 for a, b in (("eq", "ne"), ("ne", "eq"), | |
38 ("lt", "ge"), ("gt", "le"), | |
39 ("le", "gt"), ("ge", "lt"))]) | |
40 | |
41 | |
42 class DebCmp(object): | 30 class DebCmp(object): |
43 """Helper class to make deb files comparable | 31 """Helper class to make deb files comparable |
44 by there versions. | 32 by there versions. |
45 """ | 33 """ |
46 | 34 |
47 def __init__(self, version, path): | 35 def __init__(self, version, path): |
48 self.version = version | 36 self.version = version |
49 self.path = path | 37 self.path = path |
50 | 38 |
51 self.__dict__.update(RICH_CMP) | 39 def __cmp__(self, other): |
40 if self.version == other.version: | |
41 return 0 | |
42 # switch lt and gt to reverse order in heap | |
43 if (subprocess.call([ | |
44 "dpkg", "--compare-versions", | |
45 self.version, "gt", other.version]) == 0): | |
46 return -1 | |
47 if (subprocess.call([ | |
48 "dpkg", "--compare-versions", | |
49 self.version, "lt", other.version]) == 0): | |
50 return +1 | |
51 return 0 | |
52 | 52 |
53 | 53 |
54 def deb_info(deb, fields=["Package", "Version"]): | 54 def deb_info(deb, fields=["Package", "Version"]): |
55 """Extract some meta info from a deb file.""" | 55 """Extract some meta info from a deb file.""" |
56 po = subprocess.Popen( | 56 po = subprocess.Popen( |
87 log.info("%d debs found" % (num-1)) | 87 log.info("%d debs found" % (num-1)) |
88 log.info("number packages: %s" % len(packages)) | 88 log.info("number packages: %s" % len(packages)) |
89 | 89 |
90 for package, debs in packages.iteritems(): | 90 for package, debs in packages.iteritems(): |
91 if len(debs) > keep: | 91 if len(debs) > keep: |
92 # full sorting is not required | 92 debs.sort() |
93 stay = frozenset([d.path for d in nsmallest(keep, debs)]) | 93 for deb in debs[keep:]: |
94 yield deb.path | |
94 | 95 |
95 for deb in debs: | 96 ## full sorting is not required |
96 if deb.path not in stay: | 97 #stay = frozenset([d.path for d in nsmallest(keep, debs)]) |
97 yield deb.path | 98 |
99 #for deb in debs: | |
100 # if deb.path not in stay: | |
101 # yield deb.path | |
98 | 102 |
99 | 103 |
100 def main(): | 104 def main(): |
101 usage = "usage: %prog [options] dir ..." | 105 usage = "usage: %prog [options] dir ..." |
102 parser = OptionParser(usage=usage) | 106 parser = OptionParser(usage=usage) |