]> git.decadent.org.uk Git - dak.git/blobdiff - rhona
Add new top level directories
[dak.git] / rhona
diff --git a/rhona b/rhona
index 877cdb01a9c8e8a82b0805d17063c16446973da3..fdbcf26da1347c1d209d65cab0257611b2730142 100755 (executable)
--- 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 <james@nocrew.org>
-# $Id: rhona,v 1.3 2000-12-13 03:18:50 troup Exp $
+# rhona, cleans up unassociated binary and source packages
+# Copyright (C) 2000, 2001, 2002, 2003  James Troup <james@nocrew.org>
+# $Id: rhona,v 1.29 2005-11-25 06:59:45 ajt 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,6 +18,8 @@
 # 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|<elmo> 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
 #
 # 07:05|<Culus> elmo: Augh! <brain jumps out of skull>
 
-import pg, string, os, 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
+
+################################################################################
+
+def usage (exit_code=0):
+    print """Usage: rhona [OPTIONS]
+Clean old packages from suites.
+
+  -n, --no-action            don't do anything
+  -h, --help                 show this help and exit"""
+    sys.exit(exit_code)
+
+################################################################################
 
 def check_binaries():
-    # 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")
+    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, 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();
-    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):
-            date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-(4*(24*60*60))))
-            projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (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.
-
-    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")
+    # 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 1 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():
-    # A nicer way to do this would be using `EXCEPT', but see the
-    # commeint in process_binary.
+    global delete_date, now_date;
+
+    print "Checking for orphaned source packages..."
+
+    # 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, 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)
 
-    linked_sources = {};
-    q = projectB.query("SELECT source FROM binaries WHERE source is not null");
     ql = q.getresult();
+
+    projectB.query("BEGIN WORK");
     for i in ql:
-        linked_sources[i[0]] = "";
-    
-    all_sources = {};
-    q = projectB.query("SELECT s.id, s.file FROM source s, files f WHERE f.last_used IS NULL AND f.id = s.file")
+        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");
+
+    # 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 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
+
     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:
-        all_sources[i[0]] = i[1];
+        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, 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..."
+    q = projectB.query("""
+SELECT id FROM files f
+  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 id in all_sources.keys():
-        if not linked_sources.has_key(id):
-            date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-(4*(24*60*60))))
-            projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (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));
-            ql = q.getresult();
-            for i in ql:
-                q_others = projectB.query("SELECT file FROM dsc_files d WHERE file = %s" % (i[0]));
-                ql_others = q.getresult();
-                if len(ql) == 1:
-                    projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (date, i[0]));
+    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");
 
-    # 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");
+def clean_binaries():
+    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 Options["No-Action"]:
+        before = time.time();
+        sys.stdout.write("[Deleting from binaries table... ");
+        sys.stderr.write("DELETE FROM binaries WHERE EXISTS (SELECT 1 FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')\n" % (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;
+    size = 0;
+
+    print "Cleaning out packages..."
+
+    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 Options["No-Action"]:
+        before = time.time();
+        sys.stdout.write("[Deleting from source table... ");
+        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():
+        filename = i[0] + i[1];
+        if not os.path.exists(filename):
+            utils.warn("can not find '%s'." % (filename));
+            continue;
+        if os.path.isfile(filename):
+            if os.path.islink(filename):
+                count += 1;
+                if Options["No-Action"]:
+                    print "Removing symlink %s..." % (filename);
+                else:
+                    os.unlink(filename);
+            else:
+                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 = 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 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 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..."
+
+    q = projectB.query("""
+SELECT m.id FROM maintainer m
+  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();
-    for i in ql:
-        all_marked_sources[i[0]] = i[1];
+
+    count = 0;
     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:
+        maintainer_id = i[0];
+        if not Options["No-Action"]:
+            projectB.query("DELETE FROM maintainer WHERE id = %s" % (maintainer_id));
+            count += 1;
     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");
+    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();
-    for i in ql:
-        all_marked_dsc_files[i[0]] = i[1];
+
+    count = 0;
     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:
+        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");
 
-def clean_binaries():
-    date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"])));
-    print projectB.query("DELETE FROM binaries WHERE file IN (SELECT id FROM files WHERE last_used < '%s')" % (date));
+    if count > 0:
+        sys.stderr.write("Cleared out %d fingerprint entries.\n" % (count));
 
-def clean():
-    date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"])));
-    # Delete from source + dsc_files
-    q = projectB.query("SELECT l.path, f.filename FROM location l, files f WHERE f.last_used < '%s' AND l.id = f.location" % (date));
+################################################################################
+
+def clean_queue_build():
+    global now_date;
+
+    if not Cnf.ValueList("Dinstall::QueueBuildSuites") or Options["No-Action"]:
+        return;
+
+    print "Cleaning out queue build symlinks..."
+
+    our_delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::QueueBuildStayOfExecution"])));
+    count = 0;
+
+    q = projectB.query("SELECT filename FROM queue_build WHERE last_used <= '%s'" % (our_delete_date));
     for i in q.getresult():
-        filename = i[0] + i[1];
+        filename = i[0];
         if not os.path.exists(filename):
-            sys.stderr.write("E: can not find %s.\n" % (filename));
+            utils.warn("%s (from queue_build) doesn't exist." % (filename));
             continue;
-        dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"];
-        print "Cleaning %s to %s..." % (filename, dest);
-        #utils.move(filename, dest);
-    # delete from files
+        if not Cnf.FindB("Dinstall::SecurityQueueBuild") and not os.path.islink(filename):
+            utils.fubar("%s (from queue_build) should be a symlink but isn't." % (filename));
+        os.unlink(filename);
+        count += 1;
+    projectB.query("DELETE FROM queue_build WHERE last_used <= '%s'" % (our_delete_date));
+
+    if count:
+        sys.stderr.write("Cleaned %d queue_build files.\n" % (count));
+
+################################################################################
 
 def main():
-    global Cnf, projectB;
-    
-    projectB = pg.connect('projectb', 'localhost');
+    global Cnf, Options, projectB, delete_date, now_date;
 
-    apt_pkg.init();
-    
-    Cnf = apt_pkg.newConfiguration();
-    apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
+    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")];
+
+    apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
+    Options = Cnf.SubTree("Rhona::Options")
+
+    if Options["Help"]:
+        usage();
+
+    projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
+
+    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"])));
 
-    print "Checking for orphaned binary packages..."
     check_binaries();
-    print "Cleaning binaries from the DB..."
     clean_binaries();
-    print "Checking for orphaned source packages..."
     check_sources();
-    print "Cleaning orphaned packages..."
+    check_files();
     clean();
+    clean_maintainers();
+    clean_fingerprints();
+    clean_queue_build();
+
+################################################################################
 
 if __name__ == '__main__':
     main()