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 # Remove potentially harmful characters from builder name if it is to be 107 # used as the build dir. 108 badchars_map = string.maketrans("\t !#$%&'()*+,./:;<=>?@[\\]^{|}~", 109 "______________________________")
110 -def safeTranslate(str):
111 if isinstance(str, unicode): 112 str = str.encode('utf8') 113 return str.translate(badchars_map)
114
115 -def none_or_str(x):
116 """Cast X to a str if it is not None""" 117 if x is not None and not isinstance(x, str): 118 return str(x) 119 return x
120 121 # place a working json module at 'buildbot.util.json'. Code is adapted from 122 # Paul Wise <pabs@debian.org>: 123 # http://lists.debian.org/debian-python/2010/02/msg00016.html 124 # json doesn't exist as a standard module until python2.6 125 # However python2.6's json module is much slower than simplejson, so we prefer 126 # to use simplejson if available. 127 try: 128 import simplejson as json 129 assert json 130 except ImportError: 131 import json # python 2.6 or 2.7 132 try: 133 _tmp = json.loads 134 except AttributeError: 135 import warnings 136 import sys 137 warnings.warn("Use simplejson, not the old json module.") 138 sys.modules.pop('json') # get rid of the bad json module 139 import simplejson as json 140 141 # changes and schedulers consider None to be a legitimate name for a branch, 142 # which makes default function keyword arguments hard to handle. This value 143 # is always false.
144 -class NotABranch:
145 - def __nonzero__(self):
146 return False
147 NotABranch = NotABranch() 148 149 # time-handling methods 150
151 -class UTC(datetime.tzinfo):
152 """Simple definition of UTC timezone"""
153 - def utcoffset(self, dt):
154 return datetime.timedelta(0)
155
156 - def dst(self, dt):
157 return datetime.timedelta(0)
158
159 - def tzname(self):
160 return "UTC"
161 UTC = UTC() 162
163 -def epoch2datetime(epoch):
164 """Convert a UNIX epoch time to a datetime object, in the UTC timezone""" 165 return datetime.datetime.fromtimestamp(epoch, tz=UTC)
166
167 -def datetime2epoch(dt):
168 """Convert a non-naive datetime object to a UNIX epoch timestamp""" 169 return calendar.timegm(dt.utctimetuple())
170 171 __all__ = [ 172 'naturalSort', 'now', 'formatInterval', 'ComparableMixin', 'json', 173 'safeTranslate', 'remove_userpassword', 'LRUCache', 'none_or_str', 174 'NotABranch', 'deferredLocked', 'SerializedInvocation', 'UTC' ] 175