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

Source Code for Package buildbot.util

  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  import time, re, string 
 18  import datetime 
 19  import calendar 
 20  from twisted.python import reflect  
 21   
 22  from buildbot.util.misc import deferredLocked, SerializedInvocation 
 23   
24 -def naturalSort(l):
25 l = l[:] 26 def try_int(s): 27 try: 28 return int(s) 29 except ValueError: 30 return s
31 def key_func(item): 32 return [try_int(s) for s in re.split('(\d+)', item)] 33 # prepend integer keys to each element, sort them, then strip the keys 34 keyed_l = [ (key_func(i), i) for i in l ] 35 keyed_l.sort() 36 l = [ i[1] for i in keyed_l ] 37 return l 38
39 -def flatten(l, types=list):
40 if l and isinstance(l, types): 41 rv = [] 42 for e in l: 43 if isinstance(e, types): 44 rv.extend(flatten(e)) 45 else: 46 rv.append(e) 47 return rv 48 else: 49 return l
50
51 -def now(_reactor=None):
52 if _reactor and hasattr(_reactor, "seconds"): 53 return _reactor.seconds() 54 else: 55 return time.time()
56
57 -def formatInterval(eta):
58 eta_parts = [] 59 if eta > 3600: 60 eta_parts.append("%d hrs" % (eta / 3600)) 61 eta %= 3600 62 if eta > 60: 63 eta_parts.append("%d mins" % (eta / 60)) 64 eta %= 60 65 eta_parts.append("%d secs" % eta) 66 return ", ".join(eta_parts)
67
68 -class ComparableMixin:
69 70 compare_attrs = [] 71
72 - class _None:
73 pass
74
75 - def __hash__(self):
76 compare_attrs = [] 77 reflect.accumulateClassList(self.__class__, 'compare_attrs', compare_attrs) 78 79 alist = [self.__class__] + \ 80 [getattr(self, name, self._None) for name in compare_attrs] 81 return hash(tuple(map(str, alist)))
82
83 - def __cmp__(self, them):
84 result = cmp(type(self), type(them)) 85 if result: 86 return result 87 88 result = cmp(self.__class__, them.__class__) 89 if result: 90 return result 91 92 compare_attrs = [] 93 reflect.accumulateClassList(self.__class__, 'compare_attrs', compare_attrs) 94 95 self_list = [getattr(self, name, self._None) 96 for name in compare_attrs] 97 them_list = [getattr(them, name, self._None) 98 for name in compare_attrs] 99 return cmp(self_list, them_list)
100
101 -def diffSets(old, new):
102 if not isinstance(old, set): 103 old = set(old) 104 if not isinstance(new, set): 105 new = set(new) 106 return old - new, new - old
107 108 # Remove potentially harmful characters from builder name if it is to be 109 # used as the build dir. 110 badchars_map = string.maketrans("\t !#$%&'()*+,./:;<=>?@[\\]^{|}~", 111 "______________________________")
112 -def safeTranslate(str):
113 if isinstance(str, unicode): 114 str = str.encode('utf8') 115 return str.translate(badchars_map)
116
117 -def none_or_str(x):
118 if x is not None and not isinstance(x, str): 119 return str(x) 120 return x
121 122 # place a working json module at 'buildbot.util.json'. Code is adapted from 123 # Paul Wise <pabs@debian.org>: 124 # http://lists.debian.org/debian-python/2010/02/msg00016.html 125 # json doesn't exist as a standard module until python2.6 126 # However python2.6's json module is much slower than simplejson, so we prefer 127 # to use simplejson if available. 128 try: 129 import simplejson as json 130 assert json 131 except ImportError: 132 import json # python 2.6 or 2.7 133 try: 134 _tmp = json.loads 135 except AttributeError: 136 import warnings 137 import sys 138 warnings.warn("Use simplejson, not the old json module.") 139 sys.modules.pop('json') # get rid of the bad json module 140 import simplejson as json 141 142 # changes and schedulers consider None to be a legitimate name for a branch, 143 # which makes default function keyword arguments hard to handle. This value 144 # is always false.
145 -class NotABranch:
146 - def __nonzero__(self):
147 return False
148 NotABranch = NotABranch() 149 150 # time-handling methods 151
152 -class UTC(datetime.tzinfo):
153 """Simple definition of UTC timezone"""
154 - def utcoffset(self, dt):
155 return datetime.timedelta(0)
156
157 - def dst(self, dt):
158 return datetime.timedelta(0)
159
160 - def tzname(self):
161 return "UTC"
162 UTC = UTC() 163
164 -def epoch2datetime(epoch):
165 """Convert a UNIX epoch time to a datetime object, in the UTC timezone""" 166 if epoch is not None: 167 return datetime.datetime.fromtimestamp(epoch, tz=UTC)
168
169 -def datetime2epoch(dt):
170 """Convert a non-naive datetime object to a UNIX epoch timestamp""" 171 if dt is not None: 172 return calendar.timegm(dt.utctimetuple())
173
174 -def makeList(input):
175 if isinstance(input, basestring): 176 return [ input ] 177 elif input is None: 178 return [ ] 179 else: 180 return list(input)
181
182 -def in_reactor(f):
183 """decorate a function by running it with maybeDeferred in a reactor""" 184 def wrap(*args, **kwargs): 185 from twisted.internet import reactor, defer 186 result = [ ] 187 def async(): 188 d = defer.maybeDeferred(f, *args, **kwargs) 189 def eb(f): 190 f.printTraceback()
191 d.addErrback(eb) 192 def do_stop(r): 193 result.append(r) 194 reactor.stop() 195 d.addBoth(do_stop) 196 reactor.callWhenRunning(async) 197 reactor.run() 198 return result[0] 199 wrap.__doc__ = f.__doc__ 200 wrap.__name__ = f.__name__ 201 wrap._orig = f # for tests 202 return wrap 203 204 __all__ = [ 205 'naturalSort', 'now', 'formatInterval', 'ComparableMixin', 'json', 206 'safeTranslate', 'LRUCache', 'none_or_str', 207 'NotABranch', 'deferredLocked', 'SerializedInvocation', 'UTC', 208 'diffLists', 'makeList', 'in_reactor' ] 209