]> git.decadent.org.uk Git - dak.git/blob - rhona
Check size in .dsc and fix up rhona to work on auric.
[dak.git] / rhona
1 #!/usr/bin/env python
2
3 # rhona, cleans up unassociated binary and source packages
4 # Copyright (C) 2000  James Troup <james@nocrew.org>
5 # $Id: rhona,v 1.6 2000-12-20 08:15:35 troup Exp $
6
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.
11
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.
16
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
20
21 ###################################################################################################
22
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
26 #      |       that road?
27 #
28 # 07:05|<Culus> elmo: Augh! <brain jumps out of skull>
29
30 ###################################################################################################
31
32 import os, pg, stat, string, sys, time
33 import apt_pkg
34 import utils
35
36 ###################################################################################################
37
38 projectB = None
39 Cnf = None
40 delete_date = None;
41 overrides = {};
42
43 ###################################################################################################
44
45 def usage (exit_code):
46     print """Usage: rhona [OPTION]... [CHANGES]...
47   -D, --debug=VALUE         debug
48   -n, --no-action           don't do anything
49   -v, --verbose             be verbose
50   -V, --version             display version number and exit"""
51     sys.exit(exit_code)
52
53 ###################################################################################################
54
55 # See if a given package is in the override file.  Caches and only loads override files on demand.
56
57 def in_override_p (package):
58     global overrides;
59
60     if overrides == {}:
61         filename = Cnf["Dir::OverrideDir"] + Cnf["Rhona::OverrideFilename"];
62         file = utils.open_file(filename, 'r');
63         for line in file.readlines():
64             line = string.strip(utils.re_comments.sub('', line))
65             if line != "":
66                 overrides[line] = 1
67         file.close()
68
69     return overrides.get(package, None);
70
71 ###################################################################################################
72
73 def check_binaries():
74     global delete_date;
75     
76     print "Checking for orphaned binary packages..."
77
78     # A nicer way to do this would be `SELECT bin FROM
79     # bin_associations EXCEPT SELECT id from binaries WHERE
80     # last_update IS NULL', but it seems postgresql can't handle that
81     # query as it hadn't return after I left it running for 20 minutes
82     # on auric.
83     
84     linked_binaries = {};
85     q = projectB.query("SELECT bin FROM bin_associations");
86     ql = q.getresult();
87     for i in ql:
88         linked_binaries[i[0]] = "";
89     
90     all_binaries = {};
91     q = projectB.query("SELECT b.id, b.file FROM binaries b, files f WHERE f.last_used IS NULL AND f.id = b.file")
92     ql = q.getresult();
93     for i in ql:
94         all_binaries[i[0]] = i[1];
95
96     projectB.query("BEGIN WORK");
97     for id in all_binaries.keys():
98         if not linked_binaries.has_key(id):
99             projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, all_binaries[id]))
100     projectB.query("COMMIT WORK");
101
102     # Check for any binaries which are marked for eventual deletion but are now used again.
103
104     all_marked_binaries = {};
105     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")
106     ql = q.getresult();
107     for i in ql:
108         all_marked_binaries[i[0]] = i[1];
109         
110     projectB.query("BEGIN WORK");
111     for id in all_marked_binaries.keys():
112         if linked_binaries.has_key(id):
113             # Can't imagine why this would happen, so warn about it for now.
114             print "W: %s has released %s from the target list." % (id, all_marked_binaries[id]);
115             projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_binaries[id]));
116     projectB.query("COMMIT WORK");
117
118 def check_sources():
119     global delete_date;
120
121     print "Checking for orphaned source packages..."
122
123     # A nicer way to do this would be using `EXCEPT', but see the
124     # comment in check_binaries().
125
126     linked_sources = {};
127     q = projectB.query("SELECT source FROM binaries WHERE source is not null");
128     ql = q.getresult();
129     for i in ql:
130         linked_sources[i[0]] = "";
131     
132     all_sources = {};
133     all_sources_package = {};
134     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");
135     ql = q.getresult();
136     for i in ql:
137         all_sources[i[0]] = i[1];
138         all_sources_package[i[0]] = i[2];
139
140     projectB.query("BEGIN WORK");
141     for id in all_sources.keys():
142         if not linked_sources.has_key(id):
143             # Is this a known source-only package?
144             if in_override_p(all_sources_package[id]):
145                 continue;
146             # Then check to see if the source is still in any suites
147             untouchable = 0;
148             q = projectB.query("SELECT su.suite_name FROM src_associations sa, suite su WHERE sa.source = %s and su.id = sa.suite" % (id));
149             for i in q.getresult():
150                 if Cnf.Find("Suite::%s::Untouchable" % (i[0])):
151                     untouchable = 1;
152                 else:
153                     if not Cnf["Rhona::Options::No-Action"]:
154                         projectB.query("DELETE FROM src_associations WHERE source = %s" % (id));
155                     
156             # We can't delete binary-less source-only packages if
157             # they're in an untouchable suite (i.e. stable)...
158             if untouchable:
159                 continue;
160
161             projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, all_sources[id]))
162             # Delete all other files references by .dsc too if they're not used by anyone else
163             q = projectB.query("SELECT f.id FROM files f, dsc_files d WHERE d.source = %d AND d.file = f.id" % (id));
164             for i in q.getresult(): 
165                 q = projectB.query("SELECT id FROM dsc_files d WHERE file = %s" % (i[0]));
166                 ql = q.getresult();
167                 if len(ql) == 1:
168                     projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, i[0]));
169
170             # If the file is used by another source package
171             # (e.g. because it's an .orig.tar.gz) We need to delete
172             # this source package's reference to it from dsc_files.
173             # So just clear out all references to the source file in
174             # dsc_files now.
175             if not Cnf["Rhona::Options::No-Action"]:
176                 projectB.query("DELETE FROM dsc_files WHERE source = %s" % (id));
177                     
178     projectB.query("COMMIT WORK");
179
180     # Check for any sources which are marked for eventual deletion but are now used again.
181     all_marked_sources = {};
182     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");
183     ql = q.getresult();
184     for i in ql:
185         all_marked_sources[i[0]] = i[1];
186     projectB.query("BEGIN WORK");
187     for id in all_marked_sources.keys():
188         if linked_sources.has_key(id):
189             # Can't imagine why this would happen, so warn about it for now.
190             print "W: %s has released %s from the target list." % (id, all_marked_sources[id]);
191             projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_sources[id]));
192             # Unmark all other files references by .dsc too
193             q = projectB.query("SELECT file FROM dsc_files WHERE source = %d" % (id));
194             ql = q.getresult();
195             for i in ql:
196                     projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (i[0]));
197     projectB.query("COMMIT WORK");
198
199     # Whee, check for any source files (i.e. non .dsc) which are
200     # marked for eventual deletion but are now used by a source
201     # package again.
202     all_marked_dsc_files = {}
203     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");
204     ql = q.getresult();
205     for i in ql:
206         all_marked_dsc_files[i[0]] = i[1];
207     projectB.query("BEGIN WORK");
208     for id in all_marked_dsc_files.keys():
209         if linked_sources.has_key(id):
210             # Can't imagine why this would happen, so warn about it for now.
211             print "W: %s has released %s from the target list." % (id, all_marked_sources[id]);
212             projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (all_marked_sources[id]));
213             # Unmark all other files references by .dsc too
214             q = projectB.query("SELECT file FROM dsc_files WHERE source = %d" % (id));
215             ql = q.getresult();
216             for i in ql:
217                     projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (i[0]));
218     projectB.query("COMMIT WORK");
219
220 def check_files():
221     global delete_date;
222
223     print "Checking for unused files..."
224
225     # Check for files not references in either binaries or dsc_files
226     used = {};
227     q = projectB.query("SELECT file FROM binaries");
228     for i in q.getresult():
229         used[i[0]] = "";
230     q = projectB.query("SELECT file FROM dsc_files");
231     for i in q.getresult():
232         used[i[0]] = "";
233         
234     all = {};
235     q = projectB.query("SELECT f.id, l.path, f.filename FROM files f, location l WHERE f.location = l.id;");
236     for i in q.getresult():
237         all[i[0]] = i[1] + i[2];
238
239     projectB.query("BEGIN WORK");
240     for id in all.keys():
241         if not used.has_key(id):
242             projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, id));
243     projectB.query("COMMIT WORK");
244     
245 def clean_binaries():
246     global delete_date;
247
248     # We do this here so that the binaries we remove will have their
249     # source also removed (if possible).
250
251     print "Cleaning binaries from the DB..."
252     if not Cnf["Rhona::Options::No-Action"]:
253         before = time.time();
254         sys.stdout.write("[Deleting from binaries table... ");
255         projectB.query("DELETE FROM binaries WHERE EXISTS (SELECT id FROM files WHERE binaries.file = files.id AND files.last_used <= '%s')" % (delete_date));
256         sys.stdout.write("done. (%d)]\n" % (int(time.time()-before)));
257
258 def clean():
259     global delete_date;
260     count = 0;
261     size = 0;
262
263     print "Cleaning out packages..."
264
265     # Ensure destination directory exists
266     dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"];
267     if not os.path.exists(dest):
268         os.mkdir(dest);
269         
270     # Delete from source (dsc_file should already be done!)
271     if not Cnf["Rhona::Options::No-Action"]:
272         before = time.time();
273         sys.stdout.write("[Deleting from source table... ");
274         projectB.query("DELETE FROM source WHERE EXISTS (SELECT id FROM files WHERE source.file = files.id AND files.last_used <= '%s')" % (delete_date));
275         sys.stdout.write("done. (%d)]\n" % (int(time.time()-before)));
276         
277     # Delete files from the pool
278     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));
279     for i in q.getresult():
280         filename = i[0] + i[1];
281         if not os.path.exists(filename):
282             sys.stderr.write("E: can not find %s.\n" % (filename));
283             continue;
284         if os.path.isfile(filename):
285             if os.path.islink(filename):
286                 count = count + 1;
287                 if Cnf["Rhona::Options::No-Action"]:
288                     print "Removing symlink %s..." % (filename);
289                 else:
290                     os.unlink(filename);
291             else:
292                 size = size + os.stat(filename)[stat.ST_SIZE];
293                 count = count + 1;
294                 if Cnf["Rhona::Options::No-Action"]:
295                     print "Cleaning %s to %s..." % (filename, dest);
296                 else:
297                     utils.move(filename, dest);
298         else:
299             sys.stderr.write("%s is neither symlink nor file?!\n" % (filename));
300             sys.exit(1);
301     # delete from files
302     if not Cnf["Rhona::Options::No-Action"]:
303         before = time.time();
304         sys.stdout.write("[Deleting from files table... ");
305         projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date));
306         sys.stdout.write("done. (%d)]\n" % (int(time.time()-before)));
307     if count > 0:
308         sys.stderr.write("Cleaned %d files, %s.\n" % (count, utils.size_type(size)));
309
310 def clean_maintainers():
311     print "Cleaning out unused Maintainer entries..."
312     
313     used = {};
314     q = projectB.query("SELECT maintainer FROM binaries WHERE maintainer IS NOT NULL");
315     for i in q.getresult():
316         used[i[0]] = "";
317     q = projectB.query("SELECT maintainer FROM source WHERE maintainer IS NOT NULL");
318     for i in q.getresult():
319         used[i[0]] = "";
320
321     all = {};
322     q = projectB.query("SELECT id, name FROM maintainer");
323     for i in q.getresult():
324         all[i[0]] = i[1];
325
326     count = 0;
327     projectB.query("BEGIN WORK");
328     for id in all.keys():
329         if not used.has_key(id):
330             if not Cnf["Rhona::Options::No-Action"]:
331                 projectB.query("DELETE FROM maintainer WHERE id = %s" % (id));
332             count = count + 1;
333     projectB.query("COMMIT WORK");
334
335     if count > 0:
336         sys.stderr.write("Cleared out %d maintainer entries.\n" % (count));
337
338 def main():
339     global Cnf, projectB, delete_date;
340     
341     projectB = pg.connect('projectb', 'localhost');
342
343     apt_pkg.init();
344     
345     Cnf = apt_pkg.newConfiguration();
346     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
347
348     Arguments = [('D',"debug","Rhona::Options::Debug", "IntVal"),
349                  ('h',"help","Rhona::Options::Help"),
350                  ('n',"no-action","Rhona::Options::No-Action"),
351                  ('V',"version","Rhona::Options::Version")];
352     
353     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
354     if Cnf["Rhona::Options::Help"]:
355         usage(0);
356         
357     if Cnf["Rhona::Options::Version"]:
358         print "rhona version 0.0000000000";
359         usage(0);
360
361     delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()));
362
363     check_binaries();
364     clean_binaries();
365     check_sources();
366     check_files();
367     clean();
368     clean_maintainers();
369
370 if __name__ == '__main__':
371     main()
372