]> git.decadent.org.uk Git - dak.git/blob - shania
069df37d076829f35985650efe1d2e97d91b00f8
[dak.git] / shania
1 #!/usr/bin/env python
2
3 # Clean incoming of old unused files
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: shania,v 1.10 2001-11-19 20:42:40 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 import os, re, stat, string, sys, time, traceback
24 import utils
25 import apt_pkg;
26
27 ################################################################################
28
29 # 23:12|<aj> I will not hush!
30 # 23:12|<elmo> :>
31 # 23:12|<aj> Where there is injustice in the world, I shall be there!
32 # 23:13|<aj> I shall not be silenced!
33 # 23:13|<aj> The world shall know!
34 # 23:13|<aj> The world *must* know!
35 # 23:13|<elmo> oh dear, he's gone back to powerpuff girls... ;-)
36 # 23:13|<aj> yay powerpuff girls!!
37 # 23:13|<aj> buttercup's my favourite, who's yours?
38 # 23:14|<aj> you're backing away from the keyboard right now aren't you?
39 # 23:14|<aj> *AREN'T YOU*?!
40 # 23:15|<aj> I will not be treated like this.
41 # 23:15|<aj> I shall have my revenge.
42 # 23:15|<aj> I SHALL!!!
43
44 ################################################################################
45
46 Cnf = None;
47 Options = None;
48 del_dir = None;
49
50 ################################################################################
51
52 def usage (exit_code=0):
53     print """Usage: shania [OPTIONS]
54 Clean out incoming directories.
55
56   -d, --days=DAYS            remove anything older than DAYS old
57   -i, --incoming=INCOMING    the incoming directory to clean
58   -n, --no-action            don't do anything
59   -v, --verbose              explain what is being done
60   -h, --help                 show this help and exit"""
61
62     sys.exit(exit_code)
63
64 ################################################################################
65
66 def init ():
67     global delete_date, del_dir;
68
69     delete_date = int(time.time())-(int(Options["Days"])*84600);
70
71     # Ensure a directory exists to remove files to
72     if not Options["No-Action"]:
73         date = time.strftime("%Y-%m-%d", time.localtime(time.time()));
74         del_dir = Cnf["Dir::Morgue"] + '/' + Cnf["Shania::MorgueSubDir"] + '/' + date;
75         if not os.path.exists(del_dir):
76             os.makedirs(del_dir, 02775);
77         if not os.path.isdir(del_dir):
78             utils.fubar("%s must be a directory." % (del_dir));
79
80     # Move to the directory to clean
81     incoming = Options["Incoming"];
82     if incoming == "":
83         incoming = Cnf["Dir::IncomingDir"];
84     os.chdir(incoming);
85
86 # Remove a file to the morgue
87 def remove (file):
88     if os.access(file, os.R_OK):
89         dest_filename = del_dir + '/' + os.path.basename(file);
90         # If the destination file exists; try to find another filename to use
91         if os.path.exists(dest_filename):
92             dest_filename = utils.find_next_free(dest_filename, 10);
93         utils.move(file, dest_filename);
94     else:
95         utils.warn("skipping '%s', permission denied." % (os.path.basename(file)));
96
97 # Removes any old files.
98 # [Used for Incoming/REJECT]
99 #
100 def flush_old ():
101     for file in os.listdir('.'):
102         if os.path.isfile(file):
103             if os.stat(file)[stat.ST_MTIME] < delete_date:
104                 if Options["No-Action"]:
105                     print "I: Would delete '%s'." % (os.path.basename(file));
106                 else:
107                     if Options["Verbose"]:
108                         print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
109                     remove(file);
110             else:
111                 if Options["Verbose"]:
112                     print "Skipping, too new, '%s'." % (os.path.basename(file));
113
114 # Removes any files which are old orphans (not associated with a valid .changes file).
115 # [Used for Incoming]
116 #
117 def flush_orphans ():
118     all_files = {};
119     changes_files = [];
120
121     # Build up the list of all files in the directory
122     for i in os.listdir('.'):
123         if os.path.isfile(i):
124             all_files[i] = 1;
125             if i[-8:] == ".changes":
126                 changes_files.append(i);
127
128     # Proces all .changes and .dsc files.
129     for changes_filename in changes_files:
130         try:
131             changes = utils.parse_changes(changes_filename, 0)
132             files = utils.build_file_list(changes, "");
133         except:
134             utils.warn("error processing '%s'; skipping it. [Got %s]" % (changes_filename, sys.exc_type));
135             continue;
136
137         dsc_files = {};
138         for file in files.keys():
139             if file[-4:] == ".dsc":
140                 try:
141                     dsc = utils.parse_changes(file, 0)
142                     dsc_files = utils.build_file_list(dsc, 1)
143                 except:
144                     utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
145                     continue;
146
147         # Ensure all the files we've seen aren't deleted
148         keys = [];
149         for i in (files.keys(), dsc_files.keys(), [changes_filename]):
150             keys.extend(i);
151         for key in keys:
152             if all_files.has_key(key):
153                 if Options["Verbose"]:
154                     print "Skipping, has parents, '%s'." % (key);
155                 del all_files[key];
156
157     # Anthing left at this stage is not referenced by a .changes (or
158     # a .dsc) and should be deleted if old enough.
159     for file in all_files.keys():
160         if os.stat(file)[stat.ST_MTIME] < delete_date:
161             if Options["No-Action"]:
162                 print "I: Would delete '%s'." % (os.path.basename(file));
163             else:
164                 if Options["Verbose"]:
165                     print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
166                 remove(file);
167         else:
168             if Options["Verbose"]:
169                 print "Skipping, too new, '%s'." % (os.path.basename(file));
170
171 ################################################################################
172
173 def main ():
174     global Cnf, Options;
175
176     Cnf = utils.get_conf()
177
178     for i in ["Help", "Incoming", "No-Action", "Verbose" ]:
179         if not Cnf.has_key("Shania::Options::%s" % (i)):
180             Cnf["Shania::Options::%s" % (i)] = "";
181     if not Cnf.has_key("Shania::Options::Days"):
182         Cnf["Shania::Options::Days"] = "14";
183
184     Arguments = [('h',"help","Shania::Options::Help"),
185                  ('d',"days","Shania::Options::Days", "IntVal"),
186                  ('i',"incoming","Shania::Options::Incoming", "HasArg"),
187                  ('n',"no-action","Shania::Options::No-Action"),
188                  ('v',"verbose","Shania::Options::Verbose")];
189
190     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
191     Options = Cnf.SubTree("Shania::Options")
192
193     if Options["Help"]:
194         usage();
195
196     init ();
197
198     if Options["Verbose"]:
199         print "Processing incoming..."
200     flush_orphans();
201
202     if os.path.exists("REJECT") and os.path.isdir("REJECT"):
203         if Options["Verbose"]:
204             print "Processing incoming/REJECT..."
205         os.chdir("REJECT");
206         flush_old();
207
208 #######################################################################################
209
210 if __name__ == '__main__':
211     main()