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