Package buildbot :: Package changes :: Module changes
[frames] | no frames]

Source Code for Module buildbot.changes.changes

  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  import os, time 
 17  from cPickle import dump 
 18   
 19  from zope.interface import implements 
 20  from twisted.python import log, runtime 
 21  from twisted.web import html 
 22   
 23  from buildbot import interfaces, util 
 24  from buildbot.process.properties import Properties 
 25   
26 -class Change:
27 """I represent a single change to the source tree. This may involve 28 several files, but they are all changed by the same person, and there is 29 a change comment for the group as a whole. 30 31 If the version control system supports sequential repository- (or 32 branch-) wide change numbers (like SVN, P4, and Bzr), then revision= 33 should be set to that number. The highest such number will be used at 34 checkout time to get the correct set of files. 35 36 If it does not (like CVS), when= should be set to the timestamp (seconds 37 since epoch, as returned by time.time()) when the change was made. when= 38 will be filled in for you (to the current time) if you omit it, which is 39 suitable for ChangeSources which have no way of getting more accurate 40 timestamps. 41 42 The revision= and branch= values must be ASCII bytestrings, since they 43 will eventually be used in a ShellCommand and passed to os.exec(), which 44 requires bytestrings. These values will also be stored in a database, 45 possibly as unicode, so they must be safely convertable back and forth. 46 This restriction may be relaxed in the future. 47 48 Changes should be submitted to ChangeMaster.addChange() in 49 chronologically increasing order. Out-of-order changes will probably 50 cause the web status displays to be corrupted.""" 51 52 implements(interfaces.IStatusEvent) 53 54 number = None 55 56 branch = None 57 category = None 58 revision = None # used to create a source-stamp 59
60 - def __init__(self, who, files, comments, isdir=0, links=None, 61 revision=None, when=None, branch=None, category=None, 62 revlink='', properties={}, repository='', project=''):
63 self.who = who 64 self.comments = comments 65 self.isdir = isdir 66 if links is None: 67 links = [] 68 self.links = links 69 70 def none_or_unicode(x): 71 if x is None: return x 72 return unicode(x)
73 74 self.revision = none_or_unicode(revision) 75 now = util.now() 76 if when is None: 77 self.when = now 78 elif when > now: 79 # this happens when the committing system has an incorrect clock, for example. 80 # handle it gracefully 81 log.msg("received a Change with when > now; assuming the change happened now") 82 self.when = now 83 else: 84 self.when = when 85 self.branch = none_or_unicode(branch) 86 self.category = none_or_unicode(category) 87 self.revlink = revlink 88 self.properties = Properties() 89 self.properties.update(properties, "Change") 90 self.repository = repository 91 self.project = project 92 93 # keep a sorted list of the files, for easier display 94 self.files = files[:] 95 self.files.sort()
96
97 - def __setstate__(self, dict):
98 self.__dict__ = dict 99 # Older Changes won't have a 'properties' attribute in them 100 if not hasattr(self, 'properties'): 101 self.properties = Properties() 102 if not hasattr(self, 'revlink'): 103 self.revlink = ""
104
105 - def asText(self):
106 data = "" 107 data += self.getFileContents() 108 if self.repository: 109 data += "On: %s\n" % self.repository 110 if self.project: 111 data += "For: %s\n" % self.project 112 data += "At: %s\n" % self.getTime() 113 data += "Changed By: %s\n" % self.who 114 data += "Comments: %s" % self.comments 115 data += "Properties: \n%s\n\n" % self.getProperties() 116 return data
117
118 - def asDict(self):
119 '''returns a dictonary with suitable info for html/mail rendering''' 120 result = {} 121 122 files = [] 123 for file in self.files: 124 link = filter(lambda s: s.find(file) != -1, self.links) 125 if len(link) == 1: 126 url = link[0] 127 else: 128 url = None 129 files.append(dict(url=url, name=file)) 130 131 files = sorted(files, cmp=lambda a, b: a['name'] < b['name']) 132 133 # Constant 134 result['number'] = self.number 135 result['branch'] = self.branch 136 result['category'] = self.category 137 result['who'] = self.getShortAuthor() 138 result['comments'] = self.comments 139 result['revision'] = self.revision 140 result['rev'] = self.revision 141 result['when'] = self.when 142 result['at'] = self.getTime() 143 result['files'] = files 144 result['revlink'] = getattr(self, 'revlink', None) 145 result['properties'] = self.properties.asList() 146 result['repository'] = getattr(self, 'repository', None) 147 result['project'] = getattr(self, 'project', None) 148 return result
149
150 - def getShortAuthor(self):
151 return self.who
152
153 - def getTime(self):
154 if not self.when: 155 return "?" 156 return time.strftime("%a %d %b %Y %H:%M:%S", 157 time.localtime(self.when))
158
159 - def getTimes(self):
160 return (self.when, None)
161
162 - def getText(self):
163 return [html.escape(self.who)]
164 - def getLogs(self):
165 return {}
166
167 - def getFileContents(self):
168 data = "" 169 if len(self.files) == 1: 170 if self.isdir: 171 data += "Directory: %s\n" % self.files[0] 172 else: 173 data += "File: %s\n" % self.files[0] 174 else: 175 data += "Files:\n" 176 for f in self.files: 177 data += " %s\n" % f 178 return data
179
180 - def getProperties(self):
181 data = "" 182 for prop in self.properties.asList(): 183 data += " %s: %s" % (prop[0], prop[1]) 184 return data
185 186
187 -class ChangeMaster:
188 # this is a stub, retained to allow the "buildbot upgrade-master" tool to 189 # read old changes.pck pickle files and convert their contents into the 190 # new database format. This is only instantiated by that tool, or by 191 # test_db.py which tests that tool. The functionality that previously 192 # lived here has been moved into buildbot.changes.manager.ChangeManager 193
194 - def __init__(self):
195 self.changes = [] 196 # self.basedir must be filled in by the parent 197 self.nextNumber = 1
198
199 - def addChange(self, change):
200 change.number = self.nextNumber 201 self.nextNumber += 1 202 self.changes.append(change)
203
204 - def saveYourself(self):
205 filename = os.path.join(self.basedir, "changes.pck") 206 tmpfilename = filename + ".tmp" 207 try: 208 dump(self, open(tmpfilename, "wb")) 209 if runtime.platformType == 'win32': 210 # windows cannot rename a file on top of an existing one 211 if os.path.exists(filename): 212 os.unlink(filename) 213 os.rename(tmpfilename, filename) 214 except Exception: 215 log.msg("unable to save changes") 216 log.err()
217 218 # This method is used by contrib/fix_changes_pickle_encoding.py to recode all 219 # bytestrings in an old changes.pck into unicode strings
220 - def recode_changes(self, old_encoding, quiet=False):
221 """Processes the list of changes, with the change attributes re-encoded 222 as UTF-8 bytestrings""" 223 nconvert = 0 224 for c in self.changes: 225 # give revision special handling, in case it is an integer 226 if isinstance(c.revision, int): 227 c.revision = unicode(c.revision) 228 229 for attr in ("who", "comments", "revlink", "category", "branch", "revision"): 230 a = getattr(c, attr) 231 if isinstance(a, str): 232 try: 233 setattr(c, attr, a.decode(old_encoding)) 234 nconvert += 1 235 except UnicodeDecodeError: 236 raise UnicodeError("Error decoding %s of change #%s as %s:\n%r" % 237 (attr, c.number, old_encoding, a)) 238 if not quiet: 239 print "converted %d strings" % nconvert
240
241 -class OldChangeMaster(ChangeMaster):
242 # this is a reminder that the ChangeMaster class is old 243 pass
244 # vim: set ts=4 sts=4 sw=4 et: 245