68ad09a8951c1436ae388ea0bc7dc1aa39bd9a45
[build-farm.git] / import-and-analyse.py
1 #!/usr/bin/python
2 # Write sqlite entries for test reports in the build farm
3 # Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
4 # Copyright (C) 2007-2010 Andrew Bartlett <abartlet@samba.org>
5 # Published under the GNU GPL
6
7 """Script to parse build farm log files from the data directory, import
8 them into the database, add links to the oldrevs/ directory and send
9 some mail chastising the possible culprits when the build fails, based
10 on recent commits.
11 """
12
13 from buildfarm import (
14     BuildFarm,
15     data,
16     hostdb,
17     )
18 import commands
19 from email.mime.text import MIMEText
20 import logging
21 import optparse
22 import os
23 import re
24 import smtplib
25
26 dry_run = True
27
28 parser = optparse.OptionParser("import-and-analyse [options]")
29 parser.add_option("--dry-run", help="Will cause the script to send output to stdout instead of to sendmail.", action="store_true")
30 parser.add_option("--verbose", help="Be verbose", action="count")
31
32 (opts, args) = parser.parse_args()
33
34 UNPACKED_DIR = "/home/ftp/pub/unpacked"
35
36 # we open readonly here as only apache(www-run) has write access
37 buildfarm = BuildFarm()
38 db = data.BuildResultStore(os.path.abspath(os.path.dirname(__file__)), True)
39 hostsdb = buildfarm.hostdb
40
41 hosts = hostsdb.hosts()
42
43 smtp = smtplib.SMTP()
44 smtp.connect()
45
46 class Log(object):
47
48     def __init__(self):
49         self.change_log = None
50         self.committers = set()
51         self.authors = set()
52         self.recipients = None
53
54
55 def get_log_git(tree, cur, old):
56     cmd = "cd %s/%s && git log --pretty=full %s..%s ./" % (UNPACKED_DIR, tree, old, cur)
57
58     log = Log()
59
60     log.change_log = commands.getoutput(cmd)
61     #print log.change_log
62
63     # get the list of possible culprits
64     log2 = log.change_log
65
66     for m in re.findall("[\n]*Author: [^<]*<([^>]+)>\nCommit: [^<]*<([^>]+)>\n(.*)$", log.change_log):
67         author = m.group(1)
68         committer = m.group(2)
69
70         # handle cherry-picks from svnmirror repo
71         author = author.replace("0c0555d6-39d7-0310-84fc-f1cc0bd64818", "samba.org")
72
73         # for now only send reports to samba.org addresses.
74         if not "@samba.org" in author:
75             author = None
76
77         if author:
78             log.authors.add(author)
79         if committer:
80             log.committers.add(committer)
81
82     # Add a URL to the diffs for each change
83     log.change_log = re.sub("([\n]*commit ([0-9a-f]+))", "\\1\nhttp:\/\/build.samba.org\/?function=diff;tree=%s;revision=\\2" % tree, log.change_log)
84
85     all = set()
86     all.update(log.authors)
87     all.update(log.committers)
88     log.recipients = all
89     return log
90
91
92 def get_log(tree, cur, old):
93     treedir = os.path.join(UNPACKED_DIR, tree)
94
95     if os.path.exists(os.path.join(treedir, ".git")):
96         return get_log_git(tree, cur, old)
97     else:
98         raise Exception("Unknown vcs for %s" % treedir)
99
100
101 def check_and_send_mails(tree, host, compiler, cur, old):
102     t = buildfarm.trees[tree]
103
104     (cur_rev, cur_rev_timestamp) = cur.revision_details()
105     cur_status = cur.status()
106
107     (old_rev, old_rev_timestamp) = old.revision_details()
108     old_status = old.status()
109
110     if dry_run:
111         print "rev=%s status=%s" % (cur_rev, cur_status)
112         print "old rev=%s status=%s" % (old_rev, old_status)
113
114     if not cur_status.regressed_since(old_status):
115         if dry_run:
116             print "the build didn't get worse since %r" % old_status
117         return
118
119     log = get_log(tree, cur, old)
120     if not log:
121         if dry_run:
122             print "no log"
123         return
124
125     recipients = ",".join(log.recipients.keys())
126
127     body = """
128 Broken build for tree %(tree)s on host %(host)s with compiler %(compiler)s
129
130 Tree %(tree)s is %(scm)s branch %(branch)s.
131
132 Build status for new revision %(cur_rev)s is %(cur_status)s
133 Build status for old revision %(old_rev)s was %(old_status)s
134
135 See http://build.samba.org/?function=View+Build;host=%(host)s;tree=%(tree)s;compiler=%(compiler)s
136
137 The build may have been broken by one of the following commits:
138
139 %(change_log)s
140     """ % {"tree": tree, "host": host, "compiler": compiler, "change_log": log.change_log, "scm": t.scm, "branch": t.branch}
141
142     msg = MIMEText(body)
143     msg["Subject"] = "BUILD of %s:%s BROKEN on %s with %s AT REVISION %s" % (tree, t.branch, host, compiler, cur_rev)
144     msg["From"] = "\"Build Farm\" <build@samba.org>"
145     msg["To"] = recipients
146     smtp.send(msg["From"], [msg["To"]], msg.as_string())
147
148
149 for host in hosts:
150     for tree in buildfarm.trees:
151         for compiler in buildfarm.compilers:
152             retry = 0
153             if opts.verbose >= 2:
154                 print "Looking for a log file for %s %s %s..." % (host, compiler, tree)
155
156             # By building the log file name this way, using only the list of
157             # hosts, trees and compilers as input, we ensure we
158             # control the inputs
159             try:
160                 build = db.get_build(host, tree, compiler)
161             except data.NoSuchBuildError:
162                 continue
163
164             if opts.verbose >= 2:
165                 print "Processing %s..." % build
166
167             db.upload_build(build)
168
169             (rev, commit_rev, rev_timestamp) = db.revision_details()
170
171             try:
172                 prev_rev = db.get_previous_revision(tree, host, compiler, rev)
173             except hostdb.NoSuchBuild:
174                 # Can't send a nastygram until there are 2 builds..
175                 continue
176             else:
177                 prev_build = db.get_build(tree, host, compiler, prev_rev)
178                 check_and_send_mails(tree, host, compiler, build.status(), prev_build.status())
179
180
181 smtp.quit()