1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 __doc__='''FileCleanup
16
17 Walk a tree recursively and cleanup files. Slowly.
18
19 $Id$
20 '''
21
22 from twisted.internet import reactor
23
24 import os
25 import time
26 import re
27
28 __version__ = "$Revision$"[11:-2]
29
30 THIRTY_DAYS = 30 * 24 * 60 * 60
31
33
36 """Recursively delete all files under [root] matching
37 [pattern] older than [tooOld], processing only [maxProcess]
38 files every [frequency] seconds."""
39 self.tooOld = tooOld
40 self.pattern = re.compile(pattern)
41 self.root = root
42 self.frequency = frequency
43 self.maxProcess = maxProcess
44 self.iter = None
45
46
48 "Begin processing the directory, recursively"
49 reactor.callLater(0, self.run)
50
51
65
66
68 "Hook for cleanin up the file"
69 os.unlink(fullPath)
70
71
72 - def test(self, fullPath):
73 "Test file file for pattern, age and type"
74 if not os.path.isfile(fullPath):
75 return False
76 if not self.pattern.match(fullPath):
77 return False
78 mtime = os.path.getmtime(fullPath)
79 if mtime > self.now - self.tooOld:
80 return False
81 return True
82
83
84 - def walk(self, root):
85 "Generate the list of all files"
86 for f in os.listdir(root):
87 fullPath = os.path.join(root, f)
88 yield fullPath
89 if os.path.exists(fullPath) and os.path.isdir(fullPath):
90 for k in self.walk(fullPath):
91 yield k
92
93 if __name__ == '__main__':
94 import sys
95 f = FileCleanup('/tmp', 604800, '.*\\.xpi$', 10, 1)
96 f.process = lambda x: sys.stdout.write('%s\n' % x)
97 f.start()
98 reactor.run()
99