blob: 69419a1e52907df7a410d94792d3992e3afe1741 [file] [log] [blame]
mbligh4263b062008-02-21 19:14:43 +00001#!/usr/bin/python -u
mbligh05bb6332008-03-13 15:34:04 +00002import os, re, parse, frontend, db, sys, socket, fcntl
mblighb89c7b32008-01-25 17:37:23 +00003from optparse import OptionParser
mblighbc985702007-11-05 20:52:15 +00004from traceback import format_exception
mbligh74fc0462007-11-05 20:24:17 +00005
mblighbc985702007-11-05 20:52:15 +00006def format_error():
7 t, o, tb = sys.exc_info()
8 trace = format_exception(t, o, tb)
9 # Clear the backtrace to prevent a circular reference
10 # in the heap -- as per tutorial
11 tb = ''
12
13 return ''.join(trace)
14
mblighb89c7b32008-01-25 17:37:23 +000015parser = OptionParser()
16parser.add_option('-m', help='Send mail for FAILED tests', dest='mailit',
17 action='store_true')
18parser.add_option('-r', help='Reparse the results of a job', dest='reparse',
19 action='store_true')
20parser.add_option('-o', help='one: parse a single results directory',
21 dest='singledir', action='store_true')
22parser.add_option('-l', help='levels of subdirectories to include in job name',
23 type='int', dest='level', default=1)
mbligh5a0c9702008-03-11 22:40:55 +000024parser.add_option('-s', help='Database server hostname',
25 dest='db_host', action='store')
26parser.add_option('-u', help='Database username',
27 dest='db_user', action='store')
28parser.add_option('-p', help='Database password',
29 dest='db_pass', action='store')
30parser.add_option('-d', help='Database name',
31 dest='db_name', action='store')
mblighb89c7b32008-01-25 17:37:23 +000032(options, args) = parser.parse_args()
mblighbc985702007-11-05 20:52:15 +000033
mbligh5a0c9702008-03-11 22:40:55 +000034if len(args) == 0:
35 print "ERROR: You need to at least provide a directory to parse\n"
36 parser.print_help()
37 sys.exit(1)
38
mblighb89c7b32008-01-25 17:37:23 +000039dir = os.path.abspath(args[0])
40assert os.path.exists(dir)
mbligh74fc0462007-11-05 20:24:17 +000041
mblighb89c7b32008-01-25 17:37:23 +000042if options.singledir:
mbligh2c05bce2007-11-26 20:15:58 +000043 jobs_list = [dir]
mbligh74fc0462007-11-05 20:24:17 +000044else:
mblighb89c7b32008-01-25 17:37:23 +000045 jobs_list = [os.path.join(dir, subdir) for subdir in os.listdir(dir)]
mblighbb7b8912006-10-08 03:59:02 +000046
mbligh9ec94852007-10-25 15:24:16 +000047debug = True
48
mbligh74fc0462007-11-05 20:24:17 +000049failcc = ""
mbligh9e7c6d02007-11-26 20:59:45 +000050# The user to notify on job failures - TOOD, pull into the config file
mbligh74fc0462007-11-05 20:24:17 +000051notify_user = None
mbligh5a0c9702008-03-11 22:40:55 +000052# do commits transactionally
53db = db.db(autocommit=False, host=options.db_host,
54 user=options.db_user, password=options.db_pass,
55 database=options.db_name)
mbligh056d0d32006-10-08 22:31:10 +000056
mbligh9ec94852007-10-25 15:24:16 +000057
mbligh74fc0462007-11-05 20:24:17 +000058def mailfailure(jobname, job, mesgtxt):
59 # XXX: Need to insert URL here too (frontend.test.url?)
60 link = "http://" + socket.gethostname() + "/results/" + jobname
61
62 # This looks pretty good on fixed-width-font email reader.
mbligh032c2de2007-11-24 19:20:12 +000063 message_header = "\n%s\n%s\n\n%-12s %-20s %-12s %-10s %s\n" % \
64 ("The following tests FAILED for this job:",
65 link, "Job name", "Kernel", "Test name",
66 "FAIL/WARN", "Failure Reason")
67 message_header += "%-12s %-20s %-12s %-10s %s\n" % \
68 ("========", "======", "=========",
69 "=========", "==============")
mbligh74fc0462007-11-05 20:24:17 +000070
71 subject = "AUTOTEST: FAILED tests from " + " job " + jobname
mbligh9e7c6d02007-11-26 20:59:45 +000072 parse.mail(notify_user, job.user, failcc, subject,
mbligh032c2de2007-11-24 19:20:12 +000073 message_header + mesgtxt)
mbligh74fc0462007-11-05 20:24:17 +000074
75
mblighe8b37a92007-12-19 15:54:11 +000076def dprint(string):
77 if debug:
78 print string
79
80
mbligh9ec94852007-10-25 15:24:16 +000081def do_parse(jobname, path):
mbligh0a498cb2007-11-26 17:34:28 +000082 """
83 Parse a single job. Optionally send email on failure, etc.
84 """
mblighe8b37a92007-12-19 15:54:11 +000085 dprint('\nScanning %s (%s)' % (jobname, path))
mblighb89c7b32008-01-25 17:37:23 +000086 if options.reparse and db.find_job(jobname):
mblighe8b37a92007-12-19 15:54:11 +000087 dprint('! Deleting old copy of job results, to reparse it')
88 db.delete_job(jobname)
89 if db.find_job(jobname): # Job has already been parsed
90 dprint('! Already processed')
mbligh9ec94852007-10-25 15:24:16 +000091 return
mbligh532cb272007-11-26 18:54:20 +000092 job = parse.job(path)
mbligh8e1ab172007-09-13 17:29:56 +000093 if not job:
mblighe8b37a92007-12-19 15:54:11 +000094 dprint('! Failed to parse job (no status file?)')
mbligh9ec94852007-10-25 15:24:16 +000095 return
mblighd5c33db2006-10-08 21:34:16 +000096 if not job.kernel:
mblighe8b37a92007-12-19 15:54:11 +000097 dprint('! Failed to find kernel for job')
mbligh9ec94852007-10-25 15:24:16 +000098 return
mblighf54ed2a2007-11-24 19:31:30 +000099 print '+ Parsing ' + path
mbligh74fc0462007-11-05 20:24:17 +0000100 print '* jobname, kernel version: %s %s' % (jobname, job.kernel.base)
101 mesgtxt = "\n"
mblighe9cf9d42007-08-31 08:56:00 +0000102 for test in job.tests:
mbligh74fc0462007-11-05 20:24:17 +0000103 if not test.subdir:
104 continue
mbligh4a2c61e2007-11-12 22:13:11 +0000105 print "* testname, status, reason: %s %s %s" % \
106 (test.subdir, test.status, test.reason)
mbligh74fc0462007-11-05 20:24:17 +0000107 if re.match(r'(FAIL|WARN)',test.status):
mbligh4a2c61e2007-11-12 22:13:11 +0000108 mesgtxt += "%-12s %-20s %-12s %-10s %s" % \
109 (jobname, job.kernel.base, test.subdir,
110 test.status, test.reason)
mbligh74fc0462007-11-05 20:24:17 +0000111
mblighb89c7b32008-01-25 17:37:23 +0000112 if len(mesgtxt) > 2 and options.mailit:
mbligh4a2c61e2007-11-12 22:13:11 +0000113 print "Sending email report of FAILURES on %s to %s" % \
114 (jobname, job.user)
mbligh74fc0462007-11-05 20:24:17 +0000115 mailfailure(jobname, job, mesgtxt)
mbligh9ec94852007-10-25 15:24:16 +0000116 db.insert_job(jobname, job)
mblighbc985702007-11-05 20:52:15 +0000117 print "COMMITING"
mbligh432bad42007-10-09 19:56:07 +0000118 db.commit()
mblighfd6682452007-09-30 22:02:02 +0000119
mbligh9ec94852007-10-25 15:24:16 +0000120
mbligh05bb6332008-03-13 15:34:04 +0000121def parse_path(path):
mblighb89c7b32008-01-25 17:37:23 +0000122 job_elements = path.split('/')[-options.level:]
123 # last 'level' elements of path
mbligh2c05bce2007-11-26 20:15:58 +0000124 jobname = '/'.join(job_elements)
mbligh9ec94852007-10-25 15:24:16 +0000125 machine_list = os.path.join(path, '.machines')
126 if os.path.exists(machine_list):
mbligh0a498cb2007-11-26 17:34:28 +0000127 # This is a multi-machine job
mbligh05bb6332008-03-13 15:34:04 +0000128 for m in open(machine_list):
mbligh9ec94852007-10-25 15:24:16 +0000129 machine = m.rstrip()
130 if not machine:
131 continue
132 jobpath = os.path.join(path, machine)
133 jobname = os.path.join(os.path.basename(path), machine)
mblighbc985702007-11-05 20:52:15 +0000134 try:
135 do_parse(jobname, jobpath)
136 except:
137 print format_error()
138 continue
mbligh9ec94852007-10-25 15:24:16 +0000139 else:
mbligh0a498cb2007-11-26 17:34:28 +0000140 # This is a single-machine job
mblighbc985702007-11-05 20:52:15 +0000141 try:
142 do_parse(jobname, path)
143 except:
144 print format_error()
mbligh9ec94852007-10-25 15:24:16 +0000145
146
mbligh05bb6332008-03-13 15:34:04 +0000147for path in jobs_list:
148 lockfile = open(os.path.join(path, ".parse.lock"), "w")
149 fcntl.flock(lockfile, fcntl.LOCK_EX)
150 try:
151 parse_path(path)
152 finally:
153 fcntl.flock(lockfile, fcntl.LOCK_UN)
154 lockfile.close()