blob: 19a4cf8be07e639628068cd2d6716abaab8ed818 [file] [log] [blame]
mbligh4263b062008-02-21 19:14:43 +00001#!/usr/bin/python -u
mblighb89c7b32008-01-25 17:37:23 +00002import os, re, parse, frontend, db, sys, socket
3from 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)
24(options, args) = parser.parse_args()
mblighbc985702007-11-05 20:52:15 +000025
mblighb89c7b32008-01-25 17:37:23 +000026dir = os.path.abspath(args[0])
27assert os.path.exists(dir)
mbligh74fc0462007-11-05 20:24:17 +000028
mblighb89c7b32008-01-25 17:37:23 +000029if options.singledir:
mbligh2c05bce2007-11-26 20:15:58 +000030 jobs_list = [dir]
mbligh74fc0462007-11-05 20:24:17 +000031else:
mblighb89c7b32008-01-25 17:37:23 +000032 jobs_list = [os.path.join(dir, subdir) for subdir in os.listdir(dir)]
mblighbb7b8912006-10-08 03:59:02 +000033
mbligh9ec94852007-10-25 15:24:16 +000034debug = True
35
mbligh74fc0462007-11-05 20:24:17 +000036failcc = ""
mbligh9e7c6d02007-11-26 20:59:45 +000037# The user to notify on job failures - TOOD, pull into the config file
mbligh74fc0462007-11-05 20:24:17 +000038notify_user = None
mblighbb7b8912006-10-08 03:59:02 +000039
mbligh432bad42007-10-09 19:56:07 +000040db = db.db(autocommit=False) # do commits transactionally
mbligh056d0d32006-10-08 22:31:10 +000041
mbligh9ec94852007-10-25 15:24:16 +000042
mbligh74fc0462007-11-05 20:24:17 +000043def mailfailure(jobname, job, mesgtxt):
44 # XXX: Need to insert URL here too (frontend.test.url?)
45 link = "http://" + socket.gethostname() + "/results/" + jobname
46
47 # This looks pretty good on fixed-width-font email reader.
mbligh032c2de2007-11-24 19:20:12 +000048 message_header = "\n%s\n%s\n\n%-12s %-20s %-12s %-10s %s\n" % \
49 ("The following tests FAILED for this job:",
50 link, "Job name", "Kernel", "Test name",
51 "FAIL/WARN", "Failure Reason")
52 message_header += "%-12s %-20s %-12s %-10s %s\n" % \
53 ("========", "======", "=========",
54 "=========", "==============")
mbligh74fc0462007-11-05 20:24:17 +000055
56 subject = "AUTOTEST: FAILED tests from " + " job " + jobname
mbligh9e7c6d02007-11-26 20:59:45 +000057 parse.mail(notify_user, job.user, failcc, subject,
mbligh032c2de2007-11-24 19:20:12 +000058 message_header + mesgtxt)
mbligh74fc0462007-11-05 20:24:17 +000059
60
mblighe8b37a92007-12-19 15:54:11 +000061def dprint(string):
62 if debug:
63 print string
64
65
mbligh9ec94852007-10-25 15:24:16 +000066def do_parse(jobname, path):
mbligh0a498cb2007-11-26 17:34:28 +000067 """
68 Parse a single job. Optionally send email on failure, etc.
69 """
mblighe8b37a92007-12-19 15:54:11 +000070 dprint('\nScanning %s (%s)' % (jobname, path))
mblighb89c7b32008-01-25 17:37:23 +000071 if options.reparse and db.find_job(jobname):
mblighe8b37a92007-12-19 15:54:11 +000072 dprint('! Deleting old copy of job results, to reparse it')
73 db.delete_job(jobname)
74 if db.find_job(jobname): # Job has already been parsed
75 dprint('! Already processed')
mbligh9ec94852007-10-25 15:24:16 +000076 return
mbligh532cb272007-11-26 18:54:20 +000077 job = parse.job(path)
mbligh8e1ab172007-09-13 17:29:56 +000078 if not job:
mblighe8b37a92007-12-19 15:54:11 +000079 dprint('! Failed to parse job (no status file?)')
mbligh9ec94852007-10-25 15:24:16 +000080 return
mblighd5c33db2006-10-08 21:34:16 +000081 if not job.kernel:
mblighe8b37a92007-12-19 15:54:11 +000082 dprint('! Failed to find kernel for job')
mbligh9ec94852007-10-25 15:24:16 +000083 return
mblighf54ed2a2007-11-24 19:31:30 +000084 print '+ Parsing ' + path
mbligh74fc0462007-11-05 20:24:17 +000085 print '* jobname, kernel version: %s %s' % (jobname, job.kernel.base)
86 mesgtxt = "\n"
mblighe9cf9d42007-08-31 08:56:00 +000087 for test in job.tests:
mbligh74fc0462007-11-05 20:24:17 +000088 if not test.subdir:
89 continue
mbligh4a2c61e2007-11-12 22:13:11 +000090 print "* testname, status, reason: %s %s %s" % \
91 (test.subdir, test.status, test.reason)
mbligh74fc0462007-11-05 20:24:17 +000092 if re.match(r'(FAIL|WARN)',test.status):
mbligh4a2c61e2007-11-12 22:13:11 +000093 mesgtxt += "%-12s %-20s %-12s %-10s %s" % \
94 (jobname, job.kernel.base, test.subdir,
95 test.status, test.reason)
mbligh74fc0462007-11-05 20:24:17 +000096
mblighb89c7b32008-01-25 17:37:23 +000097 if len(mesgtxt) > 2 and options.mailit:
mbligh4a2c61e2007-11-12 22:13:11 +000098 print "Sending email report of FAILURES on %s to %s" % \
99 (jobname, job.user)
mbligh74fc0462007-11-05 20:24:17 +0000100 mailfailure(jobname, job, mesgtxt)
mbligh9ec94852007-10-25 15:24:16 +0000101 db.insert_job(jobname, job)
mblighbc985702007-11-05 20:52:15 +0000102 print "COMMITING"
mbligh432bad42007-10-09 19:56:07 +0000103 db.commit()
mblighfd6682452007-09-30 22:02:02 +0000104
mbligh9ec94852007-10-25 15:24:16 +0000105
mbligh74fc0462007-11-05 20:24:17 +0000106
mbligh2c05bce2007-11-26 20:15:58 +0000107for path in jobs_list:
mblighb89c7b32008-01-25 17:37:23 +0000108 job_elements = path.split('/')[-options.level:]
109 # last 'level' elements of path
mbligh2c05bce2007-11-26 20:15:58 +0000110 jobname = '/'.join(job_elements)
mbligh9ec94852007-10-25 15:24:16 +0000111 machine_list = os.path.join(path, '.machines')
112 if os.path.exists(machine_list):
mbligh0a498cb2007-11-26 17:34:28 +0000113 # This is a multi-machine job
mbligh9ec94852007-10-25 15:24:16 +0000114 for m in open(machine_list, 'r').readlines():
115 machine = m.rstrip()
116 if not machine:
117 continue
118 jobpath = os.path.join(path, machine)
119 jobname = os.path.join(os.path.basename(path), machine)
mblighbc985702007-11-05 20:52:15 +0000120 try:
121 do_parse(jobname, jobpath)
122 except:
123 print format_error()
124 continue
mbligh9ec94852007-10-25 15:24:16 +0000125 else:
mbligh0a498cb2007-11-26 17:34:28 +0000126 # This is a single-machine job
mblighbc985702007-11-05 20:52:15 +0000127 try:
128 do_parse(jobname, path)
129 except:
130 print format_error()
131 continue
mbligh9ec94852007-10-25 15:24:16 +0000132
133
mbligh0a498cb2007-11-26 17:34:28 +0000134# Generate vertical text pngs for pretty display in tables.
135#
mblighbc985702007-11-05 20:52:15 +0000136# rows = db.select('distinct hostname', 'machines', {})
137# machines = [row[0] for row in rows]
138#
139# for machine in machines:
140# dir = os.path.dirname(os.path.abspath(sys.argv[0]))
141# vertical_text = os.path.join(dir, 'vertical_text.py')
142# os.system(vertical_text + ' ' + machine)