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
)]
21 # prepend integer keys to each element, sort them, then strip the keys
22 keyed_l
= [ (key_func(i
), i
) for i
in l
]
24 l
= [ i
[1] for i
in keyed_l
]
28 #return int(time.time())
31 def earlier(old
, new
):
32 # minimum of two things, but "None" counts as +infinity
40 # maximum of two things, but "None" counts as -infinity
47 def formatInterval(eta
):
50 eta_parts
.append("%d hrs" % (eta
/ 3600))
53 eta_parts
.append("%d mins" % (eta
/ 60))
55 eta_parts
.append("%d secs" % eta
)
56 return ", ".join(eta_parts
)
58 class CancelableDeferred(Deferred
):
59 """I am a version of Deferred that can be canceled by calling my
60 .cancel() method. After being canceled, no callbacks or errbacks will be
64 Deferred
.__init
__(self
)
68 def _runCallbacks(self
):
72 Deferred
._runCallbacks
(self
)
74 def ignoreStaleRefs(failure
):
75 """d.addErrback(util.ignoreStaleRefs)"""
76 r
= failure
.trap(pb
.DeadReferenceError
, pb
.PBConnectionLost
)
82 class ComparableMixin
:
83 """Specify a list of attributes that are 'important'. These will be used
84 for all comparison operations."""
89 alist
= [self
.__class
__] + \
90 [getattr(self
, name
, _None
) for name
in self
.compare_attrs
]
91 return hash(tuple(alist
))
93 def __cmp__(self
, them
):
94 result
= cmp(type(self
), type(them
))
98 result
= cmp(self
.__class
__, them
.__class
__)
102 assert self
.compare_attrs
== them
.compare_attrs
103 self_list
= [getattr(self
, name
, _None
) for name
in self
.compare_attrs
]
104 them_list
= [getattr(them
, name
, _None
) for name
in self
.compare_attrs
]
105 return cmp(self_list
, them_list
)
108 if isinstance(s
, (str, unicode)):