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