blob: 1c0d233c336e98727e49a3fca497c10d2f98923a [file] [log] [blame]
mbligh96cf0512008-04-17 15:25:38 +00001#!/usr/bin/python -u
mblighc2514542008-02-19 15:54:26 +00002
mblighb33e53e2008-06-17 19:41:26 +00003import os, sys, optparse, fcntl, errno, traceback, socket
mblighbb7b8912006-10-08 03:59:02 +00004
mbligh96cf0512008-04-17 15:25:38 +00005import common
jadmanski6e8bf752008-05-14 00:17:48 +00006from autotest_lib.client.common_lib import mail, utils
7from autotest_lib.tko import db as tko_db, utils as tko_utils, status_lib
mbligh74fc0462007-11-05 20:24:17 +00008
9
mbligh96cf0512008-04-17 15:25:38 +000010def parse_args():
jadmanski0afbb632008-06-06 21:10:57 +000011 # build up our options parser and parse sys.argv
12 parser = optparse.OptionParser()
13 parser.add_option("-m", help="Send mail for FAILED tests",
14 dest="mailit", action="store_true")
15 parser.add_option("-r", help="Reparse the results of a job",
16 dest="reparse", action="store_true")
17 parser.add_option("-o", help="Parse a single results directory",
18 dest="singledir", action="store_true")
19 parser.add_option("-l", help=("Levels of subdirectories to include "
20 "in the job name"),
21 type="int", dest="level", default=1)
22 parser.add_option("-n", help="No blocking on an existing parse",
23 dest="noblock", action="store_true")
24 parser.add_option("-s", help="Database server hostname",
25 dest="db_host", action="store")
26 parser.add_option("-u", help="Database username", dest="db_user",
27 action="store")
28 parser.add_option("-p", help="Database password", dest="db_pass",
29 action="store")
30 parser.add_option("-d", help="Database name", dest="db_name",
31 action="store")
32 options, args = parser.parse_args()
mbligh74fc0462007-11-05 20:24:17 +000033
jadmanski0afbb632008-06-06 21:10:57 +000034 # we need a results directory
35 if len(args) == 0:
36 tko_utils.dprint("ERROR: at least one results directory must "
37 "be provided")
38 parser.print_help()
39 sys.exit(1)
mbligh74fc0462007-11-05 20:24:17 +000040
jadmanski0afbb632008-06-06 21:10:57 +000041 # pass the options back
42 return options, args
mbligh74fc0462007-11-05 20:24:17 +000043
44
mbligh96cf0512008-04-17 15:25:38 +000045def format_failure_message(jobname, kernel, testname, status, reason):
jadmanski0afbb632008-06-06 21:10:57 +000046 format_string = "%-12s %-20s %-12s %-10s %s"
47 return format_string % (jobname, kernel, testname, status, reason)
mblighb85e6b02006-10-08 17:20:56 +000048
mblighbb7b8912006-10-08 03:59:02 +000049
mbligh96cf0512008-04-17 15:25:38 +000050def mailfailure(jobname, job, message):
jadmanski0afbb632008-06-06 21:10:57 +000051 message_lines = [""]
52 message_lines.append("The following tests FAILED for this job")
53 message_lines.append("http://%s/results/%s" %
54 (socket.gethostname(), jobname))
55 message_lines.append("")
56 message_lines.append(format_failure_message("Job name", "Kernel",
57 "Test name", "FAIL/WARN",
58 "Failure reason"))
59 message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8,
60 "=" * 8, "=" * 14))
61 message_header = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +000062
jadmanski0afbb632008-06-06 21:10:57 +000063 subject = "AUTOTEST: FAILED tests from job %s" % jobname
64 mail.send("", job.user, "", subject, message_header + message)
mbligh006f2302007-09-13 20:46:46 +000065
66
mbligh96cf0512008-04-17 15:25:38 +000067def parse_one(db, jobname, path, reparse, mail_on_failure):
jadmanski0afbb632008-06-06 21:10:57 +000068 """
69 Parse a single job. Optionally send email on failure.
70 """
71 tko_utils.dprint("\nScanning %s (%s)" % (jobname, path))
72 if reparse and db.find_job(jobname):
73 tko_utils.dprint("! Deleting old copy of job results to "
74 "reparse it")
75 db.delete_job(jobname)
76 if db.find_job(jobname):
77 tko_utils.dprint("! Job is already parsed, done")
78 return
mbligh96cf0512008-04-17 15:25:38 +000079
jadmanski0afbb632008-06-06 21:10:57 +000080 # look up the status version
81 try:
82 job_keyval = utils.read_keyval(path)
83 except IOError, e:
84 if e.errno == errno.ENOENT:
85 status_version = 0
86 else:
87 raise
88 else:
89 status_version = job_keyval.get("status_version", 0)
jadmanski6e8bf752008-05-14 00:17:48 +000090
jadmanski0afbb632008-06-06 21:10:57 +000091 # parse out the job
92 parser = status_lib.parser(status_version)
93 job = parser.make_job(path)
94 status_log = os.path.join(path, "status.log")
95 if not os.path.exists(status_log):
96 status_log = os.path.join(path, "status")
97 if not os.path.exists(status_log):
98 tko_utils.dprint("! Unable to parse job, no status file")
99 return
mbligh96cf0512008-04-17 15:25:38 +0000100
jadmanski0afbb632008-06-06 21:10:57 +0000101 # parse the status logs
102 tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname))
103 status_lines = open(status_log).readlines()
104 parser.start(job)
105 tests = parser.end(status_lines)
106 job.tests = tests
mbligh96cf0512008-04-17 15:25:38 +0000107
jadmanski0afbb632008-06-06 21:10:57 +0000108 # check for failures
109 message_lines = [""]
110 for test in job.tests:
111 if not test.subdir:
112 continue
113 tko_utils.dprint("* testname, status, reason: %s %s %s"
114 % (test.subdir, test.status, test.reason))
115 if test.status in ("FAIL", "WARN"):
116 message_lines.append(format_failure_message(
117 jobname, test.kernel.base, test.subdir,
118 test.status, test.reason))
119 message = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +0000120
jadmanski0afbb632008-06-06 21:10:57 +0000121 # send out a email report of failure
122 if len(message) > 2 and mail_on_failure:
123 tko_utils.dprint("Sending email report of failure on %s to %s"
124 % (jobname, job.user))
125 mailfailure(jobname, job, message)
mbligh96cf0512008-04-17 15:25:38 +0000126
jadmanski0afbb632008-06-06 21:10:57 +0000127 # write the job into the database
128 db.insert_job(jobname, job)
129 db.commit()
mbligh26b992b2008-02-19 15:46:21 +0000130
131
mbligh96cf0512008-04-17 15:25:38 +0000132def parse_path(db, path, level, reparse, mail_on_failure):
jadmanski0afbb632008-06-06 21:10:57 +0000133 machine_list = os.path.join(path, ".machines")
134 if os.path.exists(machine_list):
135 # multi-machine job
136 for m in file(machine_list):
137 machine = m.rstrip()
138 if not machine:
139 continue
140 jobpath = os.path.join(path, machine)
141 jobname = "%s/%s" % (os.path.basename(path), machine)
142 try:
143 db.run_with_retry(parse_one, db, jobname,
144 path, reparse,
145 mail_on_failure)
146 except Exception:
147 traceback.print_exc()
148 continue
149 else:
150 # single machine job
151 job_elements = path.split("/")[-level:]
152 jobname = "/".join(job_elements)
153 try:
154 db.run_with_retry(parse_one, db, jobname, path,
155 reparse, mail_on_failure)
156 except Exception:
157 traceback.print_exc()
mblighbb7b8912006-10-08 03:59:02 +0000158
159
mbligh96cf0512008-04-17 15:25:38 +0000160def main():
jadmanski0afbb632008-06-06 21:10:57 +0000161 options, args = parse_args()
162 results_dir = os.path.abspath(args[0])
163 assert os.path.exists(results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000164
jadmanski0afbb632008-06-06 21:10:57 +0000165 # build up the list of job dirs to parse
166 if options.singledir:
167 jobs_list = [results_dir]
168 else:
169 jobs_list = [os.path.join(results_dir, subdir)
170 for subdir in os.listdir(results_dir)]
mbligh96cf0512008-04-17 15:25:38 +0000171
jadmanski0afbb632008-06-06 21:10:57 +0000172 # build up the database
173 db = tko_db.db(autocommit=False, host=options.db_host,
174 user=options.db_user, password=options.db_pass,
175 database=options.db_name)
mbligh96cf0512008-04-17 15:25:38 +0000176
jadmanski0afbb632008-06-06 21:10:57 +0000177 # parse all the jobs
178 for path in jobs_list:
179 lockfile = open(os.path.join(path, ".parse.lock"), "w")
180 flags = fcntl.LOCK_EX
181 if options.noblock:
182 flags != fcntl.LOCK_NB
183 try:
184 fcntl.flock(lockfile, flags)
185 except IOError, e:
186 # was this because the lock is unavailable?
187 if e.errno == errno.EWOULDBLOCK:
188 lockfile.close()
189 continue
190 else:
191 raise # something unexpected happened
192 try:
193 parse_path(db, path, options.level, options.reparse,
194 options.mailit)
195 finally:
196 fcntl.flock(lockfile, fcntl.LOCK_UN)
197 lockfile.close()
mbligh71d340d2008-03-05 15:51:16 +0000198
mbligh532cb272007-11-26 18:54:20 +0000199
mbligh96cf0512008-04-17 15:25:38 +0000200if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000201 main()