3 # rhona, cleans up unassociated binary and source packages
4 # Copyright (C) 2000, 2001 James Troup <james@nocrew.org>
5 # $Id: rhona,v 1.19 2001-11-24 18:42:10 troup Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ################################################################################
23 # 07:05|<elmo> well.. *shrug*.. no, probably not.. but to fix it,
24 # | we're going to have to implement reference counting
25 # | through dependencies.. do we really want to go down
28 # 07:05|<Culus> elmo: Augh! <brain jumps out of skull>
30 ################################################################################
32 import os, pg, stat, string, sys, time
36 ################################################################################
41 now_date = None; # mark newly "deleted" things as deleted "now"
42 delete_date = None; # delete things marked "deleted" earler than this
44 ################################################################################
46 def usage (exit_code=0):
47 print """Usage: rhona [OPTIONS]
48 Clean old packages from suites.
50 -n, --no-action don't do anything
51 -h, --help show this help and exit"""
54 ################################################################################
57 global delete_date, now_date;
59 print "Checking for orphaned binary packages..."
61 # Get the list of binary packages not in a suite and mark them for
64 q = projectB.query("""
65 SELECT b.file FROM binaries b WHERE NOT EXISTS
66 (SELECT ba.bin FROM bin_associations ba WHERE ba.bin = b.id)""");
69 projectB.query("BEGIN WORK");
72 projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, file_id))
73 projectB.query("COMMIT WORK");
75 # Check for any binaries which are marked for eventual deletion
76 # but are now used again.
78 q = projectB.query("""
79 SELECT b.file FROM binaries b, files f
80 WHERE f.last_used IS NOT NULL AND f.id = b.file AND
81 EXISTS (SELECT suite FROM bin_associations ba WHERE ba.bin = b.id)""");
83 projectB.query("BEGIN WORK");
86 projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id));
87 projectB.query("COMMIT WORK");
90 global delete_date, now_date;
92 print "Checking for orphaned source packages..."
94 # Get the list of source packages not in a suite.
96 q = projectB.query("""
97 SELECT s.id, s.file FROM source s
99 (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id)
100 AND NOT EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)""");
102 #### XXX: this should ignore cases where the files for the binary b
103 #### have been marked for deletion (so the delay between bins go
104 #### byebye and sources go byebye is 0 instead of StayOfExecution)
108 projectB.query("BEGIN WORK");
113 # Mark the .dsc file for deletion
114 projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, dsc_file_id))
115 # Mark all other files references by .dsc too if they're not used by anyone else
116 x = projectB.query("SELECT f.id FROM files f, dsc_files d WHERE d.source = %s AND d.file = f.id" % (source_id));
117 for j in x.getresult():
119 y = projectB.query("SELECT id FROM dsc_files d WHERE d.file = %s" % (file_id));
120 if len(y.getresult()) == 1:
121 projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s AND last_used IS NULL" % (now_date, file_id));
122 projectB.query("COMMIT WORK");
124 # Check for any sources which are marked for deletion but which
125 # are now used again.
127 q = projectB.query("""
128 SELECT f.id FROM source s, files f, dsc_files df
129 WHERE f.last_used IS NOT NULL AND s.id = df.source AND df.file = f.id
130 AND ((EXISTS (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id))
131 OR (EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)))""");
133 #### XXX: this should also handle deleted binaries specially (ie, not
134 #### reinstate sources because of them
137 # Could be done in SQL; but left this way for hysterical raisins
138 # [and freedom to innovate don'cha know?]
139 projectB.query("BEGIN WORK");
142 projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id));
143 projectB.query("COMMIT WORK");
146 global delete_date, now_date;
148 # FIXME: this is evil; nothing should ever be in this state. if
149 # they are, it's a bug and the files should not be auto-deleted.
153 print "Checking for unused files..."
155 q = projectB.query("""
156 SELECT id FROM files f
157 WHERE NOT EXISTS (SELECT id FROM binaries b WHERE b.file = f.id)
158 AND NOT EXISTS (SELECT id FROM dsc_files df WHERE df.file = f.id)""");
160 projectB.query("BEGIN WORK");
161 for i in q.getresult():
163 projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (now_date, file_id));
164 projectB.query("COMMIT WORK");
166 def clean_binaries():
167 global delete_date, now_date;
169 # We do this here so that the binaries we remove will have their
170 # source also removed (if possible).
172 # XXX: why doesn't this remove the files here as well? I don't think it
173 # buys anything keeping this separate
174 print "Cleaning binaries from the DB..."
175 if not Options["No-Action"]:
176 before = time.time();
177 sys.stdout.write("[Deleting from binaries table... ");
178 projectB.query("DELETE FROM binaries WHERE EXISTS (SELECT id FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')" % (delete_date));
179 sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before)));
182 global delete_date, now_date;
186 print "Cleaning out packages..."
188 date = time.strftime("%Y-%m-%d", time.localtime(time.time()));
189 dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"] + '/' + date;
190 if not os.path.exists(dest):
194 if not Options["No-Action"]:
195 before = time.time();
196 sys.stdout.write("[Deleting from source table... ");
197 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));
198 projectB.query("DELETE FROM source WHERE EXISTS (SELECT id FROM files WHERE source.file = files.id AND files.last_used <= '%s')" % (delete_date));
199 sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before)));
201 # Delete files from the pool
202 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));
203 for i in q.getresult():
204 filename = i[0] + i[1];
205 if not os.path.exists(filename):
206 utils.warn("can not find '%s'." % (filename));
208 if os.path.isfile(filename):
209 if os.path.islink(filename):
211 if Options["No-Action"]:
212 print "Removing symlink %s..." % (filename);
216 size = size + os.stat(filename)[stat.ST_SIZE];
219 dest_filename = dest + '/' + os.path.basename(filename);
220 # If the destination file exists; try to find another filename to use
221 if os.path.exists(dest_filename):
222 dest_filename = utils.find_next_free(dest_filename);
224 if Options["No-Action"]:
225 print "Cleaning %s -> %s ..." % (filename, dest_filename);
227 utils.move(filename, dest_filename);
229 utils.fubar("%s is neither symlink nor file?!" % (filename));
231 # Delete from the 'files' table
232 if not Options["No-Action"]:
233 before = time.time();
234 sys.stdout.write("[Deleting from files table... ");
235 projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date));
236 sys.stdout.write("done. (%d seconds)]\n" % (int(time.time()-before)));
238 sys.stderr.write("Cleaned %d files, %s.\n" % (count, utils.size_type(size)));
240 def clean_maintainers():
241 print "Cleaning out unused Maintainer entries..."
243 q = projectB.query("""
244 SELECT m.id FROM maintainer m
245 WHERE NOT EXISTS (SELECT id FROM binaries b WHERE b.maintainer = m.id)
246 AND NOT EXISTS (SELECT id FROM source s WHERE s.maintainer = m.id)""");
250 projectB.query("BEGIN WORK");
252 maintainer_id = i[0];
253 if not Options["No-Action"]:
254 projectB.query("DELETE FROM maintainer WHERE id = %s" % (maintainer_id));
256 projectB.query("COMMIT WORK");
259 sys.stderr.write("Cleared out %d maintainer entries.\n" % (count));
261 ################################################################################
264 global Cnf, Options, projectB, delete_date, now_date;
266 Cnf = utils.get_conf()
267 for i in ["Help", "No-Action" ]:
268 if not Cnf.has_key("Rhona::Options::%s" % (i)):
269 Cnf["Rhona::Options::%s" % (i)] = "";
271 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
273 Arguments = [('h',"help","Rhona::Options::Help"),
274 ('n',"no-action","Rhona::Options::No-Action")];
276 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
277 Options = Cnf.SubTree("Rhona::Options")
282 now_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()));
283 delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"])));
292 ################################################################################
294 if __name__ == '__main__':