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 hosts = hostsdb.hosts()
49 self.change_log = None
50 self.committers = set()
52 self.recipients = None
55 def get_log_git(tree, cur, old):
56 cmd = "cd %s/%s && git log --pretty=full %s..%s ./" % (UNPACKED_DIR, tree, old, cur)
60 log.change_log = commands.getoutput(cmd)
63 # get the list of possible culprits
66 for m in re.findall("[\n]*Author: [^<]*<([^>]+)>\nCommit: [^<]*<([^>]+)>\n(.*)$", log.change_log):
68 committer = m.group(2)
70 # handle cherry-picks from svnmirror repo
71 author = author.replace("0c0555d6-39d7-0310-84fc-f1cc0bd64818", "samba.org")
73 # for now only send reports to samba.org addresses.
74 if not "@samba.org" in author:
78 log.authors.add(author)
80 log.committers.add(committer)
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)
86 all.update(log.authors)
87 all.update(log.committers)
92 def get_log(tree, cur, old):
93 treedir = os.path.join(UNPACKED_DIR, tree)
95 if os.path.exists(os.path.join(treedir, ".git")):
96 return get_log_git(tree, cur, old)
98 raise Exception("Unknown vcs for %s" % treedir)
101 def check_and_send_mails(tree, host, compiler, cur, old):
102 t = buildfarm.trees[tree]
104 (cur_rev, cur_rev_timestamp) = cur.revision_details()
105 cur_status = cur.status()
107 (old_rev, old_rev_timestamp) = old.revision_details()
108 old_status = old.status()
111 print "rev=%s status=%s" % (cur_rev, cur_status)
112 print "old rev=%s status=%s" % (old_rev, old_status)
114 if not cur_status.regressed_since(old_status):
116 print "the build didn't get worse since %r" % old_status
119 log = get_log(tree, cur, old)
125 recipients = ",".join(log.recipients.keys())
128 Broken build for tree %(tree)s on host %(host)s with compiler %(compiler)s
130 Tree %(tree)s is %(scm)s branch %(branch)s.
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
135 See http://build.samba.org/?function=View+Build;host=%(host)s;tree=%(tree)s;compiler=%(compiler)s
137 The build may have been broken by one of the following commits:
140 """ % {"tree": tree, "host": host, "compiler": compiler, "change_log": log.change_log, "scm": t.scm, "branch": t.branch}
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())
150 for tree in buildfarm.trees:
151 for compiler in buildfarm.compilers:
153 if opts.verbose >= 2:
154 print "Looking for a log file for %s %s %s..." % (host, compiler, tree)
156 # By building the log file name this way, using only the list of
157 # hosts, trees and compilers as input, we ensure we
160 build = db.get_build(host, tree, compiler)
161 except data.NoSuchBuildError:
164 if opts.verbose >= 2:
165 print "Processing %s..." % build
167 db.upload_build(build)
169 (rev, commit_rev, rev_timestamp) = db.revision_details()
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..
177 prev_build = db.get_build(tree, host, compiler, prev_rev)
178 check_and_send_mails(tree, host, compiler, build.status(), prev_build.status())