X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=rhona;h=c7ee05bc92909685ee9b15125bb4314d293549e2;hb=0e9d5d8e31062edf50725735424fd89563ffa5a4;hp=fe887747cd9e1cfbe711e831639cb5b8295312ac;hpb=53f1d77f290ed84db4b4436bfd2748d63e0e6892;p=dak.git diff --git a/rhona b/rhona index fe887747..c7ee05bc 100755 --- a/rhona +++ b/rhona @@ -1,8 +1,8 @@ #!/usr/bin/env python # rhona, cleans up unassociated binary and source packages -# Copyright (C) 2000 James Troup -# $Id: rhona,v 1.7 2001-01-10 06:08:03 troup Exp $ +# Copyright (C) 2000, 2001 James Troup +# $Id: rhona,v 1.16 2001-06-22 23:30:21 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 @@ -37,8 +37,8 @@ import utils projectB = None Cnf = None -delete_date = None; -overrides = {}; +now_date = None; # mark newly "deleted" things as deleted "now" +delete_date = None; # delete things marked "deleted" earler than this ################################################################################################### @@ -51,239 +51,158 @@ def usage (exit_code): sys.exit(exit_code) ################################################################################################### - -# See if a given package is in the override file. Caches and only loads override files on demand. - -def in_override_p (package): - global overrides; - - if overrides == {}: - filename = Cnf["Dir::OverrideDir"] + Cnf["Rhona::OverrideFilename"]; - try: - file = utils.open_file(filename, 'r'); - except utils.cant_open_exc: - pass; - else: - for line in file.readlines(): - line = string.strip(utils.re_comments.sub('', line)) - if line != "": - overrides[line] = 1 - file.close() - - return overrides.get(package, None); - -################################################################################################### - + def check_binaries(): - global delete_date; + global delete_date, now_date; print "Checking for orphaned binary packages..." - # A nicer way to do this would be `SELECT bin FROM - # bin_associations EXCEPT SELECT id from binaries WHERE - # last_update IS NULL', but it seems postgresql can't handle that - # query as it hadn't return after I left it running for 20 minutes - # on auric. - - linked_binaries = {}; - q = projectB.query("SELECT bin FROM bin_associations"); - ql = q.getresult(); - for i in ql: - linked_binaries[i[0]] = ""; - - all_binaries = {}; - q = projectB.query("SELECT b.id, b.file FROM binaries b, files f WHERE f.last_used IS NULL AND f.id = b.file") + # 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)"""); ql = q.getresult(); - for i in ql: - all_binaries[i[0]] = i[1]; projectB.query("BEGIN WORK"); - for id in all_binaries.keys(): - if not linked_binaries.has_key(id): - projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, all_binaries[id])) + for i in ql: + file_id = i[0]; + projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, file_id)) projectB.query("COMMIT WORK"); - # Check for any binaries which are marked for eventual deletion but are now used again. + # Check for any binaries which are marked for eventual deletion + # but are now used again. - all_marked_binaries = {}; - q = projectB.query("SELECT b.id, b.file FROM binaries b, files f WHERE f.last_used IS NOT NULL AND f.id = b.file") + 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)"""); ql = q.getresult(); - for i in ql: - all_marked_binaries[i[0]] = i[1]; - projectB.query("BEGIN WORK"); - for id in all_marked_binaries.keys(): - if linked_binaries.has_key(id): - # Can't imagine why this would happen, so warn about it for now. - print "W: %s has released %s from the target list." % (id, all_marked_binaries[id]); - projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_binaries[id])); + 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; + global delete_date, now_date; print "Checking for orphaned source packages..." - # A nicer way to do this would be using `EXCEPT', but see the - # comment in check_binaries(). + # Get the list of source packages not in a suite. - linked_sources = {}; - q = projectB.query("SELECT source FROM binaries WHERE source is not null"); - ql = q.getresult(); - for i in ql: - linked_sources[i[0]] = ""; - - all_sources = {}; - all_sources_package = {}; - q = projectB.query("SELECT s.id, s.file, s.source FROM source s, files f WHERE f.last_used IS NULL AND f.id = s.file"); - ql = q.getresult(); - for i in ql: - all_sources[i[0]] = i[1]; - all_sources_package[i[0]] = i[2]; + 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)"""); - projectB.query("BEGIN WORK"); - for id in all_sources.keys(): - if not linked_sources.has_key(id): - # Is this a known source-only package? - if in_override_p(all_sources_package[id]): - continue; - # Then check to see if the source is still in any suites - untouchable = 0; - q = projectB.query("SELECT su.suite_name FROM src_associations sa, suite su WHERE sa.source = %s and su.id = sa.suite" % (id)); - for i in q.getresult(): - if Cnf.Find("Suite::%s::Untouchable" % (i[0])): - untouchable = 1; - else: - if not Cnf["Rhona::Options::No-Action"]: - projectB.query("DELETE FROM src_associations WHERE source = %s" % (id)); - - # We can't delete binary-less source-only packages if - # they're in an untouchable suite (i.e. stable)... - if untouchable: - continue; - - projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, all_sources[id])) - # Delete all other files references by .dsc too if they're not used by anyone else - q = projectB.query("SELECT f.id FROM files f, dsc_files d WHERE d.source = %d AND d.file = f.id" % (id)); - for i in q.getresult(): - q = projectB.query("SELECT id FROM dsc_files d WHERE file = %s" % (i[0])); - ql = q.getresult(); - if len(ql) == 1: - projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, i[0])); - - # If the file is used by another source package - # (e.g. because it's an .orig.tar.gz) We need to delete - # this source package's reference to it from dsc_files. - # So just clear out all references to the source file in - # dsc_files now. - if not Cnf["Rhona::Options::No-Action"]: - projectB.query("DELETE FROM dsc_files WHERE source = %s" % (id)); - - projectB.query("COMMIT WORK"); + #### 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) - # Check for any sources which are marked for eventual deletion but are now used again. - all_marked_sources = {}; - q = projectB.query("SELECT s.id, s.file FROM source s, files f WHERE f.last_used IS NOT NULL AND f.id = s.file"); ql = q.getresult(); - for i in ql: - all_marked_sources[i[0]] = i[1]; + projectB.query("BEGIN WORK"); - for id in all_marked_sources.keys(): - if linked_sources.has_key(id): - # Can't imagine why this would happen, so warn about it for now. - print "W: %s has released %s from the target list." % (id, all_marked_sources[id]); - projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_sources[id])); - # Unmark all other files references by .dsc too - q = projectB.query("SELECT file FROM dsc_files WHERE source = %d" % (id)); - ql = q.getresult(); - for i in ql: - projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (i[0])); + for i in ql: + source_id = i[0]; + dsc_file_id = i[1]; + + # Mark the .dsc file for deletion + projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, dsc_file_id)) + # Mark all other files references by .dsc too if they're not used by anyone else + x = projectB.query("SELECT f.id FROM files f, dsc_files d WHERE d.source = %s AND d.file = f.id" % (source_id)); + for j in x.getresult(): + file_id = j[0]; + y = projectB.query("SELECT id FROM dsc_files d WHERE d.file = %s" % (file_id)); + if len(y.getresult()) == 1: + projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, file_id)); projectB.query("COMMIT WORK"); - # Whee, check for any source files (i.e. non .dsc) which are - # marked for eventual deletion but are now used by a source - # package again. - all_marked_dsc_files = {} - q = projectB.query("SELECT s.id, s.file FROM source s, files f, dsc_files d WHERE f.last_used IS NOT NULL AND f.id = d.file AND d.source = s.id"); + # Check for any sources which are marked for deletion but which + # are now used again. + + 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)))"""); + + #### XXX: this should also handle deleted binaries specially (ie, not + #### reinstate sources because of them + ql = q.getresult(); - for i in ql: - all_marked_dsc_files[i[0]] = i[1]; + # Could be done in SQL; but left this way for hysterical raisins + # [and freedom to innovate don'cha know?] projectB.query("BEGIN WORK"); - for id in all_marked_dsc_files.keys(): - if linked_sources.has_key(id): - # Can't imagine why this would happen, so warn about it for now. - print "W: %s has released %s from the target list." % (id, all_marked_sources[id]); - projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_sources[id])); - # Unmark all other files references by .dsc too - q = projectB.query("SELECT file FROM dsc_files WHERE source = %d" % (id)); - ql = q.getresult(); - for i in ql: - projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (i[0])); + 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_files(): - global delete_date; + global delete_date, now_date; + + # FIXME: this is evil; nothing should ever be in this state. if + # they are, it's a bug and the files should not be auto-deleted. + + return; print "Checking for unused files..." - # Check for files not references in either binaries or dsc_files - used = {}; - q = projectB.query("SELECT file FROM binaries"); - for i in q.getresult(): - used[i[0]] = ""; - q = projectB.query("SELECT file FROM dsc_files"); - for i in q.getresult(): - used[i[0]] = ""; - - all = {}; - q = projectB.query("SELECT f.id, l.path, f.filename FROM files f, location l WHERE f.location = l.id;"); - for i in q.getresult(): - all[i[0]] = i[1] + i[2]; + 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)"""); projectB.query("BEGIN WORK"); - for id in all.keys(): - if not used.has_key(id): - projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, id)); + 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; + global delete_date, now_date; # We do this here so that the binaries we remove will have their # source also removed (if possible). + # 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"]: 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)); - sys.stdout.write("done. (%d)]\n" % (int(time.time()-before))); + sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before))); def clean(): - global delete_date; + global delete_date, now_date; count = 0; size = 0; print "Cleaning out packages..." - # Ensure destination directory exists - dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"]; + date = time.strftime("%Y-%m-%d", time.localtime(time.time())); + dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"] + '/' + date; if not os.path.exists(dest): os.mkdir(dest); - # Delete from source (dsc_file should already be done!) + # Delete from source if not Cnf["Rhona::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)); - sys.stdout.write("done. (%d)]\n" % (int(time.time()-before))); + 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(): filename = i[0] + i[1]; if not os.path.exists(filename): - sys.stderr.write("E: can not find %s.\n" % (filename)); + utils.warn("can not find '%s'." % (filename)); continue; if os.path.isfile(filename): if os.path.islink(filename): @@ -295,44 +214,43 @@ def clean(): else: size = size + os.stat(filename)[stat.ST_SIZE]; count = 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 = utils.find_next_free(dest_filename); + if Cnf["Rhona::Options::No-Action"]: - print "Cleaning %s to %s..." % (filename, dest); + print "Cleaning %s -> %s ..." % (filename, dest_filename); else: - utils.move(filename, dest); + utils.move(filename, dest_filename); else: - sys.stderr.write("%s is neither symlink nor file?!\n" % (filename)); - sys.exit(1); - # delete from files + utils.fubar("%s is neither symlink nor file?!" % (filename)); + + # Delete from the 'files' table if not Cnf["Rhona::Options::No-Action"]: before = time.time(); sys.stdout.write("[Deleting from files table... "); projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date)); - sys.stdout.write("done. (%d)]\n" % (int(time.time()-before))); + sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before))); 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..." - used = {}; - q = projectB.query("SELECT maintainer FROM binaries WHERE maintainer IS NOT NULL"); - for i in q.getresult(): - used[i[0]] = ""; - q = projectB.query("SELECT maintainer FROM source WHERE maintainer IS NOT NULL"); - for i in q.getresult(): - used[i[0]] = ""; - - all = {}; - q = projectB.query("SELECT id, name FROM maintainer"); - for i in q.getresult(): - all[i[0]] = i[1]; + 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)"""); + ql = q.getresult(); count = 0; projectB.query("BEGIN WORK"); - for id in all.keys(): - if not used.has_key(id): - if not Cnf["Rhona::Options::No-Action"]: - projectB.query("DELETE FROM maintainer WHERE id = %s" % (id)); + for i in ql: + maintainer_id = i[0]; + if not Cnf["Rhona::Options::No-Action"]: + projectB.query("DELETE FROM maintainer WHERE id = %s" % (maintainer_id)); count = count + 1; projectB.query("COMMIT WORK"); @@ -340,34 +258,33 @@ def clean_maintainers(): sys.stderr.write("Cleared out %d maintainer entries.\n" % (count)); def main(): - global Cnf, projectB, delete_date; + global Cnf, projectB, delete_date, now_date; - projectB = pg.connect('projectb', 'localhost'); - apt_pkg.init(); Cnf = apt_pkg.newConfiguration(); apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file()); + projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"])); + 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); - if Cnf["Rhona::Options::Help"]: + Options = Cnf.SubTree("Rhona::Options") + + if Options["Help"]: usage(0); - if Cnf["Rhona::Options::Version"]: + if Options["Version"]: print "rhona version 0.0000000000"; usage(0); - override_filename = Cnf["Dir::OverrideDir"] + Cnf["Rhona::OverrideFilename"]; - if not os.access(override_filename, os.R_OK): - sys.stderr.write("W: Could not find source-only override file '%s'.\n" % (override_filename)); - - delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())); - + now_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())); + delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"]))); + check_binaries(); clean_binaries(); check_sources();