1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import time, re, string
18 import datetime
19 import calendar
20 from buildbot.util.misc import deferredLocked, SerializedInvocation
21
23 l = l[:]
24 def try_int(s):
25 try:
26 return int(s)
27 except ValueError:
28 return s
29 def key_func(item):
30 return [try_int(s) for s in re.split('(\d+)', item)]
31
32 keyed_l = [ (key_func(i), i) for i in l ]
33 keyed_l.sort()
34 l = [ i[1] for i in keyed_l ]
35 return l
36
38 if l and type(l[0]) == list:
39 rv = []
40 for e in l:
41 if type(e) == list:
42 rv.extend(flatten(e))
43 else:
44 rv.append(e)
45 return rv
46 else:
47 return l
48
49 -def now(_reactor=None):
54
65
67
68 compare_attrs = []
69
72
74 alist = [self.__class__] + \
75 [getattr(self, name, self._None) for name in self.compare_attrs]
76 return hash(tuple(map(str, alist)))
77
79 result = cmp(type(self), type(them))
80 if result:
81 return result
82
83 result = cmp(self.__class__.__name__, them.__class__.__name__)
84 if result:
85 return result
86
87 result = cmp(self.compare_attrs, them.compare_attrs)
88 if result:
89 return result
90
91 self_list = [getattr(self, name, self._None)
92 for name in self.compare_attrs]
93 them_list = [getattr(them, name, self._None)
94 for name in self.compare_attrs]
95 return cmp(self_list, them_list)
96
98 if not isinstance(old, set):
99 old = set(old)
100 if not isinstance(new, set):
101 new = set(new)
102 return old - new, new - old
103
104
105
106 badchars_map = string.maketrans("\t !#$%&'()*+,./:;<=>?@[\\]^{|}~",
107 "______________________________")
109 if isinstance(str, unicode):
110 str = str.encode('utf8')
111 return str.translate(badchars_map)
112
114 if x is not None and not isinstance(x, str):
115 return str(x)
116 return x
117
118
119
120
121
122
123
124 try:
125 import simplejson as json
126 assert json
127 except ImportError:
128 import json
129 try:
130 _tmp = json.loads
131 except AttributeError:
132 import warnings
133 import sys
134 warnings.warn("Use simplejson, not the old json module.")
135 sys.modules.pop('json')
136 import simplejson as json
137
138
139
140
144 NotABranch = NotABranch()
145
146
147
148 -class UTC(datetime.tzinfo):
149 """Simple definition of UTC timezone"""
151 return datetime.timedelta(0)
152
154 return datetime.timedelta(0)
155
158 UTC = UTC()
159
161 """Convert a UNIX epoch time to a datetime object, in the UTC timezone"""
162 if epoch is not None:
163 return datetime.datetime.fromtimestamp(epoch, tz=UTC)
164
166 """Convert a non-naive datetime object to a UNIX epoch timestamp"""
167 if dt is not None:
168 return calendar.timegm(dt.utctimetuple())
169
171 if isinstance(input, basestring):
172 return [ input ]
173 elif input is None:
174 return [ ]
175 else:
176 return list(input)
177
179 """decorate a function by running it with maybeDeferred in a reactor"""
180 def wrap(*args, **kwargs):
181 from twisted.internet import reactor, defer
182 result = [ ]
183 def async():
184 d = defer.maybeDeferred(f, *args, **kwargs)
185 def eb(f):
186 f.printTraceback()
187 d.addErrback(eb)
188 def do_stop(r):
189 result.append(r)
190 reactor.stop()
191 d.addBoth(do_stop)
192 reactor.callWhenRunning(async)
193 reactor.run()
194 return result[0]
195 wrap.__doc__ = f.__doc__
196 wrap.__name__ = f.__name__
197 wrap._orig = f
198 return wrap
199
200 __all__ = [
201 'naturalSort', 'now', 'formatInterval', 'ComparableMixin', 'json',
202 'safeTranslate', 'LRUCache', 'none_or_str',
203 'NotABranch', 'deferredLocked', 'SerializedInvocation', 'UTC',
204 'diffLists', 'makeList', 'in_reactor' ]
205