]> git.decadent.org.uk Git - dak.git/blob - shania
sync
[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.5 2001-06-23 00:25:52 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 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 init ():
53     global delete_date, del_dir;
54     
55     delete_date = int(time.time())-(int(Options["Days"])*84600);
56
57     # Ensure a directory exists to remove files to
58     if not Options["No-Action"]:
59         date = time.strftime("%Y-%m-%d", time.localtime(time.time()));
60         del_dir = Cnf["Dir::Morgue"] + '/' + Cnf["Shania::MorgueSubDir"] + '/' + date;
61         if not os.path.exists(del_dir):
62             os.makedirs(del_dir, 02775);
63         if not os.path.isdir(del_dir):
64             utils.fubar("%s must be a directory." % (del_dir));
65
66     # Move to the directory to clean
67     incoming = Options["Incoming"];
68     if incoming == "":
69         incoming = Cnf["Dir::IncomingDir"];
70     os.chdir(incoming);
71
72 # Remove a file to the morgue
73 def remove (file):
74     if os.access(file, os.R_OK):
75         dest_filename = del_dir + '/' + os.path.basename(file);
76         # If the destination file exists; try to find another filename to use
77         if os.path.exists(dest_filename):
78             dest_filename = utils.find_next_free(dest_filename, 10);
79         utils.move(file, dest_filename);
80     else:
81         utils.warn("skipping '%s', permission denied." % (os.path.basename(file)));
82         
83 # Removes any old files.
84 # [Used for Incoming/REJECT]
85 #
86 def flush_old ():
87     for file in os.listdir('.'):
88         if os.path.isfile(file):
89             if os.stat(file)[stat.ST_MTIME] < delete_date:
90                 if Options["No-Action"]:
91                     print "I: Would delete '%s'." % (os.path.basename(file));
92                 else:
93                     if Options["Verbose"]:
94                         print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
95                     remove(file);
96             else:
97                 if Options["Verbose"]:
98                     print "Skipping, too new, '%s'." % (os.path.basename(file));
99
100 # Removes any files which are old orphans (not associated with a valid .changes file).
101 # [Used for Incoming]
102 #
103 def flush_orphans ():
104     all_files = {};
105     changes_files = [];
106     
107     # Build up the list of all files in the directory
108     for i in os.listdir('.'):
109         if os.path.isfile(i):
110             all_files[i] = 1;
111             if i[-8:] == ".changes":
112                 changes_files.append(i);
113
114     # Proces all .changes and .dsc files.
115     for changes_filename in changes_files:
116         try:
117             changes = utils.parse_changes(changes_filename, 0)
118             files = utils.build_file_list(changes, "");
119         except:
120             utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
121             continue;
122
123         dsc_files = {};
124         for file in files.keys():
125             if file[-4:] == ".dsc":
126                 try:
127                     dsc = utils.parse_changes(file, 0)
128                     dsc_files = utils.build_file_list(dsc, 1)
129                 except:
130                     utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
131                     continue;
132
133         # Ensure all the files we've seen aren't deleted
134         keys = [];
135         for i in (files.keys(), dsc_files.keys(), [changes_filename]):
136             keys.extend(i);
137         for key in keys:
138             if all_files.has_key(key):
139                 if Options["Verbose"]:
140                     print "Skipping, has parents, '%s'." % (key);
141                 del all_files[key];
142                     
143     # Anthing left at this stage is not referenced by a .changes (or
144     # a .dsc) and should be deleted if old enough.
145     for file in all_files.keys():
146         if os.stat(file)[stat.ST_MTIME] < delete_date:
147             if Options["No-Action"]:
148                 print "I: Would delete '%s'." % (os.path.basename(file));
149             else:
150                 if Options["Verbose"]:
151                     print "Removing '%s' (to '%s')."  % (os.path.basename(file), del_dir);
152                 remove(file);
153         else:
154             if Options["Verbose"]:
155                 print "Skipping, too new, '%s'." % (os.path.basename(file));
156     
157 def main ():
158     global Cnf, Options;
159     
160     apt_pkg.init();
161     
162     Cnf = apt_pkg.newConfiguration();
163     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
164
165     Arguments = [('D',"debug","Shania::Options::Debug", "IntVal"),
166                  ('h',"help","Shania::Options::Help"),
167                  ('V',"version","Shania::Options::Version"),
168                  ('d',"days","Shania::Options::Days", "IntVal"),
169                  ('i',"incoming","Shania::Options::Incoming", "HasArg"),
170                  ('n',"no-action","Shania::Options::No-Action"),
171                  ('v',"verbose","Shania::Options::Verbose")];
172
173     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
174     Options = Cnf.SubTree("Shania::Options")
175
176     init ();
177
178     if Options["Verbose"]:
179         print "Processing incoming..."
180     flush_orphans();
181
182     if os.path.exists("REJECT") and os.path.isdir("REJECT"):
183         if Options["Verbose"]:
184             print "Processing incoming/REJECT..."
185         os.chdir("REJECT");
186         flush_old();
187
188 #######################################################################################
189
190 if __name__ == '__main__':
191     main()