3 # Clean incoming of old unused files
4 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: shania,v 1.17 2002-10-16 02:47:32 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 # <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!
32 # <aj> willy: btw, if you don't maintain gcc you need to start, since
33 # the lyrics fit really well that way
35 ################################################################################
37 import os, stat, sys, time;
41 ################################################################################
48 ################################################################################
50 def usage (exit_code=0):
51 print """Usage: shania [OPTIONS]
52 Clean out incoming directories.
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"""
62 ################################################################################
65 global delete_date, del_dir;
67 delete_date = int(time.time())-(int(Options["Days"])*84600);
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));
78 # Move to the directory to clean
79 incoming = Options["Incoming"];
81 incoming = Cnf["Dir::Queue::Unchecked"];
84 # Remove a file to the morgue
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);
93 utils.warn("skipping '%s', permission denied." % (os.path.basename(file)));
95 # Removes any old files.
96 # [Used for Incoming/REJECT]
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));
105 if Options["Verbose"]:
106 print "Removing '%s' (to '%s')." % (os.path.basename(file), del_dir);
109 if Options["Verbose"]:
110 print "Skipping, too new, '%s'." % (os.path.basename(file));
112 # Removes any files which are old orphans (not associated with a valid .changes file).
113 # [Used for Incoming]
115 def flush_orphans ():
119 # Build up the list of all files in the directory
120 for i in os.listdir('.'):
121 if os.path.isfile(i):
123 if i.endswith(".changes"):
124 changes_files.append(i);
126 # Proces all .changes and .dsc files.
127 for changes_filename in changes_files:
129 changes = utils.parse_changes(changes_filename);
130 files = utils.build_file_list(changes);
132 utils.warn("error processing '%s'; skipping it. [Got %s]" % (changes_filename, sys.exc_type));
136 for file in files.keys():
137 if file.endswith(".dsc"):
139 dsc = utils.parse_changes(file);
140 dsc_files = utils.build_file_list(dsc, is_a_dsc=1);
142 utils.warn("error processing '%s'; skipping it. [Got %s]" % (file, sys.exc_type));
145 # Ensure all the files we've seen aren't deleted
147 for i in (files.keys(), dsc_files.keys(), [changes_filename]):
150 if all_files.has_key(key):
151 if Options["Verbose"]:
152 print "Skipping, has parents, '%s'." % (key);
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));
162 if Options["Verbose"]:
163 print "Removing '%s' (to '%s')." % (os.path.basename(file), del_dir);
166 if Options["Verbose"]:
167 print "Skipping, too new, '%s'." % (os.path.basename(file));
169 ################################################################################
174 Cnf = utils.get_conf()
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";
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")];
188 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
189 Options = Cnf.SubTree("Shania::Options")
196 if Options["Verbose"]:
197 print "Processing incoming..."
200 if os.path.exists("REJECT") and os.path.isdir("REJECT"):
201 if Options["Verbose"]:
202 print "Processing incoming/REJECT..."
206 #######################################################################################
208 if __name__ == '__main__':