1 # -*- test-case-name: buildbot.test.test_step -*-
5 from zope
.interface
import implements
6 from twisted
.python
import log
7 from twisted
.python
.failure
import Failure
8 from twisted
.internet
import reactor
, defer
, error
10 from buildbot
import interfaces
11 from buildbot
.status
.builder
import SUCCESS
, WARNINGS
, FAILURE
, EXCEPTION
12 from buildbot
.status
.builder
import Results
, BuildRequestStatus
13 from buildbot
.status
.progress
import BuildProgress
14 from buildbot
.process
.properties
import Properties
17 """I represent a request to a specific Builder to run a single build.
19 I have a SourceStamp which specifies what sources I will build. This may
20 specify a specific revision of the source tree (so source.branch,
21 source.revision, and source.patch are used). The .patch attribute is
22 either None or a tuple of (patchlevel, diff), consisting of a number to
23 use in 'patch -pN', and a unified-format context diff.
25 Alternatively, the SourceStamp may specify a set of Changes to be built,
26 contained in source.changes. In this case, I may be mergeable with other
27 BuildRequests on the same branch.
29 I may be part of a BuildSet, in which case I will report status results
32 I am paired with a BuildRequestStatus object, to which I feed status
35 @type source: a L{buildbot.sourcestamp.SourceStamp} instance.
36 @ivar source: the source code that this BuildRequest use
39 @ivar reason: the reason this Build is being requested. Schedulers
40 provide this, but for forced builds the user requesting the
41 build will provide a string.
43 @type properties: Properties object
44 @ivar properties: properties that should be applied to this build
46 @ivar status: the IBuildStatus object which tracks our status
48 @ivar submittedAt: a timestamp (seconds since epoch) when this request
49 was submitted to the Builder. This is used by the CVS
50 step to compute a checkout timestamp.
55 startCount
= 0 # how many times we have tried to start this build
57 implements(interfaces
.IBuildRequestControl
)
59 def __init__(self
, reason
, source
, builderName
=None, properties
=None):
60 # TODO: remove the =None on builderName, it is there so I don't have
61 # to change a lot of tests that create BuildRequest objects
62 assert interfaces
.ISourceStamp(source
, None)
66 self
.properties
= Properties()
68 self
.properties
.updateFromProperties(properties
)
70 self
.start_watchers
= []
71 self
.finish_watchers
= []
72 self
.status
= BuildRequestStatus(source
, builderName
)
74 def canBeMergedWith(self
, other
):
75 return self
.source
.canBeMergedWith(other
.source
)
77 def mergeWith(self
, others
):
78 return self
.source
.mergeWith([o
.source
for o
in others
])
80 def mergeReasons(self
, others
):
81 """Return a reason for the merged build request."""
83 for req
in [self
] + others
:
84 if req
.reason
and req
.reason
not in reasons
:
85 reasons
.append(req
.reason
)
86 return ", ".join(reasons
)
88 def waitUntilFinished(self
):
89 """Get a Deferred that will fire (with a
90 L{buildbot.interfaces.IBuildStatus} instance when the build
93 self
.finish_watchers
.append(d
)
96 # these are called by the Builder
98 def requestSubmitted(self
, builder
):
99 # the request has been placed on the queue
100 self
.builder
= builder
102 def buildStarted(self
, build
, buildstatus
):
103 """This is called by the Builder when a Build has been started in the
104 hopes of satifying this BuildRequest. It may be called multiple
105 times, since interrupted builds and lost buildslaves may force
106 multiple Builds to be run until the fate of the BuildRequest is known
108 for o
in self
.start_watchers
[:]:
109 # these observers get the IBuildControl
111 # while these get the IBuildStatus
112 self
.status
.buildStarted(buildstatus
)
114 def finished(self
, buildstatus
):
115 """This is called by the Builder when the BuildRequest has been
116 retired. This happens when its Build has either succeeded (yay!) or
117 failed (boo!). TODO: If it is halted due to an exception (oops!), or
118 some other retryable error, C{finished} will not be called yet."""
120 for w
in self
.finish_watchers
:
121 w
.callback(buildstatus
)
122 self
.finish_watchers
= []
124 # IBuildRequestControl
126 def subscribe(self
, observer
):
127 self
.start_watchers
.append(observer
)
128 def unsubscribe(self
, observer
):
129 self
.start_watchers
.remove(observer
)
132 """Cancel this request. This can only be successful if the Build has
133 not yet been started.
135 @return: a boolean indicating if the cancel was successful."""
137 return self
.builder
.cancelBuildRequest(self
)
142 """I represent a single build by a single slave. Specialized Builders can
143 use subclasses of Build to hold status information unique to those build
146 I control B{how} the build proceeds. The actual build is broken up into a
147 series of steps, saved in the .buildSteps[] array as a list of
148 L{buildbot.process.step.BuildStep} objects. Each step is a single remote
149 command, possibly a shell command.
151 During the build, I put status information into my C{BuildStatus}
154 After the build, I go away.
156 I can be used by a factory by setting buildClass on
157 L{buildbot.process.factory.BuildFactory}
159 @ivar requests: the list of L{BuildRequest}s that triggered me
160 @ivar build_status: the L{buildbot.status.builder.BuildStatus} that
164 implements(interfaces
.IBuildControl
)
172 def __init__(self
, requests
):
173 self
.requests
= requests
174 for req
in self
.requests
:
177 # build a source stamp
178 self
.source
= requests
[0].mergeWith(requests
[1:])
179 self
.reason
= requests
[0].mergeReasons(requests
[1:])
182 self
.currentStep
= None
183 self
.slaveEnvironment
= {}
185 def setBuilder(self
, builder
):
187 Set the given builder as our builder.
189 @type builder: L{buildbot.process.builder.Builder}
191 self
.builder
= builder
193 def setLocks(self
, locks
):
196 def getSourceStamp(self
):
199 def setProperty(self
, propname
, value
, source
):
200 """Set a property on this build. This may only be called after the
201 build has started, so that it has a BuildStatus object where the
202 properties can live."""
203 self
.build_status
.setProperty(propname
, value
, source
)
205 def getProperties(self
):
206 return self
.build_status
.getProperties()
208 def getProperty(self
, propname
):
209 return self
.build_status
.getProperty(propname
)
211 def allChanges(self
):
212 return self
.source
.changes
215 # return a list of all source files that were changed
218 for c
in self
.allChanges():
226 return "<Build %s>" % (self
.builder
.name
,)
228 def __getstate__(self
):
229 d
= self
.__dict
__.copy()
230 if d
.has_key('remote'):
236 for c
in self
.allChanges():
237 if c
.who
not in blamelist
:
238 blamelist
.append(c
.who
)
242 def changesText(self
):
244 for c
in self
.allChanges():
245 changetext
+= "-" * 60 + "\n\n" + c
.asText() + "\n"
246 # consider sorting these by number
249 def setStepFactories(self
, step_factories
):
250 """Set a list of 'step factories', which are tuples of (class,
251 kwargs), where 'class' is generally a subclass of step.BuildStep .
252 These are used to create the Steps themselves when the Build starts
253 (as opposed to when it is first created). By creating the steps
254 later, their __init__ method will have access to things like
255 build.allFiles() ."""
256 self
.stepFactories
= list(step_factories
)
262 def getSlaveCommandVersion(self
, command
, oldversion
=None):
263 return self
.slavebuilder
.getSlaveCommandVersion(command
, oldversion
)
264 def getSlaveName(self
):
265 return self
.slavebuilder
.slave
.slavename
267 def setupProperties(self
):
268 props
= self
.getProperties()
270 # start with global properties from the configuration
271 buildmaster
= self
.builder
.botmaster
.parent
272 props
.updateFromProperties(buildmaster
.properties
)
274 # get any properties from requests (this is the path through
275 # which schedulers will send us properties)
276 for rq
in self
.requests
:
277 props
.updateFromProperties(rq
.properties
)
279 # now set some properties of our own, corresponding to the
281 props
.setProperty("buildername", self
.builder
.name
, "Build")
282 props
.setProperty("buildnumber", self
.build_status
.number
, "Build")
283 props
.setProperty("branch", self
.source
.branch
, "Build")
284 props
.setProperty("revision", self
.source
.revision
, "Build")
286 def setupSlaveBuilder(self
, slavebuilder
):
287 self
.slavebuilder
= slavebuilder
289 # navigate our way back to the L{buildbot.buildslave.BuildSlave}
290 # object that came from the config, and get its properties
291 buildslave_properties
= slavebuilder
.slave
.properties
292 self
.getProperties().updateFromProperties(buildslave_properties
)
294 self
.slavename
= slavebuilder
.slave
.slavename
295 self
.build_status
.setSlavename(self
.slavename
)
297 def startBuild(self
, build_status
, expectations
, slavebuilder
):
298 """This method sets up the build, then starts it by invoking the
299 first Step. It returns a Deferred which will fire when the build
300 finishes. This Deferred is guaranteed to never errback."""
302 # we are taking responsibility for watching the connection to the
303 # remote. This responsibility was held by the Builder until our
304 # startBuild was called, and will not return to them until we fire
305 # the Deferred returned by this method.
307 log
.msg("%s.startBuild" % self
)
308 self
.build_status
= build_status
309 # now that we have a build_status, we can set properties
310 self
.setupProperties()
311 self
.setupSlaveBuilder(slavebuilder
)
313 # convert all locks into their real forms
314 self
.locks
= [self
.builder
.botmaster
.getLockByID(l
)
316 # then narrow SlaveLocks down to the right slave
317 self
.locks
= [l
.getLock(self
.slavebuilder
) for l
in self
.locks
]
318 self
.remote
= slavebuilder
.remote
319 self
.remote
.notifyOnDisconnect(self
.lostRemote
)
320 d
= self
.deferred
= defer
.Deferred()
321 def _release_slave(res
):
322 self
.slavebuilder
.buildFinished()
324 d
.addCallback(_release_slave
)
327 self
.setupBuild(expectations
) # create .steps
329 # the build hasn't started yet, so log the exception as a point
330 # event instead of flunking the build. TODO: associate this
331 # failure with the build instead. this involves doing
332 # self.build_status.buildStarted() from within the exception
334 log
.msg("Build.setupBuild failed")
336 self
.builder
.builder_status
.addPointEvent(["setupBuild",
340 self
.results
= FAILURE
345 self
.acquireLocks().addCallback(self
._startBuild
_2)
348 def acquireLocks(self
, res
=None):
349 log
.msg("acquireLocks(step %s, locks %s)" % (self
, self
.locks
))
351 return defer
.succeed(None)
352 for lock
in self
.locks
:
353 if not lock
.isAvailable():
354 log
.msg("Build %s waiting for lock %s" % (self
, lock
))
355 d
= lock
.waitUntilMaybeAvailable(self
)
356 d
.addCallback(self
.acquireLocks
)
358 # all locks are available, claim them all
359 for lock
in self
.locks
:
361 return defer
.succeed(None)
363 def _startBuild_2(self
, res
):
364 self
.build_status
.buildStarted(self
)
367 def setupBuild(self
, expectations
):
368 # create the actual BuildSteps. If there are any name collisions, we
369 # add a count to the loser until it is unique.
371 self
.stepStatuses
= {}
375 for factory
, args
in self
.stepFactories
:
378 step
= factory(**args
)
380 log
.msg("error while creating step, factory=%s, args=%s"
384 step
.setBuildSlave(self
.slavebuilder
.slave
)
385 step
.setDefaultWorkdir(self
.workdir
)
388 while name
in stepnames
and count
< 100:
390 name
= step
.name
+ "_%d" % count
391 if name
in stepnames
:
392 raise RuntimeError("duplicate step '%s'" % step
.name
)
394 stepnames
.append(name
)
395 self
.steps
.append(step
)
397 # tell the BuildStatus about the step. This will create a
398 # BuildStepStatus and bind it to the Step.
399 step_status
= self
.build_status
.addStepWithName(name
)
400 step
.setStepStatus(step_status
)
404 # XXX: maybe bail if step.progressMetrics is empty? or skip
405 # progress for that one step (i.e. "it is fast"), or have a
406 # separate "variable" flag that makes us bail on progress
408 sp
= step
.setupProgress()
412 # Create a buildbot.status.progress.BuildProgress object. This is
413 # called once at startup to figure out how to build the long-term
414 # Expectations object, and again at the start of each build to get a
415 # fresh BuildProgress object to track progress for that individual
416 # build. TODO: revisit at-startup call
419 self
.progress
= BuildProgress(sps
)
420 if self
.progress
and expectations
:
421 self
.progress
.setExpectationsFrom(expectations
)
423 # we are now ready to set up our BuildStatus.
424 self
.build_status
.setSourceStamp(self
.source
)
425 self
.build_status
.setReason(self
.reason
)
426 self
.build_status
.setBlamelist(self
.blamelist())
427 self
.build_status
.setProgress(self
.progress
)
429 self
.results
= [] # list of FAILURE, SUCCESS, WARNINGS, SKIPPED
430 self
.result
= SUCCESS
# overall result, may downgrade after each step
431 self
.text
= [] # list of text string lists (text2)
433 def getNextStep(self
):
434 """This method is called to obtain the next BuildStep for this build.
435 When it returns None (or raises a StopIteration exception), the build
439 return self
.steps
.pop(0)
441 def startNextStep(self
):
443 s
= self
.getNextStep()
444 except StopIteration:
447 return self
.allStepsDone()
449 d
= defer
.maybeDeferred(s
.startStep
, self
.remote
)
450 d
.addCallback(self
._stepDone
, s
)
451 d
.addErrback(self
.buildException
)
453 def _stepDone(self
, results
, step
):
454 self
.currentStep
= None
456 return # build was interrupted, don't keep building
457 terminate
= self
.stepDone(results
, step
) # interpret/merge results
459 return self
.allStepsDone()
462 def stepDone(self
, result
, step
):
463 """This method is called when the BuildStep completes. It is passed a
464 status object from the BuildStep and is responsible for merging the
465 Step's results into those of the overall Build."""
469 if type(result
) == types
.TupleType
:
470 result
, text
= result
471 assert type(result
) == type(SUCCESS
)
472 log
.msg(" step '%s' complete: %s" % (step
.name
, Results
[result
]))
473 self
.results
.append(result
)
475 self
.text
.extend(text
)
478 if result
== FAILURE
:
479 if step
.warnOnFailure
:
480 if self
.result
!= FAILURE
:
481 self
.result
= WARNINGS
482 if step
.flunkOnFailure
:
483 self
.result
= FAILURE
484 if step
.haltOnFailure
:
485 self
.result
= FAILURE
487 elif result
== WARNINGS
:
488 if step
.warnOnWarnings
:
489 if self
.result
!= FAILURE
:
490 self
.result
= WARNINGS
491 if step
.flunkOnWarnings
:
492 self
.result
= FAILURE
493 elif result
== EXCEPTION
:
494 self
.result
= EXCEPTION
498 def lostRemote(self
, remote
=None):
499 # the slave went away. There are several possible reasons for this,
500 # and they aren't necessarily fatal. For now, kill the build, but
501 # TODO: see if we can resume the build when it reconnects.
502 log
.msg("%s.lostRemote" % self
)
505 # this should cause the step to finish.
506 log
.msg(" stopping currentStep", self
.currentStep
)
507 self
.currentStep
.interrupt(Failure(error
.ConnectionLost()))
509 def stopBuild(self
, reason
="<no reason given>"):
510 # the idea here is to let the user cancel a build because, e.g.,
511 # they realized they committed a bug and they don't want to waste
512 # the time building something that they know will fail. Another
513 # reason might be to abandon a stuck build. We want to mark the
514 # build as failed quickly rather than waiting for the slave's
515 # timeout to kill it on its own.
517 log
.msg(" %s: stopping build: %s" % (self
, reason
))
520 # TODO: include 'reason' in this point event
521 self
.builder
.builder_status
.addPointEvent(['interrupt'])
522 self
.currentStep
.interrupt(reason
)
524 # TODO: maybe let its deferred do buildFinished
525 if self
.currentStep
and self
.currentStep
.progress
:
526 # XXX: really .fail or something
527 self
.currentStep
.progress
.finish()
528 text
= ["stopped", reason
]
529 self
.buildFinished(text
, "red", FAILURE
)
531 def allStepsDone(self
):
532 if self
.result
== FAILURE
:
535 elif self
.result
== WARNINGS
:
538 elif self
.result
== EXCEPTION
:
543 text
= ["build", "successful"]
544 text
.extend(self
.text
)
545 return self
.buildFinished(text
, color
, self
.result
)
547 def buildException(self
, why
):
548 log
.msg("%s.buildException" % self
)
550 self
.buildFinished(["build", "exception"], "purple", FAILURE
)
552 def buildFinished(self
, text
, color
, results
):
553 """This method must be called when the last Step has completed. It
554 marks the Build as complete and returns the Builder to the 'idle'
557 It takes three arguments which describe the overall build status:
558 text, color, results. 'results' is one of SUCCESS, WARNINGS, or
561 If 'results' is SUCCESS or WARNINGS, we will permit any dependant
562 builds to start. If it is 'FAILURE', those builds will be
567 self
.remote
.dontNotifyOnDisconnect(self
.lostRemote
)
568 self
.results
= results
570 log
.msg(" %s: build finished" % self
)
571 self
.build_status
.setText(text
)
572 self
.build_status
.setColor(color
)
573 self
.build_status
.setResults(results
)
574 self
.build_status
.buildFinished()
576 # XXX: also test a 'timing consistent' flag?
577 log
.msg(" setting expectations for next time")
578 self
.builder
.setExpectations(self
.progress
)
579 reactor
.callLater(0, self
.releaseLocks
)
580 self
.deferred
.callback(self
)
583 def releaseLocks(self
):
584 log
.msg("releaseLocks(%s): %s" % (self
, self
.locks
))
585 for lock
in self
.locks
:
591 return self
.build_status
593 # stopBuild is defined earlier