mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | c251454 | 2008-02-19 15:54:26 +0000 | [diff] [blame] | 2 | |
mbligh | b33e53e | 2008-06-17 19:41:26 +0000 | [diff] [blame] | 3 | import os, sys, optparse, fcntl, errno, traceback, socket |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 4 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 5 | import common |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import mail, utils |
| 7 | from autotest_lib.tko import db as tko_db, utils as tko_utils, status_lib |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 8 | |
| 9 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 10 | def parse_args(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 11 | # 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() |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 33 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 34 | # 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) |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 40 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 41 | # pass the options back |
| 42 | return options, args |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 43 | |
| 44 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 45 | def format_failure_message(jobname, kernel, testname, status, reason): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 46 | format_string = "%-12s %-20s %-12s %-10s %s" |
| 47 | return format_string % (jobname, kernel, testname, status, reason) |
mbligh | b85e6b0 | 2006-10-08 17:20:56 +0000 | [diff] [blame] | 48 | |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 49 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 50 | def mailfailure(jobname, job, message): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 51 | 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) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 62 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 63 | subject = "AUTOTEST: FAILED tests from job %s" % jobname |
| 64 | mail.send("", job.user, "", subject, message_header + message) |
mbligh | 006f230 | 2007-09-13 20:46:46 +0000 | [diff] [blame] | 65 | |
| 66 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 67 | def parse_one(db, jobname, path, reparse, mail_on_failure): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 68 | """ |
| 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 |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 79 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 80 | # 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) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 90 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 91 | # 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 |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 100 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 101 | # 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 |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 107 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 108 | # 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) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 120 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 121 | # 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) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 126 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 127 | # write the job into the database |
| 128 | db.insert_job(jobname, job) |
| 129 | db.commit() |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 130 | |
| 131 | |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame^] | 132 | def _get_job_subdirs(path): |
| 133 | """ |
| 134 | Returns a list of job subdirectories at path. Returns None if the test |
| 135 | is itself a job directory. Does not recurse into the subdirs. |
| 136 | """ |
| 137 | # if there's a .machines file, use it to get the subdirs |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 138 | machine_list = os.path.join(path, ".machines") |
| 139 | if os.path.exists(machine_list): |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame^] | 140 | return set(line.strip() for line in file(machine_list)) |
| 141 | |
| 142 | # if this dir contains ONLY subdirectories, return them |
| 143 | contents = set(os.listdir(path)) |
| 144 | contents.discard(".parse.lock") |
| 145 | subdirs = set(sub for sub in contents if |
| 146 | os.path.isdir(os.path.join(path, sub))) |
| 147 | if len(contents) == len(subdirs) != 0: |
| 148 | return subdirs |
| 149 | |
| 150 | # this is a job directory, or something else we don't understand |
| 151 | return None |
| 152 | |
| 153 | |
| 154 | def parse_path(db, path, level, reparse, mail_on_failure): |
| 155 | job_subdirs = _get_job_subdirs(path) |
| 156 | if job_subdirs is not None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 157 | # multi-machine job |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame^] | 158 | for subdir in job_subdirs: |
| 159 | jobpath = os.path.join(path, subdir) |
| 160 | parse_path(db, jobpath, level + 1, reparse, mail_on_failure) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 161 | else: |
| 162 | # single machine job |
| 163 | job_elements = path.split("/")[-level:] |
| 164 | jobname = "/".join(job_elements) |
| 165 | try: |
| 166 | db.run_with_retry(parse_one, db, jobname, path, |
| 167 | reparse, mail_on_failure) |
| 168 | except Exception: |
| 169 | traceback.print_exc() |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 170 | |
| 171 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 172 | def main(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 173 | options, args = parse_args() |
| 174 | results_dir = os.path.abspath(args[0]) |
| 175 | assert os.path.exists(results_dir) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 176 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 177 | # build up the list of job dirs to parse |
| 178 | if options.singledir: |
| 179 | jobs_list = [results_dir] |
| 180 | else: |
| 181 | jobs_list = [os.path.join(results_dir, subdir) |
| 182 | for subdir in os.listdir(results_dir)] |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 183 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 184 | # build up the database |
| 185 | db = tko_db.db(autocommit=False, host=options.db_host, |
| 186 | user=options.db_user, password=options.db_pass, |
| 187 | database=options.db_name) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 188 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 189 | # parse all the jobs |
| 190 | for path in jobs_list: |
| 191 | lockfile = open(os.path.join(path, ".parse.lock"), "w") |
| 192 | flags = fcntl.LOCK_EX |
| 193 | if options.noblock: |
| 194 | flags != fcntl.LOCK_NB |
| 195 | try: |
| 196 | fcntl.flock(lockfile, flags) |
| 197 | except IOError, e: |
| 198 | # was this because the lock is unavailable? |
| 199 | if e.errno == errno.EWOULDBLOCK: |
| 200 | lockfile.close() |
| 201 | continue |
| 202 | else: |
| 203 | raise # something unexpected happened |
| 204 | try: |
| 205 | parse_path(db, path, options.level, options.reparse, |
| 206 | options.mailit) |
| 207 | finally: |
| 208 | fcntl.flock(lockfile, fcntl.LOCK_UN) |
| 209 | lockfile.close() |
mbligh | 71d340d | 2008-03-05 15:51:16 +0000 | [diff] [blame] | 210 | |
mbligh | 532cb27 | 2007-11-26 18:54:20 +0000 | [diff] [blame] | 211 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 212 | if __name__ == "__main__": |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 213 | main() |