Package buildbot :: Package util :: Module misc
[frames] | no frames]

Source Code for Module buildbot.util.misc

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  """ 
17  Miscellaneous utilities; these should be imported from C{buildbot.util}, not 
18  directly from this module. 
19  """ 
20   
21  from twisted.python import log 
22  from twisted.internet import defer 
23   
24 -def deferredLocked(lock_or_attr):
25 def decorator(fn): 26 def wrapper(*args, **kwargs): 27 lock = lock_or_attr 28 if isinstance(lock, basestring): 29 lock = getattr(args[0], lock) 30 d = lock.acquire() 31 d.addCallback(lambda _ : fn(*args, **kwargs)) 32 def release(val): 33 lock.release() 34 return val
35 d.addBoth(release) 36 return d 37 return wrapper 38 return decorator 39
40 -class SerializedInvocation(object):
41 - def __init__(self, method):
42 self.method = method 43 self.running = False 44 self.pending_deferreds = []
45
46 - def __call__(self):
47 d = defer.Deferred() 48 self.pending_deferreds.append(d) 49 if not self.running: 50 self.start() 51 return d
52
53 - def start(self):
54 self.running = True 55 invocation_deferreds = self.pending_deferreds 56 self.pending_deferreds = [] 57 d = self.method() 58 d.addErrback(log.err, 'in invocation of %r' % (self.method,)) 59 60 def notify_callers(_): 61 for d in invocation_deferreds: 62 d.callback(None)
63 d.addCallback(notify_callers) 64 65 def next(_): 66 self.running = False 67 if self.pending_deferreds: 68 self.start() 69 else: 70 self._quiet()
71 d.addBoth(next) 72
73 - def _quiet(self): # hook for tests
74 pass 75