1 # -*- test-case-name: buildbot.test.test_util -*-
3 from twisted
.internet
.defer
import Deferred
4 from twisted
.spread
import pb
8 """Returns a sorted copy of l, so that numbers in strings are sorted in the
11 e.g. ['foo10', 'foo1', 'foo2'] will be sorted as ['foo1', 'foo2', 'foo10']
12 instead of the default ['foo1', 'foo10', 'foo2']"""
20 return [try_int(s
) for s
in re
.split('(\d+)', item
)]
25 #return int(time.time())
28 def earlier(old
, new
):
29 # minimum of two things, but "None" counts as +infinity
37 # maximum of two things, but "None" counts as -infinity
44 def formatInterval(eta
):
47 eta_parts
.append("%d hrs" % (eta
/ 3600))
50 eta_parts
.append("%d mins" % (eta
/ 60))
52 eta_parts
.append("%d secs" % eta
)
53 return ", ".join(eta_parts
)
55 class CancelableDeferred(Deferred
):
56 """I am a version of Deferred that can be canceled by calling my
57 .cancel() method. After being canceled, no callbacks or errbacks will be
61 Deferred
.__init
__(self
)
65 def _runCallbacks(self
):
69 Deferred
._runCallbacks
(self
)
71 def ignoreStaleRefs(failure
):
72 """d.addErrback(util.ignoreStaleRefs)"""
73 r
= failure
.trap(pb
.DeadReferenceError
, pb
.PBConnectionLost
)
79 class ComparableMixin
:
80 """Specify a list of attributes that are 'important'. These will be used
81 for all comparison operations."""
86 alist
= [self
.__class
__] + \
87 [getattr(self
, name
, _None
) for name
in self
.compare_attrs
]
88 return hash(tuple(alist
))
90 def __cmp__(self
, them
):
91 result
= cmp(type(self
), type(them
))
95 result
= cmp(self
.__class
__, them
.__class
__)
99 assert self
.compare_attrs
== them
.compare_attrs
100 self_list
= [getattr(self
, name
, _None
) for name
in self
.compare_attrs
]
101 them_list
= [getattr(them
, name
, _None
) for name
in self
.compare_attrs
]
102 return cmp(self_list
, them_list
)
105 if isinstance(s
, (str, unicode)):