def34282b1a48fedd07636da94afe1752c206df0
[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     )
17 from email.mime.text import MIMEText
18 import logging
19 import optparse
20 import smtplib
21
22 parser = optparse.OptionParser("import-and-analyse [options]")
23 parser.add_option("--dry-run", help="Will cause the script to send output to stdout instead of to sendmail.", action="store_true")
24 parser.add_option("--verbose", help="Be verbose", action="count")
25
26 (opts, args) = parser.parse_args()
27
28 # we open readonly here as only apache(www-run) has write access
29 buildfarm = BuildFarm()
30
31 smtp = smtplib.SMTP()
32 smtp.connect()
33
34 def check_and_send_mails(tree, host, compiler, cur, old):
35     t = buildfarm.trees[tree]
36
37     (cur_rev, cur_rev_timestamp) = cur.revision_details()
38     cur_status = cur.status()
39
40     (old_rev, old_rev_timestamp) = old.revision_details()
41     old_status = old.status()
42
43     if not cur_status.regressed_since(old_status):
44         if opts.verbose:
45             print "the build didn't get worse since %r" % old_status
46         return
47
48     recipients = set()
49     change_log = ""
50
51     for rev in t.get_branch().log(from_rev=cur.rev, exclude_revs=set([old.rev])):
52         recipients.add(rev.author)
53         recipients.add(rev.committer)
54         change_log += """
55 revision: %s
56 author: %s
57 committer: %s
58 message:
59     %s
60 """ % (rev.revision, rev.author, rev.committer, rev.message)
61
62     body = """
63 Broken build for tree %(tree)s on host %(host)s with compiler %(compiler)s
64
65 Tree %(tree)s is %(scm)s branch %(branch)s.
66
67 Build status for new revision %(cur_rev)s is %(cur_status)s
68 Build status for old revision %(old_rev)s was %(old_status)s
69
70 See http://build.samba.org/?function=View+Build;host=%(host)s;tree=%(tree)s;compiler=%(compiler)s
71
72 The build may have been broken by one of the following commits:
73
74 %(change_log)s
75     """ % {"tree": tree, "host": host, "compiler": compiler, "change_log": change_log, "scm": t.scm, "branch": t.branch,
76             "cur_rev": cur_rev, "old_rev": old_rev, "cur_status": cur_status, "old_status": old_status }
77
78     msg = MIMEText(body)
79     msg["Subject"] = "BUILD of %s:%s BROKEN on %s with %s AT REVISION %s" % (tree, t.branch, host, compiler, cur_rev)
80     msg["From"] = "\"Build Farm\" <build@samba.org>"
81     msg["To"] = ",".join(recipients.keys())
82     if not opts.dry_run:
83         smtp.send(msg["From"], [msg["To"]], msg.as_string())
84
85
86 for build in buildfarm.get_new_builds():
87     if opts.verbose >= 1:
88         print "Processing %s..." % build,
89
90     if not opts.dry_run:
91         buildfarm.builds.upload_build(build)
92
93     (rev, commit_rev, rev_timestamp) = build.revision_details()
94
95     if opts.verbose >= 1:
96         print str(build.status())
97
98     try:
99         prev_rev = buildfarm.builds.get_previous_revision(build.tree, build.host, build.compiler, rev)
100     except data.NoSuchBuildError:
101         # Can't send a nastygram until there are 2 builds..
102         continue
103     else:
104         prev_build = buildfarm.get_build(build.tree, build.host, build.compiler, prev_rev)
105         check_and_send_mails(build.tree, build.host, build.compiler, build, prev_build)
106
107     if not opts.dry_run:
108         build.remove()
109
110 smtp.quit()