#!/usr/bin/env python # rhona, cleans up unassociated binary and source packages # Copyright (C) 2000, 2001 James Troup # $Id: rhona,v 1.9 2001-03-14 20:31:56 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 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # 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 # | through dependencies.. do we really want to go down # | that road? # # 07:05| elmo: Augh! ################################################################################################### import os, pg, stat, string, sys, time import apt_pkg import utils ################################################################################################### projectB = None Cnf = None delete_date = None; overrides = {}; ################################################################################################### 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""" sys.exit(exit_code) ################################################################################################### # FIXME: why can't we make (sane speed) UPDATEs out of these SELECTs? def check_binaries(): global delete_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)"""); ql = q.getresult(); projectB.query("BEGIN WORK"); for i in ql: file_id = i[0]; projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, file_id)) projectB.query("COMMIT WORK"); # 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)"""); 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; print "Checking for orphaned source packages..." # Get the list of source packages not in a suite and not linked to # by any binary packages. 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)"""); ql = q.getresult(); projectB.query("BEGIN WORK"); 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" % (delete_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 = i[0]; y = projectB.query("SELECT id FROM dsc_files d WHERE file = %s" % (file_id)); if len(y.getresult()) == 1: projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, file_id)); projectB.query("COMMIT WORK"); # 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)))"""); ql = q.getresult(); # Could be done in SQL; but left this way for hysterical raisins # [and freedom to innovate don'cha know?] 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_files(): global delete_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]; 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)); projectB.query("COMMIT WORK"); def clean_binaries(): global delete_date; # We do this here so that the binaries we remove will have their # source also removed (if possible). 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))); def clean(): global delete_date; count = 0; size = 0; print "Cleaning out packages..." # Ensure destination directory exists dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"]; if not os.path.exists(dest): os.mkdir(dest); # 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))); # 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)); continue; if os.path.isfile(filename): if os.path.islink(filename): count = count + 1; if Cnf["Rhona::Options::No-Action"]: print "Removing symlink %s..." % (filename); else: os.unlink(filename); else: size = size + os.stat(filename)[stat.ST_SIZE]; count = count + 1; if Cnf["Rhona::Options::No-Action"]: print "Cleaning %s to %s..." % (filename, dest); else: utils.move(filename, dest); else: sys.stderr.write("%s is neither symlink nor file?!\n" % (filename)); sys.exit(1); # delete from files 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))); 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]; 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)); count = count + 1; projectB.query("COMMIT WORK"); if count > 0: sys.stderr.write("Cleared out %d maintainer entries.\n" % (count)); def main(): global Cnf, projectB, delete_date; projectB = pg.connect('projectb', 'localhost'); apt_pkg.init(); Cnf = apt_pkg.newConfiguration(); apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file()); 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); 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()-int(Cnf["Rhona::StayOfExecution"]))); check_binaries(); clean_binaries(); check_sources(); #check_files(); clean(); clean_maintainers(); if __name__ == '__main__': main()