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