b2303f7b7363694dedd349412b02bf80fa6ae1dd
[mat/samba.git] / script / autobuild.py
1 #!/usr/bin/env python
2 # run tests on all Samba subprojects and push to a git tree on success
3 # Copyright Andrew Tridgell 2010
4 # released under GNU GPL v3 or later
5
6 from subprocess import call, check_call,Popen, PIPE
7 import os, tarfile, sys, time
8 from optparse import OptionParser
9 import smtplib
10 import email
11 from email.mime.text import MIMEText
12 from email.mime.base import MIMEBase
13 from email.mime.application import MIMEApplication
14 from email.mime.multipart import MIMEMultipart
15 from distutils.sysconfig import get_python_lib
16 import platform
17
18 # This speeds up testing remarkably.
19 os.environ['TDB_NO_FSYNC'] = '1'
20
21 cleanup_list = []
22
23 builddirs = {
24     "ctdb"    : "ctdb",
25     "samba"  : ".",
26     "samba-ctdb" : ".",
27     "samba-libs"  : ".",
28     "ldb"     : "lib/ldb",
29     "tdb"     : "lib/tdb",
30     "talloc"  : "lib/talloc",
31     "replace" : "lib/replace",
32     "tevent"  : "lib/tevent",
33     "pidl"    : "pidl",
34     "pass"    : ".",
35     "fail"    : ".",
36     "retry"   : "."
37     }
38
39 defaulttasks = [ "ctdb", "samba", "samba-ctdb", "samba-libs", "ldb", "tdb", "talloc", "replace", "tevent", "pidl" ]
40
41 tasks = {
42     "ctdb" : [ ("random-sleep", "../script/random-sleep.sh 60 600", "text/plain"),
43                ("configure", "./configure ${PREFIX}", "text/plain"),
44                ("make", "make all", "text/plain"),
45                ("install", "make install", "text/plain"),
46                ("test", "make autotest", "text/plain"),
47                ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
48                ("clean", "make clean", "text/plain") ],
49
50     # We have 'test' before 'install' because, 'test' should work without 'install'
51     "samba" : [ ("configure", "./configure.developer --picky-developer ${PREFIX} --with-selftest-prefix=./bin/ab  --with-profiling-data", "text/plain"),
52                 ("make", "make -j", "text/plain"),
53                 ("test", "make test FAIL_IMMEDIATELY=1", "text/plain"),
54                 ("install", "make install", "text/plain"),
55                 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
56                 ("clean", "make clean", "text/plain") ],
57
58     "samba-ctdb" : [ ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
59
60                      # make sure we have tdb around:
61                      ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
62                      ("tdb-make", "cd lib/tdb && make", "text/plain"),
63                      ("tdb-install", "cd lib/tdb && make install", "text/plain"),
64
65
66                      # build samba with cluster support (also building ctdb):
67                      ("samba-configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=${PREFIX_DIR}/lib/pkgconfig:${PKG_CONFIG_PATH} ./configure.developer --picky-developer ${PREFIX} --with-selftest-prefix=./bin/ab --with-cluster-support --bundled-libraries=!tdb", "text/plain"),
68                      ("samba-make", "make", "text/plain"),
69                      ("samba-check", "./bin/smbd -b | grep CLUSTER_SUPPORT", "text/plain"),
70                      ("samba-install", "make install", "text/plain"),
71                      ("ctdb-check", "test -e ${PREFIX_DIR}/sbin/ctdbd", "text/plain"),
72
73                      # clean up:
74                      ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
75                      ("clean", "make clean", "text/plain"),
76                      ("ctdb-clean", "cd ./ctdb && make clean", "text/plain") ],
77
78     "samba-libs" : [
79                       ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
80                       ("talloc-configure", "cd lib/talloc && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
81                       ("talloc-make", "cd lib/talloc && make", "text/plain"),
82                       ("talloc-install", "cd lib/talloc && make install", "text/plain"),
83
84                       ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
85                       ("tdb-make", "cd lib/tdb && make", "text/plain"),
86                       ("tdb-install", "cd lib/tdb && make install", "text/plain"),
87
88                       ("tevent-configure", "cd lib/tevent && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
89                       ("tevent-make", "cd lib/tevent && make", "text/plain"),
90                       ("tevent-install", "cd lib/tevent && make install", "text/plain"),
91
92                       ("ldb-configure", "cd lib/ldb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
93                       ("ldb-make", "cd lib/ldb && make", "text/plain"),
94                       ("ldb-install", "cd lib/ldb && make install", "text/plain"),
95
96                       ("configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=!talloc,!tdb,!pytdb,!ldb,!pyldb,!tevent,!pytevent --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
97                       ("make", "make", "text/plain"),
98                       ("install", "make install", "text/plain"),
99                       ("dist", "make dist", "text/plain")],
100
101     "ldb" : [
102               ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
103               ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
104               ("make", "make", "text/plain"),
105               ("install", "make install", "text/plain"),
106               ("test", "make test", "text/plain"),
107               ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
108               ("distcheck", "make distcheck", "text/plain"),
109               ("clean", "make clean", "text/plain") ],
110
111     "tdb" : [
112               ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
113               ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
114               ("make", "make", "text/plain"),
115               ("install", "make install", "text/plain"),
116               ("test", "make test", "text/plain"),
117               ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
118               ("distcheck", "make distcheck", "text/plain"),
119               ("clean", "make clean", "text/plain") ],
120
121     "talloc" : [
122                  ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
123                  ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
124                  ("make", "make", "text/plain"),
125                  ("install", "make install", "text/plain"),
126                  ("test", "make test", "text/plain"),
127                  ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
128                  ("distcheck", "make distcheck", "text/plain"),
129                  ("clean", "make clean", "text/plain") ],
130
131     "replace" : [
132                   ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
133                   ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
134                   ("make", "make", "text/plain"),
135                   ("install", "make install", "text/plain"),
136                   ("test", "make test", "text/plain"),
137                   ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
138                   ("distcheck", "make distcheck", "text/plain"),
139                   ("clean", "make clean", "text/plain") ],
140
141     "tevent" : [
142                  ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
143                  ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
144                  ("make", "make", "text/plain"),
145                  ("install", "make install", "text/plain"),
146                  ("test", "make test", "text/plain"),
147                  ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
148                  ("distcheck", "make distcheck", "text/plain"),
149                  ("clean", "make clean", "text/plain") ],
150
151     "pidl" : [
152                ("random-sleep", "../script/random-sleep.sh 60 600", "text/plain"),
153                ("configure", "perl Makefile.PL PREFIX=${PREFIX_DIR}", "text/plain"),
154                ("touch", "touch *.yp", "text/plain"),
155                ("make", "make", "text/plain"),
156                ("test", "make test", "text/plain"),
157                ("install", "make install", "text/plain"),
158                ("checkout-yapp-generated", "git checkout lib/Parse/Pidl/IDL.pm lib/Parse/Pidl/Expr.pm", "text/plain"),
159                ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
160                ("clean", "make clean", "text/plain") ],
161
162     # these are useful for debugging autobuild
163     'pass' : [ ("pass", 'echo passing && /bin/true', "text/plain") ],
164     'fail' : [ ("fail", 'echo failing && /bin/false', "text/plain") ]
165 }
166
167 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
168     if show is None:
169         show = options.verbose
170     if show:
171         print("Running: '%s' in '%s'" % (cmd, dir))
172     if output:
173         return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
174     elif checkfail:
175         return check_call(cmd, shell=True, cwd=dir)
176     else:
177         return call(cmd, shell=True, cwd=dir)
178
179
180 class builder(object):
181     '''handle build of one directory'''
182
183     def __init__(self, name, sequence):
184         self.name = name
185         self.dir = builddirs[name]
186
187         self.tag = self.name.replace('/', '_')
188         self.sequence = sequence
189         self.next = 0
190         self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
191         self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
192         if options.verbose:
193             print("stdout for %s in %s" % (self.name, self.stdout_path))
194             print("stderr for %s in %s" % (self.name, self.stderr_path))
195         run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
196         self.stdout = open(self.stdout_path, 'w')
197         self.stderr = open(self.stderr_path, 'w')
198         self.stdin  = open("/dev/null", 'r')
199         self.sdir = "%s/%s" % (testbase, self.tag)
200         self.prefix = "%s/prefix/%s" % (testbase, self.tag)
201         run_cmd("rm -rf %s" % self.sdir)
202         cleanup_list.append(self.sdir)
203         cleanup_list.append(self.prefix)
204         os.makedirs(self.sdir)
205         run_cmd("rm -rf %s" % self.sdir)
206         run_cmd("git clone --recursive --shared %s %s" % (test_master, self.sdir), dir=test_master, show=True)
207         self.start_next()
208
209     def start_next(self):
210         if self.next == len(self.sequence):
211             print '%s: Completed OK' % self.name
212             self.done = True
213             return
214         (self.stage, self.cmd, self.output_mime_type) = self.sequence[self.next]
215         self.cmd = self.cmd.replace("${PYTHON_PREFIX}", get_python_lib(standard_lib=1, prefix=self.prefix))
216         self.cmd = self.cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
217         self.cmd = self.cmd.replace("${PREFIX_DIR}", "%s" % self.prefix)
218 #        if self.output_mime_type == "text/x-subunit":
219 #            self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
220         print '%s: [%s] Running %s' % (self.name, self.stage, self.cmd)
221         cwd = os.getcwd()
222         os.chdir("%s/%s" % (self.sdir, self.dir))
223         self.proc = Popen(self.cmd, shell=True,
224                           stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
225         os.chdir(cwd)
226         self.next += 1
227
228
229 class buildlist(object):
230     '''handle build of multiple directories'''
231
232     def __init__(self, tasklist, tasknames, rebase_url, rebase_branch="master"):
233         global tasks
234         self.tlist = []
235         self.tail_proc = None
236         self.retry = None
237         if tasknames == []:
238             tasknames = defaulttasks
239         else:
240             # If we are only running one test,
241             # do not sleep randomly to wait for it to start
242             os.environ['AUTOBUILD_RANDOM_SLEEP_OVERRIDE'] = '1'
243
244         for n in tasknames:
245             b = builder(n, tasks[n])
246             self.tlist.append(b)
247         if options.retry:
248             rebase_remote = "rebaseon"
249             retry_task = [ ("retry",
250                             '''set -e
251                             git remote add -t %s %s %s
252                             git fetch %s
253                             while :; do
254                               sleep 60
255                               git describe %s/%s > old_remote_branch.desc
256                               git fetch %s
257                               git describe %s/%s > remote_branch.desc
258                               diff old_remote_branch.desc remote_branch.desc
259                             done
260                            ''' % (
261                                rebase_branch, rebase_remote, rebase_url,
262                                rebase_remote,
263                                rebase_remote, rebase_branch,
264                                rebase_remote,
265                                rebase_remote, rebase_branch
266                            ),
267                            "test/plain" ) ]
268
269             self.retry = builder('retry', retry_task)
270             self.need_retry = False
271
272     def kill_kids(self):
273         if self.tail_proc is not None:
274             self.tail_proc.terminate()
275             self.tail_proc.wait()
276             self.tail_proc = None
277         if self.retry is not None:
278             self.retry.proc.terminate()
279             self.retry.proc.wait()
280             self.retry = None
281         for b in self.tlist:
282             if b.proc is not None:
283                 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
284                 b.proc.terminate()
285                 b.proc.wait()
286                 b.proc = None
287
288     def wait_one(self):
289         while True:
290             none_running = True
291             for b in self.tlist:
292                 if b.proc is None:
293                     continue
294                 none_running = False
295                 b.status = b.proc.poll()
296                 if b.status is None:
297                     continue
298                 b.proc = None
299                 return b
300             if options.retry:
301                 ret = self.retry.proc.poll()
302                 if ret is not None:
303                     self.need_retry = True
304                     self.retry = None
305                     return None
306             if none_running:
307                 return None
308             time.sleep(0.1)
309
310     def run(self):
311         while True:
312             b = self.wait_one()
313             if options.retry and self.need_retry:
314                 self.kill_kids()
315                 print("retry needed")
316                 return (0, None, None, None, "retry")
317             if b is None:
318                 break
319             if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
320                 self.kill_kids()
321                 return (b.status, b.name, b.stage, b.tag, "%s: [%s] failed '%s' with status %d" % (b.name, b.stage, b.cmd, b.status))
322             b.start_next()
323         self.kill_kids()
324         return (0, None, None, None, "All OK")
325
326     def tarlogs(self, fname):
327         tar = tarfile.open(fname, "w:gz")
328         for b in self.tlist:
329             tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
330             tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
331         if os.path.exists("autobuild.log"):
332             tar.add("autobuild.log")
333         tar.close()
334
335     def remove_logs(self):
336         for b in self.tlist:
337             os.unlink(b.stdout_path)
338             os.unlink(b.stderr_path)
339
340     def start_tail(self):
341         cwd = os.getcwd()
342         cmd = "tail -f *.stdout *.stderr"
343         os.chdir(gitroot)
344         self.tail_proc = Popen(cmd, shell=True)
345         os.chdir(cwd)
346
347
348 def cleanup():
349     if options.nocleanup:
350         return
351     print("Cleaning up ....")
352     for d in cleanup_list:
353         run_cmd("rm -rf %s" % d)
354
355
356 def find_git_root():
357     '''get to the top of the git repo'''
358     p=os.getcwd()
359     while p != '/':
360         if os.path.isdir(os.path.join(p, ".git")):
361             return p
362         p = os.path.abspath(os.path.join(p, '..'))
363     return None
364
365
366 def daemonize(logfile):
367     pid = os.fork()
368     if pid == 0: # Parent
369         os.setsid()
370         pid = os.fork()
371         if pid != 0: # Actual daemon
372             os._exit(0)
373     else: # Grandparent
374         os._exit(0)
375
376     import resource      # Resource usage information.
377     maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
378     if maxfd == resource.RLIM_INFINITY:
379         maxfd = 1024 # Rough guess at maximum number of open file descriptors.
380     for fd in range(0, maxfd):
381         try:
382             os.close(fd)
383         except OSError:
384             pass
385     os.open(logfile, os.O_RDWR | os.O_CREAT)
386     os.dup2(0, 1)
387     os.dup2(0, 2)
388
389 def write_pidfile(fname):
390     '''write a pid file, cleanup on exit'''
391     f = open(fname, mode='w')
392     f.write("%u\n" % os.getpid())
393     f.close()
394
395
396 def rebase_tree(rebase_url, rebase_branch = "master"):
397     rebase_remote = "rebaseon"
398     print("Rebasing on %s" % rebase_url)
399     run_cmd("git describe HEAD", show=True, dir=test_master)
400     run_cmd("git remote add -t %s %s %s" %
401             (rebase_branch, rebase_remote, rebase_url),
402             show=True, dir=test_master)
403     run_cmd("git fetch %s" % rebase_remote, show=True, dir=test_master)
404     if options.fix_whitespace:
405         run_cmd("git rebase --force-rebase --whitespace=fix %s/%s" %
406                 (rebase_remote, rebase_branch),
407                 show=True, dir=test_master)
408     else:
409         run_cmd("git rebase --force-rebase %s/%s" %
410                 (rebase_remote, rebase_branch),
411                 show=True, dir=test_master)
412     diff = run_cmd("git --no-pager diff HEAD %s/%s" %
413                    (rebase_remote, rebase_branch),
414                    dir=test_master, output=True)
415     if diff == '':
416         print("No differences between HEAD and %s/%s - exiting" %
417               (rebase_remote, rebase_branch))
418         sys.exit(0)
419     run_cmd("git describe %s/%s" %
420             (rebase_remote, rebase_branch),
421             show=True, dir=test_master)
422     run_cmd("git describe HEAD", show=True, dir=test_master)
423     run_cmd("git --no-pager diff --stat HEAD %s/%s" %
424             (rebase_remote, rebase_branch),
425             show=True, dir=test_master)
426
427 def push_to(push_url, push_branch = "master"):
428     push_remote = "pushto"
429     print("Pushing to %s" % push_url)
430     if options.mark:
431         run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master)
432         run_cmd("git commit --amend -c HEAD", dir=test_master)
433         # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
434         # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
435     run_cmd("git remote add -t %s %s %s" %
436             (push_branch, push_remote, push_url),
437             show=True, dir=test_master)
438     run_cmd("git push %s +HEAD:%s" %
439             (push_remote, push_branch),
440             show=True, dir=test_master)
441
442 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
443
444 gitroot = find_git_root()
445 if gitroot is None:
446     raise Exception("Failed to find git root")
447
448 parser = OptionParser()
449 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
450 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
451 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
452 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
453                   default=def_testbase)
454 parser.add_option("", "--passcmd", help="command to run on success", default=None)
455 parser.add_option("", "--verbose", help="show all commands as they are run",
456                   default=False, action="store_true")
457 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
458                   default=None, type='str')
459 parser.add_option("", "--pushto", help="push to a git url on success",
460                   default=None, type='str')
461 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
462                   default=False, action="store_true")
463 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
464                   default=False, action="store_true")
465 parser.add_option("", "--retry", help="automatically retry if master changes",
466                   default=False, action="store_true")
467 parser.add_option("", "--email", help="send email to the given address on failure",
468                   type='str', default=None)
469 parser.add_option("", "--email-from", help="send email from the given address",
470                   type='str', default="autobuild@samba.org")
471 parser.add_option("", "--email-server", help="send email via the given server",
472                   type='str', default='localhost')
473 parser.add_option("", "--always-email", help="always send email, even on success",
474                   action="store_true")
475 parser.add_option("", "--daemon", help="daemonize after initial setup",
476                   action="store_true")
477 parser.add_option("", "--branch", help="the branch to work on (default=master)",
478                   default="master", type='str')
479 parser.add_option("", "--log-base", help="location where the logs can be found (default=cwd)",
480                   default=gitroot, type='str')
481 parser.add_option("", "--attach-logs", help="Attach logs to mails sent on success/failure?",
482                   default=False, action="store_true")
483
484 def send_email(subject, text, log_tar):
485     outer = MIMEMultipart()
486     outer['Subject'] = subject
487     outer['To'] = options.email
488     outer['From'] = options.email_from
489     outer['Date'] = email.utils.formatdate(localtime = True)
490     outer.preamble = 'Autobuild mails are now in MIME because we optionally attach the logs.\n'
491     outer.attach(MIMEText(text, 'plain'))
492     if options.attach_logs:
493         fp = open(log_tar, 'rb')
494         msg = MIMEApplication(fp.read(), 'gzip', email.encoders.encode_base64)
495         fp.close()
496         # Set the filename parameter
497         msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(log_tar))
498         outer.attach(msg)
499     content = outer.as_string()
500     s = smtplib.SMTP(options.email_server)
501     s.sendmail(options.email_from, [options.email], content)
502     s.set_debuglevel(1)
503     s.quit()
504
505 def email_failure(status, failed_task, failed_stage, failed_tag, errstr, log_base=None):
506     '''send an email to options.email about the failure'''
507     user = os.getenv("USER")
508     if log_base is None:
509         log_base = gitroot
510     text = '''
511 Dear Developer,
512
513 Your autobuild on %s failed when trying to test %s with the following error:
514    %s
515
516 the autobuild has been abandoned. Please fix the error and resubmit.
517
518 A summary of the autobuild process is here:
519
520   %s/autobuild.log
521 ''' % (platform.node(), failed_task, errstr, log_base)
522     
523     if failed_task != 'rebase':
524         text += '''
525 You can see logs of the failed task here:
526
527   %s/%s.stdout
528   %s/%s.stderr
529
530 or you can get full logs of all tasks in this job here:
531
532   %s/logs.tar.gz
533
534 The top commit for the tree that was built was:
535
536 %s
537
538 ''' % (log_base, failed_tag, log_base, failed_tag, log_base, top_commit_msg)
539
540     logs = os.path.join(gitroot, 'logs.tar.gz')
541     send_email('autobuild failure on %s for task %s during %s'
542                % (platform.node(), failed_task, failed_stage),
543                text, logs)
544
545 def email_success(log_base=None):
546     '''send an email to options.email about a successful build'''
547     user = os.getenv("USER")
548     if log_base is None:
549         log_base = gitroot
550     text = '''
551 Dear Developer,
552
553 Your autobuild on %s has succeeded.
554
555 ''' % platform.node()
556
557     if options.keeplogs:
558         text += '''
559
560 you can get full logs of all tasks in this job here:
561
562   %s/logs.tar.gz
563
564 ''' % log_base
565
566     text += '''
567 The top commit for the tree that was built was:
568
569 %s
570 ''' % top_commit_msg
571
572     logs = os.path.join(gitroot, 'logs.tar.gz')
573     send_email('autobuild sucess on %s ' % platform.node(),
574                text, logs)
575
576
577 (options, args) = parser.parse_args()
578
579 if options.retry:
580     if options.rebase is None:
581         raise Exception('You can only use --retry if you also rebase')
582
583 testbase = "%s/b%u" % (options.testbase, os.getpid())
584 test_master = "%s/master" % testbase
585
586 # get the top commit message, for emails
587 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
588
589 try:
590     os.makedirs(testbase)
591 except Exception, reason:
592     raise Exception("Unable to create %s : %s" % (testbase, reason))
593 cleanup_list.append(testbase)
594
595 if options.daemon:
596     logfile = os.path.join(testbase, "log")
597     print "Forking into the background, writing progress to %s" % logfile
598     daemonize(logfile)
599
600 write_pidfile(gitroot + "/autobuild.pid")
601
602 while True:
603     try:
604         run_cmd("rm -rf %s" % test_master)
605         cleanup_list.append(test_master)
606         run_cmd("git clone --recursive --shared %s %s" % (gitroot, test_master), show=True, dir=gitroot)
607     except Exception:
608         cleanup()
609         raise
610
611     try:
612         try:
613             if options.rebase is not None:
614                 rebase_tree(options.rebase, rebase_branch=options.branch)
615         except Exception:
616             cleanup_list.append(gitroot + "/autobuild.pid")
617             cleanup()
618             email_failure(-1, 'rebase', 'rebase', 'rebase',
619                           'rebase on %s failed' % options.branch,
620                           log_base=options.log_base)
621             sys.exit(1)
622         blist = buildlist(tasks, args, options.rebase, rebase_branch=options.branch)
623         if options.tail:
624             blist.start_tail()
625         (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
626         if status != 0 or errstr != "retry":
627             break
628         cleanup()
629     except Exception:
630         cleanup()
631         raise
632
633 cleanup_list.append(gitroot + "/autobuild.pid")
634
635 blist.kill_kids()
636 if options.tail:
637     print("waiting for tail to flush")
638     time.sleep(1)
639
640 if status == 0:
641     print errstr
642     if options.passcmd is not None:
643         print("Running passcmd: %s" % options.passcmd)
644         run_cmd(options.passcmd, dir=test_master)
645     if options.pushto is not None:
646         push_to(options.pushto, push_branch=options.branch)
647     if options.keeplogs or options.attach_logs:
648         blist.tarlogs("logs.tar.gz")
649         print("Logs in logs.tar.gz")
650     if options.always_email:
651         email_success(log_base=options.log_base)
652     blist.remove_logs()
653     cleanup()
654     print(errstr)
655     sys.exit(0)
656
657 # something failed, gather a tar of the logs
658 blist.tarlogs("logs.tar.gz")
659
660 if options.email is not None:
661     email_failure(status, failed_task, failed_stage, failed_tag, errstr, log_base=options.log_base)
662
663 cleanup()
664 print(errstr)
665 print("Logs in logs.tar.gz")
666 sys.exit(status)