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
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
13 from buildfarm import (
19 from email.mime.text import MIMEText
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")
32 (opts, args) = parser.parse_args()
34 UNPACKED_DIR = "/home/ftp/pub/unpacked"
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
41 compilers = buildfarm.compilers
42 hosts = hostsdb.hosts()
51 self.change_log = None
52 self.committers = set()
54 self.recipients = None
57 def get_log_git(tree, cur, old):
58 cmd = "cd %s/%s && git log --pretty=full %s..%s ./" % (UNPACKED_DIR, tree, old, cur)
62 log.change_log = commands.getoutput(cmd)
65 # get the list of possible culprits
68 for m in re.findall("[\n]*Author: [^<]*<([^>]+)>\nCommit: [^<]*<([^>]+)>\n(.*)$", log.change_log):
70 committer = m.group(2)
72 # handle cherry-picks from svnmirror repo
73 author = author.replace("0c0555d6-39d7-0310-84fc-f1cc0bd64818", "samba.org")
75 # for now only send reports to samba.org addresses.
76 if not "@samba.org" in author:
80 log.authors.add(author)
82 log.committers.add(committer)
84 # Add a URL to the diffs for each change
85 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)
88 all.update(log.authors)
89 all.update(log.committers)
94 def get_log(tree, cur, old):
95 treedir = os.path.join(UNPACKED_DIR, tree)
97 if os.path.exists(os.path.join(treedir, ".git")):
98 return get_log_git(tree, cur, old)
100 raise Exception("Unknown vcs for %s" % treedir)
103 def check_and_send_mails(tree, host, compiler, cur, old):
106 (cur_rev, cur_rev_timestamp) = cur.revision_details()
107 cur_status = cur.status()
109 (old_rev, old_rev_timestamp) = old.revision_details()
110 old_status = old.status()
113 print "rev=%s status=%s" % (cur_rev, cur_status)
114 print "old rev=%s status=%s" % (old_rev, old_status)
116 if not cur_status.regressed_since(old_status):
118 print "the build didn't get worse since %r" % old_status
121 log = get_log(tree, cur, old)
127 recipients = ",".join(log.recipients.keys())
130 Broken build for tree %(tree)s on host %(host)s with compiler %(compiler)s
132 Tree %(tree)s is %(scm)s branch %(branch)s.
134 Build status for new revision %(cur_rev)s is %(cur_status)s
135 Build status for old revision %(old_rev)s was %(old_status)s
137 See http://build.samba.org/?function=View+Build;host=%(host)s;tree=%(tree)s;compiler=%(compiler)s
139 The build may have been broken by one of the following commits:
142 """ % {"tree": tree, "host": host, "compiler": compiler, "change_log": log.change_log, "scm": t.scm, "branch": t.branch}
145 msg["Subject"] = "BUILD of %s:%s BROKEN on %s with %s AT REVISION %s" % (tree, t.branch, host, compiler, cur_rev)
146 msg["From"] = "\"Build Farm\" <build@samba.org>"
147 msg["To"] = recipients
148 smtp.send(msg["From"], [msg["To"]], msg.as_string())
153 for compiler in compilers:
155 if opts.verbose >= 2:
156 print "Looking for a log file for %s %s %s..." % (host, compiler, tree)
158 # By building the log file name this way, using only the list of
159 # hosts, trees and compilers as input, we ensure we
162 build = db.get_build(host, tree, compiler)
163 except data.NoSuchBuildError:
166 if opts.verbose >= 2:
167 print "Processing %s..." % build
169 db.upload_build(build)
171 (rev, commit_rev, rev_timestamp) = db.revision_details()
174 prev_rev = db.get_previous_revision(tree, host, compiler, rev)
175 except hostdb.NoSuchBuild:
176 # Can't send a nastygram until there are 2 builds..
179 prev_build = db.get_build(tree, host, compiler, prev_rev)
180 check_and_send_mails(tree, host, compiler, build.status(), prev_build.status())