]> git.decadent.org.uk Git - dak.git/blob - dak/clean_queues.py
Stop using silly names, and migrate to a saner directory structure.
[dak.git] / dak / clean_queues.py
1 #!/usr/bin/env python
2
3 # Clean incoming of old unused files
4 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: shania,v 1.18 2005-03-06 21:51:51 rmurray 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 # <aj> Bdale, a ham-er, and the leader,
24 # <aj> Willy, a GCC maintainer,
25 # <aj> Lamont-work, 'cause he's the top uploader....
26 # <aj>         Penguin Puff' save the day!
27 # <aj> Porting code, trying to build the world,
28 # <aj> Here they come just in time...
29 # <aj>         The Penguin Puff' Guys!
30 # <aj> [repeat]
31 # <aj> Penguin Puff'!
32 # <aj> willy: btw, if you don't maintain gcc you need to start, since
33 #      the lyrics fit really well that way
34
35 ################################################################################
36
37 import os, stat, sys, time;
38 import utils;
39 import apt_pkg;
40
41 ################################################################################
42
43 Cnf = None;
44 Options = None;
45 del_dir = None;
46 delete_date = None;
47
48 ################################################################################
49
50 def usage (exit_code=0):
51     print """Usage: shania [OPTIONS]
52 Clean out incoming directories.
53
54   -d, --days=DAYS            remove anything older than DAYS old
55   -i, --incoming=INCOMING    the incoming directory to clean
56   -n, --no-action            don't do anything
57   -v, --verbose              explain what is being done
58   -h, --help                 show this help and exit"""
59
60     sys.exit(exit_code)
61
62 ################################################################################
63
64 def init ():
65     global delete_date, del_dir;
66
67     delete_date = int(time.time())-(int(Options["Days"])*84600);
68
69     # Ensure a directory exists to remove files to
70     if not Options["No-Action"]:
71         date = time.strftime("%Y-%m-%d");
72         del_dir = Cnf["Dir::Morgue"] + '/' + Cnf["Shania::MorgueSubDir"] + '/' + date;
73         if not os.path.exists(del_dir):
74             os.makedirs(del_dir, 02775);
75         if not os.path.isdir(del_dir):
76             utils.fubar("%s must be a directory." % (del_dir));
77
78     # Move to the directory to clean
79     incoming = Options["Incoming"];
80     if incoming == "":
81         incoming = Cnf["Dir::Queue::Unchecked"];
82     os.chdir(incoming);
83
84 # Remove a file to the morgue
85 def remove (file):
86     if os.access(file, os.R_OK):
87         dest_filename = del_dir + '/' + os.path.basename(file);
88         # If the destination file exists; try to find another filename to use
89         if os.path.exists(dest_filename):
90             dest_filename = utils.find_next_free(dest_filename, 10);
91         utils.move(file, dest_filename, 0660);
92     else:
93         utils.warn("skipping '%s', permission denied." % (os.path.basename(file)));
94
95 # Removes any old files.
96 # [Used for Incoming/REJECT]
97 #
98 def flush_old ():
99     for file in os.listdir('.'):
100         if os.path.isfile(file):
101             if os.stat(file)[stat.ST_MTIME] < delete_date:
102                 if Options["No-Action"]:
103                     print "I: Would delete '%s'." % (os.path.basename(file));
104                 else:
105                     if Options["Verbose"]:
106                         print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
107                     remove(file);
108             else:
109                 if Options["Verbose"]:
110                     print "Skipping, too new, '%s'." % (os.path.basename(file));
111
112 # Removes any files which are old orphans (not associated with a valid .changes file).
113 # [Used for Incoming]
114 #
115 def flush_orphans ():
116     all_files = {};
117     changes_files = [];
118
119     # Build up the list of all files in the directory
120     for i in os.listdir('.'):
121         if os.path.isfile(i):
122             all_files[i] = 1;
123             if i.endswith(".changes"):
124                 changes_files.append(i);
125
126     # Proces all .changes and .dsc files.
127     for changes_filename in changes_files:
128         try:
129             changes = utils.parse_changes(changes_filename);
130             files = utils.build_file_list(changes);
131         except:
132             utils.warn("error processing '%s'; skipping it. [Got %s]" % (changes_filename, sys.exc_type));
133             continue;
134
135         dsc_files = {};
136         for file in files.keys():
137             if file.endswith(".dsc"):
138                 try:
139                     dsc = utils.parse_changes(file);
140                     dsc_files = utils.build_file_list(dsc, is_a_dsc=1);
141                 except:
142                     utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
143                     continue;
144
145         # Ensure all the files we've seen aren't deleted
146         keys = [];
147         for i in (files.keys(), dsc_files.keys(), [changes_filename]):
148             keys.extend(i);
149         for key in keys:
150             if all_files.has_key(key):
151                 if Options["Verbose"]:
152                     print "Skipping, has parents, '%s'." % (key);
153                 del all_files[key];
154
155     # Anthing left at this stage is not referenced by a .changes (or
156     # a .dsc) and should be deleted if old enough.
157     for file in all_files.keys():
158         if os.stat(file)[stat.ST_MTIME] < delete_date:
159             if Options["No-Action"]:
160                 print "I: Would delete '%s'." % (os.path.basename(file));
161             else:
162                 if Options["Verbose"]:
163                     print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
164                 remove(file);
165         else:
166             if Options["Verbose"]:
167                 print "Skipping, too new, '%s'." % (os.path.basename(file));
168
169 ################################################################################
170
171 def main ():
172     global Cnf, Options;
173
174     Cnf = utils.get_conf()
175
176     for i in ["Help", "Incoming", "No-Action", "Verbose" ]:
177         if not Cnf.has_key("Shania::Options::%s" % (i)):
178             Cnf["Shania::Options::%s" % (i)] = "";
179     if not Cnf.has_key("Shania::Options::Days"):
180         Cnf["Shania::Options::Days"] = "14";
181
182     Arguments = [('h',"help","Shania::Options::Help"),
183                  ('d',"days","Shania::Options::Days", "IntLevel"),
184                  ('i',"incoming","Shania::Options::Incoming", "HasArg"),
185                  ('n',"no-action","Shania::Options::No-Action"),
186                  ('v',"verbose","Shania::Options::Verbose")];
187
188     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
189     Options = Cnf.SubTree("Shania::Options")
190
191     if Options["Help"]:
192         usage();
193
194     init();
195
196     if Options["Verbose"]:
197         print "Processing incoming..."
198     flush_orphans();
199
200     reject = Cnf["Dir::Queue::Reject"]
201     if os.path.exists(reject) and os.path.isdir(reject):
202         if Options["Verbose"]:
203             print "Processing incoming/REJECT..."
204         os.chdir(reject);
205         flush_old();
206
207 #######################################################################################
208
209 if __name__ == '__main__':
210     main();