WebStatus: yes create public_html/ at startup, otherwise we get internal server error...
[buildbot.git] / buildbot / util.py
blob84b965d1240654a2f3f4140fadc79631961c366b
1 # -*- test-case-name: buildbot.test.test_util -*-
3 from twisted.internet.defer import Deferred
4 from twisted.spread import pb
5 import time
7 def now():
8 #return int(time.time())
9 return time.time()
11 def earlier(old, new):
12 # minimum of two things, but "None" counts as +infinity
13 if old:
14 if new < old:
15 return new
16 return old
17 return new
19 def later(old, new):
20 # maximum of two things, but "None" counts as -infinity
21 if old:
22 if new > old:
23 return new
24 return old
25 return new
27 class CancelableDeferred(Deferred):
28 """I am a version of Deferred that can be canceled by calling my
29 .cancel() method. After being canceled, no callbacks or errbacks will be
30 executed.
31 """
32 def __init__(self):
33 Deferred.__init__(self)
34 self.canceled = 0
35 def cancel(self):
36 self.canceled = 1
37 def _runCallbacks(self):
38 if self.canceled:
39 self.callbacks = []
40 return
41 Deferred._runCallbacks(self)
43 def ignoreStaleRefs(failure):
44 """d.addErrback(util.ignoreStaleRefs)"""
45 r = failure.trap(pb.DeadReferenceError, pb.PBConnectionLost)
46 return None
48 class _None:
49 pass
51 class ComparableMixin:
52 """Specify a list of attributes that are 'important'. These will be used
53 for all comparison operations."""
55 compare_attrs = []
57 def __hash__(self):
58 alist = [self.__class__] + \
59 [getattr(self, name, _None) for name in self.compare_attrs]
60 return hash(tuple(alist))
62 def __cmp__(self, them):
63 if cmp(type(self), type(them)):
64 return cmp(type(self), type(them))
65 if cmp(self.__class__, them.__class__):
66 return cmp(self.__class__, them.__class__)
67 assert self.compare_attrs == them.compare_attrs
68 self_list= [getattr(self, name, _None) for name in self.compare_attrs]
69 them_list= [getattr(them, name, _None) for name in self.compare_attrs]
70 return cmp(self_list, them_list)