bisect: more bisection options
[sfrench/samba-autobuild/.git] / script / bisect-test.py
1 #!/usr/bin/env python
2 # use git bisect to work out what commit caused a test failure
3 # Copyright Andrew Tridgell 2010
4 # released under GNU GPL v3 or later
5
6
7 from subprocess import call, check_call, Popen, PIPE
8 import os, tempfile, sys
9 from optparse import OptionParser
10
11 parser = OptionParser()
12 parser.add_option("", "--tests", help="list of tests to run", default='*')
13 parser.add_option("", "--quick", help="use make quicktest", default='')
14 parser.add_option("", "--good", help="known good revision (default HEAD~100)", default='HEAD~100')
15 parser.add_option("", "--bad", help="known bad revision (default HEAD)", default='HEAD')
16 parser.add_option("", "--skip-build-errors", help="skip revision where make fails",
17                   action='store_true', default=False)
18 parser.add_option("", "--autogen", help="run autogen before each build",action="store_true", default=False)
19 parser.add_option("", "--configure", help="run configure.developer before each build",
20     action="store_true", default=False)
21 parser.add_option("", "--clean", help="run make clean before each build",
22     action="store_true", default=False)
23 parser.add_option("-j", "", help="use make -j N", dest='N', type='int', action="store", default=1)
24
25 (opts, args) = parser.parse_args()
26
27
28 def run_cmd(cmd, dir=".", show=True, output=False, checkfail=True):
29     if show:
30         print("Running: '%s' in '%s'" % (cmd, dir))
31     if output:
32         return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
33     elif checkfail:
34         return check_call(cmd, shell=True, cwd=dir)
35     else:
36         return call(cmd, shell=True, cwd=dir)
37
38 def find_git_root():
39     '''get to the top of the git repo'''
40     p=os.getcwd()
41     while p != '/':
42         if os.path.isdir(os.path.join(p, ".git")):
43             return p
44         p = os.path.abspath(os.path.join(p, '..'))
45     return None
46
47 cwd = os.getcwd()
48 gitroot = find_git_root()
49
50 # create a bisect script
51 f = tempfile.NamedTemporaryFile(delete=False)
52 f.write("set -x\n")
53 f.write("cd %s || exit 125\n" % cwd)
54 if opts.autogen:
55     f.write("./autogen.sh || exit 125\n")
56 if opts.configure:
57     f.write("./configure.developer || exit 125\n")
58 if opts.clean:
59     f.write("make clean || exit 125\n")
60 if opts.skip_build_errors:
61     f.write("make -j %u || exit 125\n" % opts.N)
62 else:
63     f.write("make -j %u || exit 1\n" % opts.N)
64 if opts.quick:
65     target="quicktest"
66 else:
67     target="test"
68 f.write("make -j %u %s TESTS='%s' FAIL_IMMEDIATELY=1 || exit 1\n" % (opts.N, target, opts.tests))
69 f.write("exit 0\n")
70 f.close()
71
72 def cleanup():
73     run_cmd("git bisect reset", dir=gitroot)
74     os.unlink(f.name)
75     sys.exit(-1)
76
77 # run bisect
78 ret = -1
79 try:
80     run_cmd("git bisect reset", dir=gitroot, show=False, checkfail=False)
81     run_cmd("git bisect start %s %s --" % (opts.bad, opts.good), dir=gitroot)
82     ret = run_cmd("git bisect run bash %s" % f.name, dir=gitroot, show=True, checkfail=False)
83 except KeyboardInterrupt:
84     print("Cleaning up")
85     cleanup()
86 except Exception, reason:
87     print("Failed bisect: %s" % reason)
88     cleanup()
89
90 os.unlink(f.name)
91 sys.exit(ret)