Package ZenRRD :: Module FileCleanup
[hide private]
[frames] | no frames]

Source Code for Module ZenRRD.FileCleanup

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  #! /usr/bin/env python  
 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   
32 -class FileCleanup:
33
34 - def __init__(self, root, pattern, 35 tooOld = THIRTY_DAYS, maxProcess = 50, frequency = 60):
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
47 - def start(self):
48 "Begin processing the directory, recursively" 49 reactor.callLater(0, self.run)
50 51
52 - def run(self):
53 "Start a run at the directory" 54 if self.iter is None: 55 self.now = time.time() 56 self.iter = self.walk(self.root) 57 for i, f in enumerate(self.iter): 58 if i > self.maxProcess: 59 break 60 if self.test(f): 61 self.process(f) 62 else: 63 self.iter = None 64 reactor.callLater(self.frequency, self.run)
65 66
67 - def process(self, fullPath):
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