]> git.decadent.org.uk Git - dak.git/blob - rhona
rewritten
[dak.git] / rhona
1 #!/usr/bin/env python
2
3 # rhona, cleans up unassociated binary and source packages
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: rhona,v 1.9 2001-03-14 20:31:56 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 # FIXME: why can't we make (sane speed) UPDATEs out of these SELECTs?
56
57 def check_binaries():
58     global delete_date;
59     
60     print "Checking for orphaned binary packages..."
61
62     # Get the list of binary packages not in a suite and mark them for
63     # deletion.
64
65     q = projectB.query("""
66 SELECT b.file FROM binaries b WHERE NOT EXISTS
67         (SELECT ba.bin FROM bin_associations ba WHERE ba.bin = b.id)""");
68     ql = q.getresult();
69
70     projectB.query("BEGIN WORK");
71     for i in ql:
72         file_id = i[0];
73         projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, file_id))
74     projectB.query("COMMIT WORK");
75
76     # Check for any binaries which are marked for eventual deletion
77     # but are now used again.
78
79     q = projectB.query("""
80 SELECT b.file FROM binaries b, files f
81    WHERE f.last_used IS NOT NULL AND f.id = b.file AND
82       EXISTS (SELECT suite FROM bin_associations ba WHERE ba.bin = b.id)""");
83     ql = q.getresult();
84     projectB.query("BEGIN WORK");
85     for i in ql:
86         file_id = i[0];
87         projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id));
88     projectB.query("COMMIT WORK");
89
90 def check_sources():
91     global delete_date;
92
93     print "Checking for orphaned source packages..."
94
95     # Get the list of source packages not in a suite and not linked to
96     # by any binary packages.
97
98     q = projectB.query("""
99 SELECT s.id, s.file FROM source s
100   WHERE NOT EXISTS
101    (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id)
102   AND NOT EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)""");
103     ql = q.getresult();
104
105     projectB.query("BEGIN WORK");
106     for i in ql:
107         source_id = i[0];
108         dsc_file_id = i[1];
109
110         # Mark the .dsc file for deletion
111         projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, dsc_file_id))
112         # Mark all other files references by .dsc too if they're not used by anyone else
113         x = projectB.query("SELECT f.id FROM files f, dsc_files d WHERE d.source = %s AND d.file = f.id" % (source_id));
114         for j in x.getresult():
115             file_id = i[0];
116             y = projectB.query("SELECT id FROM dsc_files d WHERE file = %s" % (file_id));
117             if len(y.getresult()) == 1:
118                 projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, file_id));
119     projectB.query("COMMIT WORK");
120
121     # Check for any sources which are marked for deletion but which
122     # are now used again.
123
124     q = projectB.query("""
125 SELECT f.id FROM source s, files f, dsc_files df
126   WHERE f.last_used IS NOT NULL AND s.id = df.source AND df.file = f.id
127     AND ((EXISTS (SELECT sa.suite FROM src_associations sa WHERE sa.source = s.id))
128       OR (EXISTS (SELECT b.id FROM binaries b WHERE b.source = s.id)))""");
129     ql = q.getresult();
130     # Could be done in SQL; but left this way for hysterical raisins
131     # [and freedom to innovate don'cha know?]
132     projectB.query("BEGIN WORK");
133     for i in ql:
134         file_id = i[0];
135         projectB.query("UPDATE files SET last_used = NULL WHERE id = %s" % (file_id));
136     projectB.query("COMMIT WORK");
137
138 def check_files():
139     global delete_date;
140
141     # FIXME: this is evil; nothing should ever be in this state.  if
142     # they are, it's a bug and the files should not be auto-deleted.
143
144     return;
145
146     print "Checking for unused files..."
147
148     # Check for files not references in either binaries or dsc_files
149     used = {};
150     q = projectB.query("SELECT file FROM binaries");
151     for i in q.getresult():
152         used[i[0]] = "";
153     q = projectB.query("SELECT file FROM dsc_files");
154     for i in q.getresult():
155         used[i[0]] = "";
156         
157     all = {};
158     q = projectB.query("SELECT f.id, l.path, f.filename FROM files f, location l WHERE f.location = l.id;");
159     for i in q.getresult():
160         all[i[0]] = i[1] + i[2];
161
162     projectB.query("BEGIN WORK");
163     for id in all.keys():
164         if not used.has_key(id):
165             projectB.query("UPDATE files SET last_used = '%s' WHERE id = %s" % (delete_date, id));
166     projectB.query("COMMIT WORK");
167     
168 def clean_binaries():
169     global delete_date;
170
171     # We do this here so that the binaries we remove will have their
172     # source also removed (if possible).
173
174     print "Cleaning binaries from the DB..."
175     if not Cnf["Rhona::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)]\n" % (int(time.time()-before)));
180
181 def clean():
182     global delete_date;
183     count = 0;
184     size = 0;
185
186     print "Cleaning out packages..."
187
188     # Ensure destination directory exists
189     dest = Cnf["Dir::Morgue"] + '/' + Cnf["Rhona::MorgueSubDir"];
190     if not os.path.exists(dest):
191         os.mkdir(dest);
192         
193     # Delete from source
194     if not Cnf["Rhona::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)]\n" % (int(time.time()-before)));
200         
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             sys.stderr.write("E: can not find %s.\n" % (filename));
207             continue;
208         if os.path.isfile(filename):
209             if os.path.islink(filename):
210                 count = count + 1;
211                 if Cnf["Rhona::Options::No-Action"]:
212                     print "Removing symlink %s..." % (filename);
213                 else:
214                     os.unlink(filename);
215             else:
216                 size = size + os.stat(filename)[stat.ST_SIZE];
217                 count = count + 1;
218                 if Cnf["Rhona::Options::No-Action"]:
219                     print "Cleaning %s to %s..." % (filename, dest);
220                 else:
221                     utils.move(filename, dest);
222         else:
223             sys.stderr.write("%s is neither symlink nor file?!\n" % (filename));
224             sys.exit(1);
225     # delete from files
226     if not Cnf["Rhona::Options::No-Action"]:
227         before = time.time();
228         sys.stdout.write("[Deleting from files table... ");
229         projectB.query("DELETE FROM files WHERE last_used <= '%s'" % (delete_date));
230         sys.stdout.write("done. (%d)]\n" % (int(time.time()-before)));
231     if count > 0:
232         sys.stderr.write("Cleaned %d files, %s.\n" % (count, utils.size_type(size)));
233
234 def clean_maintainers():
235     print "Cleaning out unused Maintainer entries..."
236     
237     used = {};
238     q = projectB.query("SELECT maintainer FROM binaries WHERE maintainer IS NOT NULL");
239     for i in q.getresult():
240         used[i[0]] = "";
241     q = projectB.query("SELECT maintainer FROM source WHERE maintainer IS NOT NULL");
242     for i in q.getresult():
243         used[i[0]] = "";
244
245     all = {};
246     q = projectB.query("SELECT id, name FROM maintainer");
247     for i in q.getresult():
248         all[i[0]] = i[1];
249
250     count = 0;
251     projectB.query("BEGIN WORK");
252     for id in all.keys():
253         if not used.has_key(id):
254             if not Cnf["Rhona::Options::No-Action"]:
255                 projectB.query("DELETE FROM maintainer WHERE id = %s" % (id));
256             count = count + 1;
257     projectB.query("COMMIT WORK");
258
259     if count > 0:
260         sys.stderr.write("Cleared out %d maintainer entries.\n" % (count));
261
262 def main():
263     global Cnf, projectB, delete_date;
264     
265     projectB = pg.connect('projectb', 'localhost');
266
267     apt_pkg.init();
268     
269     Cnf = apt_pkg.newConfiguration();
270     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
271
272     Arguments = [('D',"debug","Rhona::Options::Debug", "IntVal"),
273                  ('h',"help","Rhona::Options::Help"),
274                  ('n',"no-action","Rhona::Options::No-Action"),
275                  ('V',"version","Rhona::Options::Version")];
276     
277     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
278     Options = Cnf.SubTree("Rhona::Options")
279
280     if Options["Help"]:
281         usage(0);
282         
283     if Options["Version"]:
284         print "rhona version 0.0000000000";
285         usage(0);
286
287     override_filename = Cnf["Dir::OverrideDir"] + Cnf["Rhona::OverrideFilename"];
288     if not os.access(override_filename, os.R_OK):
289         sys.stderr.write("W: Could not find source-only override file '%s'.\n" % (override_filename));
290
291     delete_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()-int(Cnf["Rhona::StayOfExecution"])));
292
293     check_binaries();
294     clean_binaries();
295     check_sources();
296     #check_files();
297     clean();
298     clean_maintainers();
299
300 if __name__ == '__main__':
301     main()
302