3 # Clean incoming of old unused files
4 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: shania,v 1.14 2002-05-08 11:13:02 troup Exp $
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.
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.
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
21 ################################################################################
23 import os, stat, sys, time;
27 ################################################################################
29 # 23:12|<aj> I will not hush!
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!!!
44 ################################################################################
51 ################################################################################
53 def usage (exit_code=0):
54 print """Usage: shania [OPTIONS]
55 Clean out incoming directories.
57 -d, --days=DAYS remove anything older than DAYS old
58 -i, --incoming=INCOMING the incoming directory to clean
59 -n, --no-action don't do anything
60 -v, --verbose explain what is being done
61 -h, --help show this help and exit"""
65 ################################################################################
68 global delete_date, del_dir;
70 delete_date = int(time.time())-(int(Options["Days"])*84600);
72 # Ensure a directory exists to remove files to
73 if not Options["No-Action"]:
74 date = time.strftime("%Y-%m-%d", time.localtime(time.time()));
75 del_dir = Cnf["Dir::Morgue"] + '/' + Cnf["Shania::MorgueSubDir"] + '/' + date;
76 if not os.path.exists(del_dir):
77 os.makedirs(del_dir, 02775);
78 if not os.path.isdir(del_dir):
79 utils.fubar("%s must be a directory." % (del_dir));
81 # Move to the directory to clean
82 incoming = Options["Incoming"];
84 incoming = Cnf["Dir::Queue::Unchecked"];
87 # Remove a file to the morgue
89 if os.access(file, os.R_OK):
90 dest_filename = del_dir + '/' + os.path.basename(file);
91 # If the destination file exists; try to find another filename to use
92 if os.path.exists(dest_filename):
93 dest_filename = utils.find_next_free(dest_filename, 10);
94 utils.move(file, dest_filename, 0660);
96 utils.warn("skipping '%s', permission denied." % (os.path.basename(file)));
98 # Removes any old files.
99 # [Used for Incoming/REJECT]
102 for file in os.listdir('.'):
103 if os.path.isfile(file):
104 if os.stat(file)[stat.ST_MTIME] < delete_date:
105 if Options["No-Action"]:
106 print "I: Would delete '%s'." % (os.path.basename(file));
108 if Options["Verbose"]:
109 print "Removing '%s' (to '%s')." % (os.path.basename(file), del_dir);
112 if Options["Verbose"]:
113 print "Skipping, too new, '%s'." % (os.path.basename(file));
115 # Removes any files which are old orphans (not associated with a valid .changes file).
116 # [Used for Incoming]
118 def flush_orphans ():
122 # Build up the list of all files in the directory
123 for i in os.listdir('.'):
124 if os.path.isfile(i):
126 if i[-8:] == ".changes":
127 changes_files.append(i);
129 # Proces all .changes and .dsc files.
130 for changes_filename in changes_files:
132 changes = utils.parse_changes(changes_filename, 0)
133 files = utils.build_file_list(changes, "");
135 utils.warn("error processing '%s'; skipping it. [Got %s]" % (changes_filename, sys.exc_type));
139 for file in files.keys():
140 if file[-4:] == ".dsc":
142 dsc = utils.parse_changes(file, 0)
143 dsc_files = utils.build_file_list(dsc, 1)
145 utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
148 # Ensure all the files we've seen aren't deleted
150 for i in (files.keys(), dsc_files.keys(), [changes_filename]):
153 if all_files.has_key(key):
154 if Options["Verbose"]:
155 print "Skipping, has parents, '%s'." % (key);
158 # Anthing left at this stage is not referenced by a .changes (or
159 # a .dsc) and should be deleted if old enough.
160 for file in all_files.keys():
161 if os.stat(file)[stat.ST_MTIME] < delete_date:
162 if Options["No-Action"]:
163 print "I: Would delete '%s'." % (os.path.basename(file));
165 if Options["Verbose"]:
166 print "Removing '%s' (to '%s')." % (os.path.basename(file), del_dir);
169 if Options["Verbose"]:
170 print "Skipping, too new, '%s'." % (os.path.basename(file));
172 ################################################################################
177 Cnf = utils.get_conf()
179 for i in ["Help", "Incoming", "No-Action", "Verbose" ]:
180 if not Cnf.has_key("Shania::Options::%s" % (i)):
181 Cnf["Shania::Options::%s" % (i)] = "";
182 if not Cnf.has_key("Shania::Options::Days"):
183 Cnf["Shania::Options::Days"] = "14";
185 Arguments = [('h',"help","Shania::Options::Help"),
186 ('d',"days","Shania::Options::Days", "IntLevel"),
187 ('i',"incoming","Shania::Options::Incoming", "HasArg"),
188 ('n',"no-action","Shania::Options::No-Action"),
189 ('v',"verbose","Shania::Options::Verbose")];
191 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
192 Options = Cnf.SubTree("Shania::Options")
199 if Options["Verbose"]:
200 print "Processing incoming..."
203 if os.path.exists("REJECT") and os.path.isdir("REJECT"):
204 if Options["Verbose"]:
205 print "Processing incoming/REJECT..."
209 #######################################################################################
211 if __name__ == '__main__':