blob: 6f8bba9fd8cc307fba71e6c2a8816d0527ee4ed2 [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
jadmanskidb4f9b52008-12-03 22:52:53 +00006from autotest_lib.client.common_lib import mail, pidfile
7from autotest_lib.tko import db as tko_db, utils as tko_utils, status_lib, models
mbligh9e936402009-05-13 20:42:17 +00008from autotest_lib.client.common_lib import utils
mbligh74fc0462007-11-05 20:24:17 +00009
10
mbligh96cf0512008-04-17 15:25:38 +000011def parse_args():
jadmanski0afbb632008-06-06 21:10:57 +000012 # build up our options parser and parse sys.argv
13 parser = optparse.OptionParser()
14 parser.add_option("-m", help="Send mail for FAILED tests",
15 dest="mailit", action="store_true")
16 parser.add_option("-r", help="Reparse the results of a job",
17 dest="reparse", action="store_true")
18 parser.add_option("-o", help="Parse a single results directory",
19 dest="singledir", action="store_true")
20 parser.add_option("-l", help=("Levels of subdirectories to include "
21 "in the job name"),
22 type="int", dest="level", default=1)
23 parser.add_option("-n", help="No blocking on an existing parse",
24 dest="noblock", action="store_true")
25 parser.add_option("-s", help="Database server hostname",
26 dest="db_host", action="store")
27 parser.add_option("-u", help="Database username", dest="db_user",
28 action="store")
29 parser.add_option("-p", help="Database password", dest="db_pass",
30 action="store")
31 parser.add_option("-d", help="Database name", dest="db_name",
32 action="store")
jadmanskid5ab8c52008-12-03 16:27:07 +000033 parser.add_option("--write-pidfile",
34 help="write pidfile (.parser_execute)",
35 dest="write_pidfile", action="store_true",
36 default=False)
jadmanski0afbb632008-06-06 21:10:57 +000037 options, args = parser.parse_args()
mbligh74fc0462007-11-05 20:24:17 +000038
jadmanski0afbb632008-06-06 21:10:57 +000039 # we need a results directory
40 if len(args) == 0:
41 tko_utils.dprint("ERROR: at least one results directory must "
42 "be provided")
43 parser.print_help()
44 sys.exit(1)
mbligh74fc0462007-11-05 20:24:17 +000045
jadmanski0afbb632008-06-06 21:10:57 +000046 # pass the options back
47 return options, args
mbligh74fc0462007-11-05 20:24:17 +000048
49
mbligh96cf0512008-04-17 15:25:38 +000050def format_failure_message(jobname, kernel, testname, status, reason):
jadmanski0afbb632008-06-06 21:10:57 +000051 format_string = "%-12s %-20s %-12s %-10s %s"
52 return format_string % (jobname, kernel, testname, status, reason)
mblighb85e6b02006-10-08 17:20:56 +000053
mblighbb7b8912006-10-08 03:59:02 +000054
mbligh96cf0512008-04-17 15:25:38 +000055def mailfailure(jobname, job, message):
jadmanski0afbb632008-06-06 21:10:57 +000056 message_lines = [""]
57 message_lines.append("The following tests FAILED for this job")
58 message_lines.append("http://%s/results/%s" %
59 (socket.gethostname(), jobname))
60 message_lines.append("")
61 message_lines.append(format_failure_message("Job name", "Kernel",
62 "Test name", "FAIL/WARN",
63 "Failure reason"))
64 message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8,
65 "=" * 8, "=" * 14))
66 message_header = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +000067
jadmanski0afbb632008-06-06 21:10:57 +000068 subject = "AUTOTEST: FAILED tests from job %s" % jobname
69 mail.send("", job.user, "", subject, message_header + message)
mbligh006f2302007-09-13 20:46:46 +000070
71
mbligh96cf0512008-04-17 15:25:38 +000072def parse_one(db, jobname, path, reparse, mail_on_failure):
jadmanski0afbb632008-06-06 21:10:57 +000073 """
74 Parse a single job. Optionally send email on failure.
75 """
76 tko_utils.dprint("\nScanning %s (%s)" % (jobname, path))
jadmanski9b6babf2009-04-21 17:57:40 +000077 old_job_idx = db.find_job(jobname)
showard0fec8a02009-12-04 01:19:54 +000078 # old tests is a dict from tuple (test_name, subdir) to test_idx
79 old_tests = {}
80 if old_job_idx is not None:
81 if not reparse:
82 tko_utils.dprint("! Job is already parsed, done")
83 return
84
showardeab66ce2009-12-23 00:03:56 +000085 raw_old_tests = db.select("test_idx,subdir,test", "tko_tests",
showard0fec8a02009-12-04 01:19:54 +000086 {"job_idx": old_job_idx})
87 if raw_old_tests:
88 old_tests = dict(((test, subdir), test_idx)
89 for test_idx, subdir, test in raw_old_tests)
mbligh96cf0512008-04-17 15:25:38 +000090
jadmanski0afbb632008-06-06 21:10:57 +000091 # look up the status version
jadmanskidb4f9b52008-12-03 22:52:53 +000092 job_keyval = models.job.read_keyval(path)
93 status_version = job_keyval.get("status_version", 0)
jadmanski6e8bf752008-05-14 00:17:48 +000094
jadmanski0afbb632008-06-06 21:10:57 +000095 # parse out the job
96 parser = status_lib.parser(status_version)
97 job = parser.make_job(path)
98 status_log = os.path.join(path, "status.log")
99 if not os.path.exists(status_log):
100 status_log = os.path.join(path, "status")
101 if not os.path.exists(status_log):
102 tko_utils.dprint("! Unable to parse job, no status file")
103 return
mbligh96cf0512008-04-17 15:25:38 +0000104
jadmanski0afbb632008-06-06 21:10:57 +0000105 # parse the status logs
106 tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname))
107 status_lines = open(status_log).readlines()
108 parser.start(job)
109 tests = parser.end(status_lines)
jadmanski9b6babf2009-04-21 17:57:40 +0000110
111 # parser.end can return the same object multiple times, so filter out dups
112 job.tests = []
113 already_added = set()
114 for test in tests:
115 if test not in already_added:
116 already_added.add(test)
117 job.tests.append(test)
118
showard0fec8a02009-12-04 01:19:54 +0000119 # try and port test_idx over from the old tests, but if old tests stop
jadmanski9b6babf2009-04-21 17:57:40 +0000120 # matching up with new ones just give up
showard0fec8a02009-12-04 01:19:54 +0000121 if reparse and old_job_idx is not None:
122 job.index = old_job_idx
123 for test in job.tests:
124 test_idx = old_tests.pop((test.testname, test.subdir), None)
125 if test_idx is not None:
126 test.test_idx = test_idx
127 else:
128 tko_utils.dprint("! Reparse returned new test "
129 "testname=%r subdir=%r" %
130 (test.testname, test.subdir))
131 for test_idx in old_tests.itervalues():
132 where = {'test_idx' : test_idx}
133 db.delete('iteration_result', where)
134 db.delete('iteration_attributes', where)
135 db.delete('test_attributes', where)
136 db.delete('test_labels_tests', {'test_id': test_idx})
137 db.delete('tests', where)
mbligh96cf0512008-04-17 15:25:38 +0000138
jadmanski0afbb632008-06-06 21:10:57 +0000139 # check for failures
140 message_lines = [""]
141 for test in job.tests:
142 if not test.subdir:
143 continue
144 tko_utils.dprint("* testname, status, reason: %s %s %s"
145 % (test.subdir, test.status, test.reason))
146 if test.status in ("FAIL", "WARN"):
147 message_lines.append(format_failure_message(
148 jobname, test.kernel.base, test.subdir,
149 test.status, test.reason))
150 message = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +0000151
jadmanski0afbb632008-06-06 21:10:57 +0000152 # send out a email report of failure
153 if len(message) > 2 and mail_on_failure:
154 tko_utils.dprint("Sending email report of failure on %s to %s"
155 % (jobname, job.user))
156 mailfailure(jobname, job, message)
mbligh96cf0512008-04-17 15:25:38 +0000157
jadmanski0afbb632008-06-06 21:10:57 +0000158 # write the job into the database
159 db.insert_job(jobname, job)
jamesren7a522042010-06-10 22:53:55 +0000160
161 # Serializing job into a binary file
162 try:
163 from autotest_lib.tko import tko_pb2
164 from autotest_lib.tko import job_serializer
165
166 serializer = job_serializer.JobSerializer()
167 serializer.serialize_to_binary(job, os.path.join(path, "job.serialize"))
168 except ImportError:
169 tko_utils.dprint("DEBUG: tko_pb2.py doesn't exist. Create by "
170 "compiling tko/tko.proto.")
171
jadmanski0afbb632008-06-06 21:10:57 +0000172 db.commit()
mbligh26b992b2008-02-19 15:46:21 +0000173
174
jadmanski8e9c2572008-11-11 00:29:02 +0000175def _get_job_subdirs(path):
176 """
177 Returns a list of job subdirectories at path. Returns None if the test
178 is itself a job directory. Does not recurse into the subdirs.
179 """
180 # if there's a .machines file, use it to get the subdirs
jadmanski0afbb632008-06-06 21:10:57 +0000181 machine_list = os.path.join(path, ".machines")
182 if os.path.exists(machine_list):
jadmanski42fbd072009-01-30 15:07:05 +0000183 subdirs = set(line.strip() for line in file(machine_list))
184 existing_subdirs = set(subdir for subdir in subdirs
185 if os.path.exists(os.path.join(path, subdir)))
186 if len(existing_subdirs) != 0:
187 return existing_subdirs
jadmanski8e9c2572008-11-11 00:29:02 +0000188
189 # if this dir contains ONLY subdirectories, return them
190 contents = set(os.listdir(path))
191 contents.discard(".parse.lock")
192 subdirs = set(sub for sub in contents if
193 os.path.isdir(os.path.join(path, sub)))
194 if len(contents) == len(subdirs) != 0:
195 return subdirs
196
197 # this is a job directory, or something else we don't understand
198 return None
199
200
mbligha48eeb22009-03-11 16:44:43 +0000201def parse_leaf_path(db, path, level, reparse, mail_on_failure):
202 job_elements = path.split("/")[-level:]
203 jobname = "/".join(job_elements)
204 try:
205 db.run_with_retry(parse_one, db, jobname, path, reparse,
206 mail_on_failure)
207 except Exception:
208 traceback.print_exc()
209
210
jadmanski8e9c2572008-11-11 00:29:02 +0000211def parse_path(db, path, level, reparse, mail_on_failure):
212 job_subdirs = _get_job_subdirs(path)
213 if job_subdirs is not None:
mbligha48eeb22009-03-11 16:44:43 +0000214 # parse status.log in current directory, if it exists. multi-machine
215 # synchronous server side tests record output in this directory. without
216 # this check, we do not parse these results.
217 if os.path.exists(os.path.join(path, 'status.log')):
218 parse_leaf_path(db, path, level, reparse, mail_on_failure)
jadmanski0afbb632008-06-06 21:10:57 +0000219 # multi-machine job
jadmanski8e9c2572008-11-11 00:29:02 +0000220 for subdir in job_subdirs:
221 jobpath = os.path.join(path, subdir)
222 parse_path(db, jobpath, level + 1, reparse, mail_on_failure)
jadmanski0afbb632008-06-06 21:10:57 +0000223 else:
224 # single machine job
mbligha48eeb22009-03-11 16:44:43 +0000225 parse_leaf_path(db, path, level, reparse, mail_on_failure)
mblighbb7b8912006-10-08 03:59:02 +0000226
227
mbligh96cf0512008-04-17 15:25:38 +0000228def main():
jadmanski0afbb632008-06-06 21:10:57 +0000229 options, args = parse_args()
230 results_dir = os.path.abspath(args[0])
231 assert os.path.exists(results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000232
jadmanskid5ab8c52008-12-03 16:27:07 +0000233 pid_file_manager = pidfile.PidFileManager("parser", results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000234
jadmanskid5ab8c52008-12-03 16:27:07 +0000235 if options.write_pidfile:
236 pid_file_manager.open_file()
mbligh96cf0512008-04-17 15:25:38 +0000237
jadmanskid5ab8c52008-12-03 16:27:07 +0000238 try:
239 # build up the list of job dirs to parse
240 if options.singledir:
241 jobs_list = [results_dir]
242 else:
243 jobs_list = [os.path.join(results_dir, subdir)
244 for subdir in os.listdir(results_dir)]
245
246 # build up the database
247 db = tko_db.db(autocommit=False, host=options.db_host,
248 user=options.db_user, password=options.db_pass,
249 database=options.db_name)
250
251 # parse all the jobs
252 for path in jobs_list:
253 lockfile = open(os.path.join(path, ".parse.lock"), "w")
254 flags = fcntl.LOCK_EX
255 if options.noblock:
mblighdb18b0e2009-01-30 00:34:32 +0000256 flags |= fcntl.LOCK_NB
jadmanskid5ab8c52008-12-03 16:27:07 +0000257 try:
258 fcntl.flock(lockfile, flags)
259 except IOError, e:
mblighdb18b0e2009-01-30 00:34:32 +0000260 # lock is not available and nonblock has been requested
jadmanskid5ab8c52008-12-03 16:27:07 +0000261 if e.errno == errno.EWOULDBLOCK:
262 lockfile.close()
263 continue
264 else:
265 raise # something unexpected happened
266 try:
267 parse_path(db, path, options.level, options.reparse,
268 options.mailit)
mbligh9e936402009-05-13 20:42:17 +0000269
jadmanskid5ab8c52008-12-03 16:27:07 +0000270 finally:
271 fcntl.flock(lockfile, fcntl.LOCK_UN)
jadmanski0afbb632008-06-06 21:10:57 +0000272 lockfile.close()
mblighe97e0e62009-05-21 01:41:58 +0000273
jadmanskid5ab8c52008-12-03 16:27:07 +0000274 except:
275 pid_file_manager.close_file(1)
276 raise
277 else:
278 pid_file_manager.close_file(0)
mbligh71d340d2008-03-05 15:51:16 +0000279
mbligh532cb272007-11-26 18:54:20 +0000280
mbligh96cf0512008-04-17 15:25:38 +0000281if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000282 main()