script: added bisect-test.py git bisect script
[ira/wip.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("", "--good", help="known good revision (default HEAD)", default='HEAD')
14 parser.add_option("", "--bad", help="known bad revision (default HEAD~100)", default='HEAD~100')
15 parser.add_option("", "--skip-build-errors", help="skip revision where make fails", default=False)
16 parser.add_option("", "--autogen", help="run autogen before each build",action="store_true", default=False)
17 parser.add_option("", "--configure", help="run configure.developer before each build",
18     action="store_true", default=False)
19 parser.add_option("", "--clean", help="run make clean before each build",
20     action="store_true", default=False)
21 parser.add_option("-j", "", help="use make -j N", dest='N', type='int', action="store", default=1)
22
23 (opts, args) = parser.parse_args()
24
25
26 def run_cmd(cmd, dir=".", show=True, output=False, checkfail=True):
27     if show:
28         print("Running: '%s' in '%s'" % (cmd, dir))
29     if output:
30         return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
31     elif checkfail:
32         return check_call(cmd, shell=True, cwd=dir)
33     else:
34         return call(cmd, shell=True, cwd=dir)
35
36 def find_git_root():
37     '''get to the top of the git repo'''
38     p=os.getcwd()
39     while p != '/':
40         if os.path.isdir(os.path.join(p, ".git")):
41             return p
42         p = os.path.abspath(os.path.join(p, '..'))
43     return None
44
45 cwd = os.getcwd()
46 gitroot = find_git_root()
47
48 # create a bisect script
49 f = tempfile.NamedTemporaryFile(delete=False)
50 f.write("set -x\n")
51 f.write("cd %s || exit 125\n" % cwd)
52 if opts.autogen:
53     f.write("./autogen.sh || exit 125\n")
54 if opts.configure:
55     f.write("./configure.developer || exit 125\n")
56 if opts.clean:
57     f.write("make clean || exit 125\n")
58 if opts.skip_build_errors:
59     f.write("make -j %u || exit 125\n" % opts.N)
60 else:
61     f.write("make -j %u || exit 1\n" % opts.N)
62 f.write("make -j %u test TESTS='%s' FAIL_IMMEDIATELY=1 || exit 1\n" % (opts.N, opts.tests))
63 f.write("exit 0\n")
64 f.close()
65
66 def cleanup():
67     run_cmd("git bisect reset", dir=gitroot)
68     os.unlink(f.name)
69     sys.exit(-1)
70
71 # run bisect
72 ret = -1
73 try:
74     run_cmd("git bisect reset", dir=gitroot, show=False, checkfail=False)
75     run_cmd("git bisect start %s %s --" % (opts.good, opts.bad), dir=gitroot)
76     ret = run_cmd("git bisect run bash %s" % f.name, dir=gitroot, show=True, checkfail=False)
77 except KeyboardInterrupt:
78     print("Cleaning up")
79     cleanup()
80 except Exception, reason:
81     print("Failed bisect: %s" % reason)
82     cleanup()
83
84 os.unlink(f.name)
85 sys.exit(ret)