1
2
3 import sys, os, re, time, random
4 from twisted.internet import utils, protocol, defer, reactor, task
5 from twisted.spread import pb
6 from twisted.cred import credentials
7 from twisted.python import log
8 from twisted.python.procutils import which
9
10 from buildbot.sourcestamp import SourceStamp
11 from buildbot.scripts import runner
12 from buildbot.util import now
13 from buildbot.status import builder
14
16
21
23 """This accepts the arguments of a command, without the actual
24 command itself."""
25 env = os.environ.copy()
26 env['LC_ALL'] = "C"
27 d = utils.getProcessOutputAndValue(self.exe, cmd, env=env,
28 path=self.treetop)
29 d.addCallback(self._didvc, cmd)
30 return d
32 (stdout, stderr, code) = res
33
34
35 return stdout
36
38 """Return a Deferred that fires with a SourceStamp instance."""
39 d = self.getBaseRevision()
40 d.addCallback(self.getPatch)
41 d.addCallback(self.done)
42 return d
49
51 patchlevel = 0
52 vcexe = "cvs"
54
55
56
57
58 self.baserev = time.strftime("%Y-%m-%d %H:%M:%S +0000",
59 time.gmtime(now()))
60 return defer.succeed(None)
61
63
64 if self.branch is not None:
65
66
67
68
69
70
71 raise RuntimeError("Sorry, CVS 'try' builds don't work with "
72 "branches")
73 args = ['-q', 'diff', '-u', '-D', self.baserev]
74 d = self.dovc(args)
75 d.addCallback(self.readPatch, self.patchlevel)
76 return d
77
79 patchlevel = 0
80 vcexe = "svn"
81
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 for line in res.split("\n"):
105 m = re.search(r'^Status against revision:\s+(\d+)', line)
106 if m:
107 self.baserev = int(m.group(1))
108 return
109 raise IndexError("Could not find 'Status against revision' in "
110 "SVN output: %s" % res)
115
117 patchlevel = 1
118 vcexe = "baz"
124 tid = res.strip()
125 slash = tid.index("/")
126 dd = tid.rindex("--")
127 self.branch = tid[slash+1:dd]
128 self.baserev = tid[dd+2:]
133
135 patchlevel = 1
136 vcexe = "tla"
138
139
140 d = self.dovc(["logs", "--full", "--reverse"])
141 d.addCallback(self.parseStatus)
142 return d
144 tid = res.split("\n")[0].strip()
145 slash = tid.index("/")
146 dd = tid.rindex("--")
147 self.branch = tid[slash+1:dd]
148 self.baserev = tid[dd+2:]
149
154
156 patchlevel = 0
157 vcexe = "bzr"
162
164 revno, revid= out.split()
165 self.baserev = 'revid:' + revid
166 return
167
172
174 patchlevel = 1
175 vcexe = "hg"
181 m = re.search(r'^(\w+)', output)
182 self.baserev = m.group(0)
187
201
203 patchlevel = 1
204 vcexe = "git"
205
207 d = self.dovc(["branch", "--no-color", "-v", "--no-abbrev"])
208 d.addCallback(self.parseStatus)
209 return d
210
212 d = self.dovc(["config", "-l"])
213 d.addCallback(self.parseConfig)
214 return d
215
217 git_config = {}
218 for l in res.split("\n"):
219 if l.strip():
220 parts = l.strip().split("=", 2)
221 git_config[parts[0]] = parts[1]
222
223
224 remote = git_config.get("branch." + self.branch + ".remote")
225 ref = git_config.get("branch." + self.branch + ".merge")
226 if remote and ref:
227 remote_branch = ref.split("/", 3)[-1]
228 d = self.dovc(["rev-parse", remote + "/" + remote_branch])
229 d.addCallback(self.override_baserev)
230 return d
231
233 self.baserev = res.strip()
234
236
237
238
239
240 m = re.search(r'^\* (\S+)\s+([0-9a-f]{40})', res, re.MULTILINE)
241 if m:
242 self.baserev = m.group(2)
243
244
245
246
247
248 if self.branch:
249 d = self.dovc(["rev-parse", self.branch])
250 if '/' in self.branch:
251 self.branch = self.branch.split('/', 1)[1]
252 d.addCallback(self.override_baserev)
253 return d
254 else:
255 self.branch = m.group(1)
256 return self.readConfig()
257 raise IndexError("Could not find current GIT branch: %s" % res)
258
260 d = self.dovc(["diff", self.baserev])
261 d.addCallback(self.readPatch, self.patchlevel)
262 return d
263
284
285
287 return "%d:%s," % (len(s), s)
288
289 -def createJobfile(bsid, branch, baserev, patchlevel, diff, builderNames):
290 job = ""
291 job += ns("1")
292 job += ns(bsid)
293 job += ns(branch)
294 job += ns(str(baserev))
295 job += ns("%d" % patchlevel)
296 job += ns(diff)
297 for bn in builderNames:
298 job += ns(bn)
299 return job
300
302 """walk upwards from the current directory until we find this topfile"""
303 if not start:
304 start = os.getcwd()
305 here = start
306 toomany = 20
307 while toomany > 0:
308 if os.path.exists(os.path.join(here, topfile)):
309 return here
310 next = os.path.dirname(here)
311 if next == here:
312 break
313 here = next
314 toomany -= 1
315 raise ValueError("Unable to find topfile '%s' anywhere from %s upwards"
316 % (topfile, start))
317
320 self.job = job
321 self.d = defer.Deferred()
326 sys.stdout.write(data)
328 sys.stderr.write(data)
330 sig = status_object.value.signal
331 rc = status_object.value.exitCode
332 if sig != None or rc != 0:
333 self.d.errback(RuntimeError("remote 'buildbot tryserver' failed"
334 ": sig=%s, rc=%s" % (sig, rc)))
335 return
336 self.d.callback((sig, rc))
337
339 retryCount = 5
340 retryDelay = 3
341
345
347
348
349 self.d = defer.Deferred()
350
351
352 reactor.callLater(1, self.go)
353 return self.d
354
355 - def go(self, dummy=None):
356 if self.retryCount == 0:
357 raise RuntimeError("couldn't find matching buildset")
358 self.retryCount -= 1
359 d = self.status.callRemote("getBuildSets")
360 d.addCallback(self._gotSets)
361
363 for bs,bsid in buildsets:
364 if bsid == self.bsid:
365
366 self.d.callback(bs)
367 return
368 d = defer.Deferred()
369 d.addCallback(self.go)
370 reactor.callLater(self.retryDelay, d.callback, None)
371
372
373 -class Try(pb.Referenceable):
374 buildsetStatus = None
375 quiet = False
376
378 self.config = config
379 self.connect = self.getopt('connect')
380 assert self.connect, "you must specify a connect style: ssh or pb"
381 self.builderNames = self.getopt('builders')
382
383 - def getopt(self, config_name, default=None):
384 value = self.config.get(config_name)
385 if value is None or value == []:
386 value = default
387 return value
388
390
391
392
393
394
395
396 self.bsid = "%d-%s" % (time.time(), random.randint(0, 1000000))
397
398
399 branch = self.getopt("branch")
400
401 difffile = self.config.get("diff")
402 if difffile:
403 baserev = self.config.get("baserev")
404 if difffile == "-":
405 diff = sys.stdin.read()
406 else:
407 diff = open(difffile,"r").read()
408 patch = (self.config['patchlevel'], diff)
409 ss = SourceStamp(branch, baserev, patch)
410 d = defer.succeed(ss)
411 else:
412 vc = self.getopt("vc")
413 if vc in ("cvs", "svn"):
414
415 topdir = self.getopt("try-topdir")
416 if topdir:
417 treedir = os.path.expanduser(topdir)
418 else:
419 topfile = self.getopt("try-topfile")
420 treedir = getTopdir(topfile)
421 else:
422 treedir = os.getcwd()
423 d = getSourceStamp(vc, treedir, branch)
424 d.addCallback(self._createJob_1)
425 return d
426
438
440
441 ss = self.sourcestamp
442 print ("Job:\n\tBranch: %s\n\tRevision: %s\n\tBuilders: %s\n%s"
443 % (ss.branch,
444 ss.revision,
445 self.builderNames,
446 ss.patch[1]))
447 d = defer.Deferred()
448 d.callback(True)
449 return d
450
452
453
454 if self.connect == "ssh":
455 tryhost = self.getopt("tryhost")
456 tryuser = self.getopt("username")
457 trydir = self.getopt("trydir")
458
459 argv = ["ssh", "-l", tryuser, tryhost,
460 "buildbot", "tryserver", "--jobdir", trydir]
461
462
463 pp = RemoteTryPP(self.jobfile)
464 p = reactor.spawnProcess(pp, argv[0], argv, os.environ)
465 d = pp.d
466 return d
467 if self.connect == "pb":
468 user = self.getopt("username")
469 passwd = self.getopt("passwd")
470 master = self.getopt("master")
471 tryhost, tryport = master.split(":")
472 tryport = int(tryport)
473 f = pb.PBClientFactory()
474 d = f.login(credentials.UsernamePassword(user, passwd))
475 reactor.connectTCP(tryhost, tryport, f)
476 d.addCallback(self._deliverJob_pb)
477 return d
478 raise RuntimeError("unknown connecttype '%s', should be 'ssh' or 'pb'"
479 % self.connect)
480
495
497
498
499 wait = bool(self.getopt("wait", "try_wait", False))
500 if not wait:
501
502
503
504
505
506 print "not waiting for builds to finish"
507 return
508 d = self.running = defer.Deferred()
509 if self.buildsetStatus:
510 self._getStatus_1()
511
512
513 master = self.getopt("master")
514 host, port = master.split(":")
515 port = int(port)
516 self.announce("contacting the status port at %s:%d" % (host, port))
517 f = pb.PBClientFactory()
518 creds = credentials.UsernamePassword("statusClient", "clientpw")
519 d = f.login(creds)
520 reactor.connectTCP(host, port, f)
521 d.addCallback(self._getStatus_ssh_1)
522 return self.running
523
531
538
540 self.builderNames = []
541 self.buildRequests = {}
542
543
544 self.builds = {}
545
546
547
548 self.outstanding = []
549
550
551
552 self.results = {}
553
554
555
556 self.currentStep = {}
557
558
559
560 self.ETA = {}
561
562 for n,br in brs:
563 self.builderNames.append(n)
564 self.buildRequests[n] = br
565 self.builds[n] = None
566 self.outstanding.append(n)
567 self.results[n] = [None,None]
568 self.currentStep[n] = None
569 self.ETA[n] = None
570
571
572 br.callRemote("subscribe", self)
573
574
575
576 self.printloop = task.LoopingCall(self.printStatus)
577 self.printloop.start(3, now=False)
578
579
580
581
583 if self.builds[builderName]:
584 self.builds[builderName].callRemote("unsubscribe", self)
585 self.builds[builderName] = bs
586 bs.callRemote("subscribe", self, 20)
587 d = bs.callRemote("waitUntilFinished")
588 d.addCallback(self._build_finished, builderName)
589
592
595
597 self.ETA[buildername] = now() + eta
598
600
601
602
603 self.builds[builderName] = None
604 self.ETA[builderName] = None
605 self.currentStep[builderName] = "finished"
606 d = bs.callRemote("getResults")
607 d.addCallback(self._build_finished_2, bs, builderName)
608 return d
610 self.results[builderName][0] = results
611 d = bs.callRemote("getText")
612 d.addCallback(self._build_finished_3, builderName)
613 return d
615 self.results[builderName][1] = text
616
617 self.outstanding.remove(builderName)
618 if not self.outstanding:
619
620 return self.statusDone()
621
623 names = self.buildRequests.keys()
624 names.sort()
625 for n in names:
626 if n not in self.outstanding:
627
628 code,text = self.results[n]
629 t = builder.Results[code]
630 if text:
631 t += " (%s)" % " ".join(text)
632 elif self.builds[n]:
633 t = self.currentStep[n] or "building"
634 if self.ETA[n]:
635 t += " [ETA %ds]" % (self.ETA[n] - now())
636 else:
637 t = "no build"
638 self.announce("%s: %s" % (n, t))
639 self.announce("")
640
642 self.printloop.stop()
643 print "All Builds Complete"
644
645 names = self.buildRequests.keys()
646 names.sort()
647 happy = True
648 for n in names:
649 code,text = self.results[n]
650 t = "%s: %s" % (n, builder.Results[code])
651 if text:
652 t += " (%s)" % " ".join(text)
653 print t
654 if self.results[n] != builder.SUCCESS:
655 happy = False
656
657 if happy:
658 self.exitcode = 0
659 else:
660 self.exitcode = 1
661 self.running.callback(self.exitcode)
662
664 if not self.quiet:
665 print message
666
668
669
670 print "using '%s' connect method" % self.connect
671 self.exitcode = 0
672 d = defer.Deferred()
673 d.addCallback(lambda res: self.createJob())
674 d.addCallback(lambda res: self.announce("job created"))
675 deliver = self.deliverJob
676 if bool(self.config.get("dryrun")):
677 deliver = self.fakeDeliverJob
678 d.addCallback(lambda res: deliver())
679 d.addCallback(lambda res: self.announce("job has been delivered"))
680 d.addCallback(lambda res: self.getStatus())
681 d.addErrback(log.err)
682 d.addCallback(self.cleanup)
683 d.addCallback(lambda res: reactor.stop())
684
685 reactor.callLater(0, d.callback, None)
686 reactor.run()
687 sys.exit(self.exitcode)
688
690 log.err(why)
691 print "error during 'try' processing"
692 print why
693
697