3 from cPickle
import dump
5 from zope
.interface
import implements
6 from twisted
.python
import log
7 from twisted
.internet
import defer
8 from twisted
.application
import service
9 from twisted
.web
import html
11 from buildbot
import interfaces
, util
14 <p>Changed by: <b>%(who)s</b><br />
15 Changed at: <b>%(at)s</b><br />
29 """I represent a single change to the source tree. This may involve
30 several files, but they are all changed by the same person, and there is
31 a change comment for the group as a whole.
33 If the version control system supports sequential repository- (or
34 branch-) wide change numbers (like SVN, P4, and Arch), then revision=
35 should be set to that number. The highest such number will be used at
36 checkout time to get the correct set of files.
38 If it does not (like CVS), when= should be set to the timestamp (seconds
39 since epoch, as returned by time.time()) when the change was made. when=
40 will be filled in for you (to the current time) if you omit it, which is
41 suitable for ChangeSources which have no way of getting more accurate
44 Changes should be submitted to ChangeMaster.addChange() in
45 chronologically increasing order. Out-of-order changes will probably
46 cause the html.Waterfall display to be corrupted."""
48 implements(interfaces
.IStatusEvent
)
54 revision
= None # used to create a source-stamp
56 def __init__(self
, who
, files
, comments
, isdir
=0, links
=[],
57 revision
=None, when
=None, branch
=None, category
=None):
59 self
.comments
= comments
62 self
.revision
= revision
67 self
.category
= category
69 # keep a sorted list of the files, for easier display
75 data
+= self
.getFileContents()
76 data
+= "At: %s\n" % self
.getTime()
77 data
+= "Changed By: %s\n" % self
.who
78 data
+= "Comments: %s\n\n" % self
.comments
83 for file in self
.files
:
84 link
= filter(lambda s
: s
.find(file) != -1, self
.links
)
87 links
.append('<a href="%s"><b>%s</b></a>' % (link
[0], file))
89 links
.append('<b>%s</b>' % file)
92 revision
= "Revision: <b>%s</b><br />\n" % self
.revision
95 branch
= "Branch: <b>%s</b><br />\n" % self
.branch
97 kwargs
= { 'who' : html
.escape(self
.who
),
98 'at' : self
.getTime(),
99 'files' : html
.UL(links
) + '\n',
100 'revision': revision
,
102 'comments': html
.PRE(self
.comments
) }
103 return html_tmpl
% kwargs
105 def get_HTML_box(self
, url
):
106 """Return the contents of a TD cell for the waterfall display.
108 @param url: the URL that points to an HTML page that will render
109 using our asHTML method. The Change is free to use this or ignore it
112 @return: the HTML that will be put inside the table cell. Typically
113 this is just a single href named after the author of the change and
114 pointing at the passed-in 'url'.
116 who
= self
.getShortAuthor()
117 return '<a href="%s" title="%s">%s</a>' % (url
,
118 html
.escape(self
.comments
),
121 def getShortAuthor(self
):
127 return time
.strftime("%a %d %b %Y %H:%M:%S",
128 time
.localtime(self
.when
))
131 return (self
.when
, None)
134 return [html
.escape(self
.who
)]
140 def getFileContents(self
):
142 if len(self
.files
) == 1:
144 data
+= "Directory: %s\n" % self
.files
[0]
146 data
+= "File: %s\n" % self
.files
[0]
153 class ChangeMaster(service
.MultiService
):
155 """This is the master-side service which receives file change
156 notifications from CVS. It keeps a log of these changes, enough to
157 provide for the HTML waterfall display, and to tell
158 temporarily-disconnected bots what they missed while they were
161 Change notifications come from two different kinds of sources. The first
162 is a PB service (servicename='changemaster', perspectivename='change'),
163 which provides a remote method called 'addChange', which should be
164 called with a dict that has keys 'filename' and 'comments'.
166 The second is a list of objects derived from the ChangeSource class.
167 These are added with .addSource(), which also sets the .changemaster
168 attribute in the source to point at the ChangeMaster. When the
169 application begins, these will be started with .start() . At shutdown
170 time, they will be terminated with .stop() . They must be persistable.
171 They are expected to call self.changemaster.addChange() with Change
174 There are several different variants of the second type of source:
176 - L{buildbot.changes.mail.MaildirSource} watches a maildir for CVS
177 commit mail. It uses DNotify if available, or polls every 10
178 seconds if not. It parses incoming mail to determine what files
181 - L{buildbot.changes.freshcvs.FreshCVSSource} makes a PB
182 connection to the CVSToys 'freshcvs' daemon and relays any
183 changes it announces.
187 implements(interfaces
.IEventSource
)
190 # todo: use Maildir class to watch for changes arriving by mail
193 service
.MultiService
.__init
__(self
)
195 # self.basedir must be filled in by the parent
198 def addSource(self
, source
):
199 assert interfaces
.IChangeSource
.providedBy(source
)
200 assert service
.IService
.providedBy(source
)
202 print "ChangeMaster.addSource", source
203 source
.setServiceParent(self
)
205 def removeSource(self
, source
):
206 assert source
in self
208 print "ChangeMaster.removeSource", source
, source
.parent
209 d
= defer
.maybeDeferred(source
.disownServiceParent
)
212 def addChange(self
, change
):
213 """Deliver a file change event. The event should be a Change object.
214 This method will timestamp the object as it is received."""
215 log
.msg("adding change, who %s, %d files, rev=%s, branch=%s, "
216 "comments %s, category %s" % (change
.who
, len(change
.files
),
217 change
.revision
, change
.branch
,
218 change
.comments
, change
.category
))
219 change
.number
= self
.nextNumber
221 self
.changes
.append(change
)
222 self
.parent
.addChange(change
)
223 # TODO: call pruneChanges after a while
225 def pruneChanges(self
):
226 self
.changes
= self
.changes
[-100:] # or something
228 def eventGenerator(self
, branches
=[]):
229 for i
in range(len(self
.changes
)-1, -1, -1):
231 if not branches
or c
.branch
in branches
:
234 def getChangeNumbered(self
, num
):
237 first
= self
.changes
[0].number
238 if first
+ len(self
.changes
)-1 != self
.changes
[-1].number
:
240 "lost a change somewhere: [0] is %d, [%d] is %d" % \
241 (self
.changes
[0].number
,
242 len(self
.changes
) - 1,
243 self
.changes
[-1].number
))
244 for c
in self
.changes
:
245 log
.msg("c[%d]: " % c
.number
, c
)
248 log
.msg(self
, "offset", offset
)
249 return self
.changes
[offset
]
251 def __getstate__(self
):
252 d
= service
.MultiService
.__getstate
__(self
)
254 del d
['services'] # lose all children
255 del d
['namedServices']
258 def __setstate__(self
, d
):
260 # self.basedir must be set by the parent
261 self
.services
= [] # they'll be repopulated by readConfig
262 self
.namedServices
= {}
265 def saveYourself(self
):
266 filename
= os
.path
.join(self
.basedir
, "changes.pck")
267 tmpfilename
= filename
+ ".tmp"
269 dump(self
, open(tmpfilename
, "wb"))
270 if sys
.platform
== 'win32':
271 # windows cannot rename a file on top of an existing one
272 if os
.path
.exists(filename
):
274 os
.rename(tmpfilename
, filename
)
276 log
.msg("unable to save changes")
279 def stopService(self
):
281 return service
.MultiService
.stopService(self
)