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 | db4f9b5 | 2008-12-03 22:52:53 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import mail, pidfile |
| 7 | from autotest_lib.tko import db as tko_db, utils as tko_utils, status_lib, models |
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") |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 32 | parser.add_option("--write-pidfile", |
| 33 | help="write pidfile (.parser_execute)", |
| 34 | dest="write_pidfile", action="store_true", |
| 35 | default=False) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 36 | options, args = parser.parse_args() |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 37 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | # we need a results directory |
| 39 | if len(args) == 0: |
| 40 | tko_utils.dprint("ERROR: at least one results directory must " |
| 41 | "be provided") |
| 42 | parser.print_help() |
| 43 | sys.exit(1) |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 44 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 45 | # pass the options back |
| 46 | return options, args |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 47 | |
| 48 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 49 | def format_failure_message(jobname, kernel, testname, status, reason): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | format_string = "%-12s %-20s %-12s %-10s %s" |
| 51 | return format_string % (jobname, kernel, testname, status, reason) |
mbligh | b85e6b0 | 2006-10-08 17:20:56 +0000 | [diff] [blame] | 52 | |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 53 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 54 | def mailfailure(jobname, job, message): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 55 | message_lines = [""] |
| 56 | message_lines.append("The following tests FAILED for this job") |
| 57 | message_lines.append("http://%s/results/%s" % |
| 58 | (socket.gethostname(), jobname)) |
| 59 | message_lines.append("") |
| 60 | message_lines.append(format_failure_message("Job name", "Kernel", |
| 61 | "Test name", "FAIL/WARN", |
| 62 | "Failure reason")) |
| 63 | message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8, |
| 64 | "=" * 8, "=" * 14)) |
| 65 | message_header = "\n".join(message_lines) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 66 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 67 | subject = "AUTOTEST: FAILED tests from job %s" % jobname |
| 68 | mail.send("", job.user, "", subject, message_header + message) |
mbligh | 006f230 | 2007-09-13 20:46:46 +0000 | [diff] [blame] | 69 | |
| 70 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 71 | def parse_one(db, jobname, path, reparse, mail_on_failure): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 72 | """ |
| 73 | Parse a single job. Optionally send email on failure. |
| 74 | """ |
| 75 | tko_utils.dprint("\nScanning %s (%s)" % (jobname, path)) |
| 76 | if reparse and db.find_job(jobname): |
| 77 | tko_utils.dprint("! Deleting old copy of job results to " |
| 78 | "reparse it") |
| 79 | db.delete_job(jobname) |
| 80 | if db.find_job(jobname): |
| 81 | tko_utils.dprint("! Job is already parsed, done") |
| 82 | return |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 83 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 84 | # look up the status version |
jadmanski | db4f9b5 | 2008-12-03 22:52:53 +0000 | [diff] [blame] | 85 | job_keyval = models.job.read_keyval(path) |
| 86 | status_version = job_keyval.get("status_version", 0) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 87 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 88 | # parse out the job |
| 89 | parser = status_lib.parser(status_version) |
| 90 | job = parser.make_job(path) |
| 91 | status_log = os.path.join(path, "status.log") |
| 92 | if not os.path.exists(status_log): |
| 93 | status_log = os.path.join(path, "status") |
| 94 | if not os.path.exists(status_log): |
| 95 | tko_utils.dprint("! Unable to parse job, no status file") |
| 96 | return |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 97 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 98 | # parse the status logs |
| 99 | tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname)) |
| 100 | status_lines = open(status_log).readlines() |
| 101 | parser.start(job) |
| 102 | tests = parser.end(status_lines) |
| 103 | job.tests = tests |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 104 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | # check for failures |
| 106 | message_lines = [""] |
| 107 | for test in job.tests: |
| 108 | if not test.subdir: |
| 109 | continue |
| 110 | tko_utils.dprint("* testname, status, reason: %s %s %s" |
| 111 | % (test.subdir, test.status, test.reason)) |
| 112 | if test.status in ("FAIL", "WARN"): |
| 113 | message_lines.append(format_failure_message( |
| 114 | jobname, test.kernel.base, test.subdir, |
| 115 | test.status, test.reason)) |
| 116 | message = "\n".join(message_lines) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 117 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 118 | # send out a email report of failure |
| 119 | if len(message) > 2 and mail_on_failure: |
| 120 | tko_utils.dprint("Sending email report of failure on %s to %s" |
| 121 | % (jobname, job.user)) |
| 122 | mailfailure(jobname, job, message) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 123 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 124 | # write the job into the database |
| 125 | db.insert_job(jobname, job) |
| 126 | db.commit() |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 127 | |
| 128 | |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 129 | def _get_job_subdirs(path): |
| 130 | """ |
| 131 | Returns a list of job subdirectories at path. Returns None if the test |
| 132 | is itself a job directory. Does not recurse into the subdirs. |
| 133 | """ |
| 134 | # if there's a .machines file, use it to get the subdirs |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 135 | machine_list = os.path.join(path, ".machines") |
| 136 | if os.path.exists(machine_list): |
jadmanski | 42fbd07 | 2009-01-30 15:07:05 +0000 | [diff] [blame] | 137 | subdirs = set(line.strip() for line in file(machine_list)) |
| 138 | existing_subdirs = set(subdir for subdir in subdirs |
| 139 | if os.path.exists(os.path.join(path, subdir))) |
| 140 | if len(existing_subdirs) != 0: |
| 141 | return existing_subdirs |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 142 | |
| 143 | # if this dir contains ONLY subdirectories, return them |
| 144 | contents = set(os.listdir(path)) |
| 145 | contents.discard(".parse.lock") |
| 146 | subdirs = set(sub for sub in contents if |
| 147 | os.path.isdir(os.path.join(path, sub))) |
| 148 | if len(contents) == len(subdirs) != 0: |
| 149 | return subdirs |
| 150 | |
| 151 | # this is a job directory, or something else we don't understand |
| 152 | return None |
| 153 | |
| 154 | |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame^] | 155 | def parse_leaf_path(db, path, level, reparse, mail_on_failure): |
| 156 | job_elements = path.split("/")[-level:] |
| 157 | jobname = "/".join(job_elements) |
| 158 | try: |
| 159 | db.run_with_retry(parse_one, db, jobname, path, reparse, |
| 160 | mail_on_failure) |
| 161 | except Exception: |
| 162 | traceback.print_exc() |
| 163 | |
| 164 | |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 165 | def parse_path(db, path, level, reparse, mail_on_failure): |
| 166 | job_subdirs = _get_job_subdirs(path) |
| 167 | if job_subdirs is not None: |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame^] | 168 | # parse status.log in current directory, if it exists. multi-machine |
| 169 | # synchronous server side tests record output in this directory. without |
| 170 | # this check, we do not parse these results. |
| 171 | if os.path.exists(os.path.join(path, 'status.log')): |
| 172 | parse_leaf_path(db, path, level, reparse, mail_on_failure) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 173 | # multi-machine job |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 174 | for subdir in job_subdirs: |
| 175 | jobpath = os.path.join(path, subdir) |
| 176 | parse_path(db, jobpath, level + 1, reparse, mail_on_failure) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 177 | else: |
| 178 | # single machine job |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame^] | 179 | parse_leaf_path(db, path, level, reparse, mail_on_failure) |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 180 | |
| 181 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 182 | def main(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 183 | options, args = parse_args() |
| 184 | results_dir = os.path.abspath(args[0]) |
| 185 | assert os.path.exists(results_dir) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 186 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 187 | pid_file_manager = pidfile.PidFileManager("parser", results_dir) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 188 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 189 | if options.write_pidfile: |
| 190 | pid_file_manager.open_file() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 191 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 192 | try: |
| 193 | # build up the list of job dirs to parse |
| 194 | if options.singledir: |
| 195 | jobs_list = [results_dir] |
| 196 | else: |
| 197 | jobs_list = [os.path.join(results_dir, subdir) |
| 198 | for subdir in os.listdir(results_dir)] |
| 199 | |
| 200 | # build up the database |
| 201 | db = tko_db.db(autocommit=False, host=options.db_host, |
| 202 | user=options.db_user, password=options.db_pass, |
| 203 | database=options.db_name) |
| 204 | |
| 205 | # parse all the jobs |
| 206 | for path in jobs_list: |
| 207 | lockfile = open(os.path.join(path, ".parse.lock"), "w") |
| 208 | flags = fcntl.LOCK_EX |
| 209 | if options.noblock: |
mbligh | db18b0e | 2009-01-30 00:34:32 +0000 | [diff] [blame] | 210 | flags |= fcntl.LOCK_NB |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 211 | try: |
| 212 | fcntl.flock(lockfile, flags) |
| 213 | except IOError, e: |
mbligh | db18b0e | 2009-01-30 00:34:32 +0000 | [diff] [blame] | 214 | # lock is not available and nonblock has been requested |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 215 | if e.errno == errno.EWOULDBLOCK: |
| 216 | lockfile.close() |
| 217 | continue |
| 218 | else: |
| 219 | raise # something unexpected happened |
| 220 | try: |
| 221 | parse_path(db, path, options.level, options.reparse, |
| 222 | options.mailit) |
| 223 | finally: |
| 224 | fcntl.flock(lockfile, fcntl.LOCK_UN) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 225 | lockfile.close() |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 226 | except: |
| 227 | pid_file_manager.close_file(1) |
| 228 | raise |
| 229 | else: |
| 230 | pid_file_manager.close_file(0) |
mbligh | 71d340d | 2008-03-05 15:51:16 +0000 | [diff] [blame] | 231 | |
mbligh | 532cb27 | 2007-11-26 18:54:20 +0000 | [diff] [blame] | 232 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 233 | if __name__ == "__main__": |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 234 | main() |