+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)));