X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=rhona;h=e3c7fc561a167dbe56f4948b10140064d7eaf2b1;hb=dd0ee5b67c650470139ffa8a50ae6b83a92ca76a;hp=c05db3129836a4bdc39320bcf59e4d85ea83caf0;hpb=6ada259d555e36a1546789c280edc3f944b7de7f;p=dak.git diff --git a/rhona b/rhona index c05db312..e3c7fc56 100755 --- a/rhona +++ b/rhona @@ -1,8 +1,8 @@ #!/usr/bin/env python # rhona, cleans up unassociated binary and source packages -# Copyright (C) 2000, 2001 James Troup -# $Id: rhona,v 1.15 2001-06-22 22:53:14 troup Exp $ +# Copyright (C) 2000, 2001, 2002, 2003 James Troup +# $Id: rhona,v 1.27 2003-09-07 13:52:20 troup Exp $ # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,7 +18,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -################################################################################################### +################################################################################ # 07:05| well.. *shrug*.. no, probably not.. but to fix it, # | we're going to have to implement reference counting @@ -27,57 +27,43 @@ # # 07:05| elmo: Augh! -################################################################################################### +################################################################################ -import os, pg, stat, string, sys, time +import os, pg, stat, sys, time import apt_pkg import utils -################################################################################################### +################################################################################ -projectB = None -Cnf = None +projectB = None; +Cnf = None; +Options = None; now_date = None; # mark newly "deleted" things as deleted "now" delete_date = None; # delete things marked "deleted" earler than this -tried_too_hard_exc = "Tried too hard to find a free filename for %s; something's gone Pete Tong"; +################################################################################ -################################################################################################### +def usage (exit_code=0): + print """Usage: rhona [OPTIONS] +Clean old packages from suites. -def usage (exit_code): - print """Usage: rhona [OPTION]... [CHANGES]... - -D, --debug=VALUE debug - -n, --no-action don't do anything - -v, --verbose be verbose - -V, --version display version number and exit""" + -n, --no-action don't do anything + -h, --help show this help and exit""" sys.exit(exit_code) -################################################################################################### - -def find_next_free (dest): - extra = 0; - orig_dest = dest; - too_much = 100; - while os.path.exists(dest) and extra < too_much: - dest = orig_dest + '.' + repr(extra); - extra = extra + 1; - if extra >= too_much: - raise tried_too_hard_exc; - return dest; - -# FIXME: why can't we make (sane speed) UPDATEs out of these SELECTs? +################################################################################ def check_binaries(): global delete_date, now_date; - + print "Checking for orphaned binary packages..." # Get the list of binary packages not in a suite and mark them for # deletion. - q = projectB.query(""" -SELECT b.file FROM binaries b WHERE NOT EXISTS - (SELECT ba.bin FROM bin_associations ba WHERE ba.bin = b.id)"""); +SELECT b.file FROM binaries b, files f + WHERE f.last_used IS NULL AND b.file = f.id + AND NOT EXISTS (SELECT 1 FROM bin_associations ba WHERE ba.bin = b.id)"""); ql = q.getresult(); projectB.query("BEGIN WORK"); @@ -88,37 +74,39 @@ SELECT b.file FROM binaries b WHERE NOT EXISTS # Check for any binaries which are marked for eventual deletion # but are now used again. - q = projectB.query(""" SELECT b.file FROM binaries b, files f - WHERE f.last_used IS NOT NULL AND f.id = b.file AND - EXISTS (SELECT suite FROM bin_associations ba WHERE ba.bin = b.id)"""); + WHERE f.last_used IS NOT NULL AND f.id = b.file + AND EXISTS (SELECT 1 FROM bin_associations ba WHERE ba.bin = b.id)"""); ql = q.getresult(); + projectB.query("BEGIN WORK"); for i in ql: file_id = i[0]; projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id)); projectB.query("COMMIT WORK"); +######################################## + def check_sources(): global delete_date, now_date; print "Checking for orphaned source packages..." - # Get the list of source packages not in a suite. - + # Get the list of source packages not in a suite and not used by + # any binaries. q = projectB.query(""" -SELECT s.id, s.file FROM source s - WHERE NOT EXISTS - (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id) - AND NOT EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)"""); +SELECT s.id, s.file FROM source s, files f + WHERE f.last_used IS NULL AND s.file = f.id + AND NOT EXISTS (SELECT 1 FROM src_associations sa WHERE sa.source = s.id) + AND NOT EXISTS (SELECT 1 FROM binaries b WHERE b.source = s.id)"""); #### XXX: this should ignore cases where the files for the binary b #### have been marked for deletion (so the delay between bins go #### byebye and sources go byebye is 0 instead of StayOfExecution) ql = q.getresult(); - + projectB.query("BEGIN WORK"); for i in ql: source_id = i[0]; @@ -141,8 +129,8 @@ SELECT s.id, s.file FROM source s q = projectB.query(""" SELECT f.id FROM source s, files f, dsc_files df WHERE f.last_used IS NOT NULL AND s.id = df.source AND df.file = f.id - AND ((EXISTS (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id)) - OR (EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)))"""); + AND ((EXISTS (SELECT 1 FROM src_associations sa WHERE sa.source = s.id)) + OR (EXISTS (SELECT 1 FROM binaries b WHERE b.source = s.id)))"""); #### XXX: this should also handle deleted binaries specially (ie, not #### reinstate sources because of them @@ -156,6 +144,8 @@ SELECT f.id FROM source s, files f, dsc_files df projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id)); projectB.query("COMMIT WORK"); +######################################## + def check_files(): global delete_date, now_date; @@ -165,18 +155,17 @@ def check_files(): return; print "Checking for unused files..." - q = projectB.query(""" SELECT id FROM files f - WHERE NOT EXISTS (SELECT id FROM binaries b WHERE b.file = f.id) - AND NOT EXISTS (SELECT id FROM dsc_files df WHERE df.file = f.id)"""); + WHERE NOT EXISTS (SELECT 1 FROM binaries b WHERE b.file = f.id) + AND NOT EXISTS (SELECT 1 FROM dsc_files df WHERE df.file = f.id)"""); projectB.query("BEGIN WORK"); for i in q.getresult(): file_id = i[0]; projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (now_date, file_id)); projectB.query("COMMIT WORK"); - + def clean_binaries(): global delete_date, now_date; @@ -186,12 +175,14 @@ def clean_binaries(): # XXX: why doesn't this remove the files here as well? I don't think it # buys anything keeping this separate print "Cleaning binaries from the DB..." - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from binaries table... "); - projectB.query("DELETE FROM binaries WHERE EXISTS (SELECT id FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')" % (delete_date)); + projectB.query("DELETE FROM binaries WHERE EXISTS (SELECT 1 FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')" % (delete_date)); sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before))); +######################################## + def clean(): global delete_date, now_date; count = 0; @@ -199,19 +190,19 @@ def clean(): print "Cleaning out packages..." - date = time.strftime("%Y-%m-%d", time.localtime(time.time())); + date = time.strftime("%Y-%m-%d"); dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"] + '/' + date; if not os.path.exists(dest): os.mkdir(dest); - + # Delete from source - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from source table... "); - projectB.query("DELETE FROM dsc_files WHERE EXISTS (SELECT df.id FROM source s, files f, dsc_files df WHERE f.last_used <= '%s' AND s.file = f.id AND s.id = df.source AND df.id = dsc_files.id)" % (delete_date)); - projectB.query("DELETE FROM source WHERE EXISTS (SELECT id FROM files WHERE source.file = files.id AND files.last_used <= '%s')" % (delete_date)); + projectB.query("DELETE FROM dsc_files WHERE EXISTS (SELECT 1 FROM source s, files f, dsc_files df WHERE f.last_used <= '%s' AND s.file = f.id AND s.id = df.source AND df.id = dsc_files.id)" % (delete_date)); + projectB.query("DELETE FROM source WHERE EXISTS (SELECT 1 FROM files WHERE source.file = files.id AND files.last_used <= '%s')" % (delete_date)); sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before))); - + # Delete files from the pool q = projectB.query("SELECT l.path, f.filename FROM location l, files f WHERE f.last_used <= '%s' AND l.id = f.location" % (delete_date)); for i in q.getresult(): @@ -221,29 +212,29 @@ def clean(): continue; if os.path.isfile(filename): if os.path.islink(filename): - count = count + 1; - if Cnf["Rhona::Options::No-Action"]: + count += 1; + if Options["No-Action"]: print "Removing symlink %s..." % (filename); else: os.unlink(filename); else: - size = size + os.stat(filename)[stat.ST_SIZE]; - count = count + 1; + size += os.stat(filename)[stat.ST_SIZE]; + count += 1; dest_filename = dest + '/' + os.path.basename(filename); # If the destination file exists; try to find another filename to use if os.path.exists(dest_filename): - dest_filename = find_next_free(dest_filename); - - if Cnf["Rhona::Options::No-Action"]: + dest_filename = utils.find_next_free(dest_filename); + + if Options["No-Action"]: print "Cleaning %s -> %s ..." % (filename, dest_filename); else: utils.move(filename, dest_filename); else: utils.fubar("%s is neither symlink nor file?!" % (filename)); - + # Delete from the 'files' table - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: before = time.time(); sys.stdout.write("[Deleting from files table... "); projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date)); @@ -251,61 +242,114 @@ def clean(): if count > 0: sys.stderr.write("Cleaned %d files, %s.\n" % (count, utils.size_type(size))); +################################################################################ + def clean_maintainers(): print "Cleaning out unused Maintainer entries..." - + q = projectB.query(""" SELECT m.id FROM maintainer m - WHERE NOT EXISTS (SELECT id FROM binaries b WHERE b.maintainer = m.id) - AND NOT EXISTS (SELECT id FROM source s WHERE s.maintainer = m.id)"""); + WHERE NOT EXISTS (SELECT 1 FROM binaries b WHERE b.maintainer = m.id) + AND NOT EXISTS (SELECT 1 FROM source s WHERE s.maintainer = m.id)"""); ql = q.getresult(); count = 0; projectB.query("BEGIN WORK"); for i in ql: maintainer_id = i[0]; - if not Cnf["Rhona::Options::No-Action"]: + if not Options["No-Action"]: projectB.query("DELETE FROM maintainer WHERE id = %s" % (maintainer_id)); - count = count + 1; + count += 1; projectB.query("COMMIT WORK"); if count > 0: sys.stderr.write("Cleared out %d maintainer entries.\n" % (count)); +################################################################################ + +def clean_fingerprints(): + print "Cleaning out unused fingerprint entries..." + + q = projectB.query(""" +SELECT f.id FROM fingerprint f + WHERE NOT EXISTS (SELECT 1 FROM binaries b WHERE b.sig_fpr = f.id) + AND NOT EXISTS (SELECT 1 FROM source s WHERE s.sig_fpr = f.id)"""); + ql = q.getresult(); + + count = 0; + projectB.query("BEGIN WORK"); + for i in ql: + fingerprint_id = i[0]; + if not Options["No-Action"]: + projectB.query("DELETE FROM fingerprint WHERE id = %s" % (fingerprint_id)); + count += 1; + projectB.query("COMMIT WORK"); + + if count > 0: + sys.stderr.write("Cleared out %d fingerprint entries.\n" % (count)); + +################################################################################ + +def clean_accepted_autobuild(): + global now_date; + + if not Cnf.ValueList("Dinstall::AcceptedAutoBuildSuites") or Options["No-Action"]: + return; + + print "Cleaning out accepted autobuild symlinks..." + + our_delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::AcceptedAutoBuildStayOfExecution"]))); + count = 0; + + q = projectB.query("SELECT filename FROM accepted_autobuild WHERE last_used <= '%s'" % (our_delete_date)); + for i in q.getresult(): + filename = i[0]; + if not os.path.exists(filename): + utils.warn("%s (from accepted_autobuild) doesn't exist." % (filename)); + continue; + if not Cnf.FindB("Dinstall::SecurityAcceptedAutoBuild") and not os.path.islink(filename): + utils.fubar("%s (from accepted_autobuild) should be a symlink but isn't." % (filename)); + os.unlink(filename); + count += 1; + projectB.query("DELETE FROM accepted_autobuild WHERE last_used <= '%s'" % (our_delete_date)); + + if count: + sys.stderr.write("Cleaned %d accepted-autobuild files.\n" % (count)); + +################################################################################ + def main(): - global Cnf, projectB, delete_date, now_date; - - apt_pkg.init(); - - Cnf = apt_pkg.newConfiguration(); - apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file()); + global Cnf, Options, projectB, delete_date, now_date; - projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"])); + Cnf = utils.get_conf() + for i in ["Help", "No-Action" ]: + if not Cnf.has_key("Rhona::Options::%s" % (i)): + Cnf["Rhona::Options::%s" % (i)] = ""; + + Arguments = [('h',"help","Rhona::Options::Help"), + ('n',"no-action","Rhona::Options::No-Action")]; - Arguments = [('D',"debug","Rhona::Options::Debug", "IntVal"), - ('h',"help","Rhona::Options::Help"), - ('n',"no-action","Rhona::Options::No-Action"), - ('V',"version","Rhona::Options::Version")]; - apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv); Options = Cnf.SubTree("Rhona::Options") if Options["Help"]: - usage(0); - - if Options["Version"]: - print "rhona version 0.0000000000"; - usage(0); + usage(); + + projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"])); - now_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())); + now_date = time.strftime("%Y-%m-%d %H:%M"); delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"]))); - + check_binaries(); clean_binaries(); check_sources(); check_files(); clean(); clean_maintainers(); + clean_fingerprints(); + clean_accepted_autobuild(); + +################################################################################ if __name__ == '__main__': main()