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