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