lmr | 6f80e7a | 2010-02-04 03:18:28 +0000 | [diff] [blame] | 1 | import os |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 2 | |
| 3 | from autotest_lib.client.common_lib import utils |
| 4 | from autotest_lib.tko import utils as tko_utils |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 5 | |
| 6 | |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 7 | class job(object): |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 8 | def __init__(self, dir, user, label, machine, queued_time, started_time, |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 9 | finished_time, machine_owner, machine_group, aborted_by, |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 10 | aborted_on, keyval_dict): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 11 | self.dir = dir |
| 12 | self.tests = [] |
| 13 | self.user = user |
| 14 | self.label = label |
| 15 | self.machine = machine |
| 16 | self.queued_time = queued_time |
| 17 | self.started_time = started_time |
| 18 | self.finished_time = finished_time |
| 19 | self.machine_owner = machine_owner |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 20 | self.machine_group = machine_group |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 21 | self.aborted_by = aborted_by |
| 22 | self.aborted_on = aborted_on |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 23 | self.keyval_dict = keyval_dict |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 24 | |
| 25 | |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 26 | @staticmethod |
| 27 | def read_keyval(dir): |
| 28 | dir = os.path.normpath(dir) |
| 29 | top_dir = tko_utils.find_toplevel_job_dir(dir) |
| 30 | if not top_dir: |
| 31 | top_dir = dir |
| 32 | assert(dir.startswith(top_dir)) |
| 33 | |
| 34 | # pull in and merge all the keyval files, with higher-level |
| 35 | # overriding values in the lower-level ones |
| 36 | keyval = {} |
| 37 | while True: |
| 38 | try: |
jadmanski | fc4e338 | 2008-10-02 16:19:19 +0000 | [diff] [blame] | 39 | upper_keyval = utils.read_keyval(dir) |
| 40 | # HACK: exclude hostname from the override - this is a special |
| 41 | # case where we want lower to override higher |
| 42 | if "hostname" in upper_keyval and "hostname" in keyval: |
| 43 | del upper_keyval["hostname"] |
jadmanski | 8e00afa | 2008-10-02 16:22:04 +0000 | [diff] [blame] | 44 | keyval.update(upper_keyval) |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 45 | except IOError: |
| 46 | pass # if the keyval can't be read just move on to the next |
| 47 | if dir == top_dir: |
| 48 | break |
| 49 | else: |
| 50 | assert(dir != "/") |
| 51 | dir = os.path.dirname(dir) |
| 52 | return keyval |
| 53 | |
| 54 | |
| 55 | |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 56 | class kernel(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 57 | def __init__(self, base, patches, kernel_hash): |
| 58 | self.base = base |
| 59 | self.patches = patches |
| 60 | self.kernel_hash = kernel_hash |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 61 | |
| 62 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 63 | @staticmethod |
| 64 | def compute_hash(base, hashes): |
| 65 | key_string = ','.join([base] + hashes) |
lmr | 6f80e7a | 2010-02-04 03:18:28 +0000 | [diff] [blame] | 66 | return utils.hash('md5', key_string).hexdigest() |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 67 | |
| 68 | |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 69 | class test(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | def __init__(self, subdir, testname, status, reason, test_kernel, |
| 71 | machine, started_time, finished_time, iterations, |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 72 | attributes, labels): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 73 | self.subdir = subdir |
| 74 | self.testname = testname |
| 75 | self.status = status |
| 76 | self.reason = reason |
| 77 | self.kernel = test_kernel |
| 78 | self.machine = machine |
| 79 | self.started_time = started_time |
| 80 | self.finished_time = finished_time |
| 81 | self.iterations = iterations |
| 82 | self.attributes = attributes |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 83 | self.labels = labels |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 84 | |
| 85 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 86 | @staticmethod |
| 87 | def load_iterations(keyval_path): |
| 88 | """Abstract method to load a list of iterations from a keyval |
| 89 | file.""" |
| 90 | raise NotImplementedError |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 91 | |
| 92 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 93 | @classmethod |
| 94 | def parse_test(cls, job, subdir, testname, status, reason, test_kernel, |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 95 | started_time, finished_time, existing_instance=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 96 | """Given a job and the basic metadata about the test that |
| 97 | can be extracted from the status logs, parse the test |
| 98 | keyval files and use it to construct a complete test |
| 99 | instance.""" |
| 100 | tko_utils.dprint("parsing test %s %s" % (subdir, testname)) |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 101 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 102 | if subdir: |
| 103 | # grab iterations from the results keyval |
| 104 | iteration_keyval = os.path.join(job.dir, subdir, |
| 105 | "results", "keyval") |
| 106 | iterations = cls.load_iterations(iteration_keyval) |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 107 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 108 | # grab test attributes from the subdir keyval |
| 109 | test_keyval = os.path.join(job.dir, subdir, "keyval") |
| 110 | attributes = test.load_attributes(test_keyval) |
| 111 | else: |
| 112 | iterations = [] |
| 113 | attributes = {} |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 114 | |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 115 | # grab test+host attributes from the host keyval |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 116 | host_keyval = cls.parse_host_keyval(job.dir, job.machine) |
| 117 | attributes.update(dict(("host-%s" % k, v) |
| 118 | for k, v in host_keyval.iteritems())) |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 119 | |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 120 | if existing_instance: |
| 121 | def constructor(*args, **dargs): |
| 122 | existing_instance.__init__(*args, **dargs) |
| 123 | return existing_instance |
| 124 | else: |
| 125 | constructor = cls |
| 126 | return constructor(subdir, testname, status, reason, test_kernel, |
| 127 | job.machine, started_time, finished_time, |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 128 | iterations, attributes, []) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | @classmethod |
| 132 | def parse_partial_test(cls, job, subdir, testname, reason, test_kernel, |
| 133 | started_time): |
| 134 | """Given a job and the basic metadata available when a test is |
| 135 | started, create a test instance representing the partial result. |
| 136 | Assume that since the test is not complete there are no results files |
| 137 | actually available for parsing.""" |
| 138 | tko_utils.dprint("parsing partial test %s %s" % (subdir, testname)) |
| 139 | |
| 140 | return cls(subdir, testname, "RUNNING", reason, test_kernel, |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 141 | job.machine, started_time, None, [], {}, []) |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 142 | |
| 143 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 144 | @staticmethod |
| 145 | def load_attributes(keyval_path): |
| 146 | """Load the test attributes into a dictionary from a test |
| 147 | keyval path. Does not assume that the path actually exists.""" |
| 148 | if not os.path.exists(keyval_path): |
| 149 | return {} |
| 150 | return utils.read_keyval(keyval_path) |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 151 | |
| 152 | |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 153 | @staticmethod |
| 154 | def parse_host_keyval(job_dir, hostname): |
| 155 | # the "real" job dir may be higher up in the directory tree |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 156 | job_dir = tko_utils.find_toplevel_job_dir(job_dir) |
| 157 | if not job_dir: |
| 158 | return {} # we can't find a top-level job dir with host keyvals |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 159 | |
| 160 | # the keyval is <job_dir>/host_keyvals/<hostname> if it exists |
| 161 | keyval_path = os.path.join(job_dir, "host_keyvals", hostname) |
| 162 | if os.path.isfile(keyval_path): |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 163 | return utils.read_keyval(keyval_path) |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 164 | else: |
| 165 | return {} |
| 166 | |
| 167 | |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 168 | class patch(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 169 | def __init__(self, spec, reference, hash): |
| 170 | self.spec = spec |
| 171 | self.reference = reference |
| 172 | self.hash = hash |
mbligh | e79ebb0 | 2008-04-17 15:39:22 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | class iteration(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 176 | def __init__(self, index, attr_keyval, perf_keyval): |
| 177 | self.index = index |
| 178 | self.attr_keyval = attr_keyval |
| 179 | self.perf_keyval = perf_keyval |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 180 | |
| 181 | |
jadmanski | 9978bdf | 2008-06-06 16:29:13 +0000 | [diff] [blame] | 182 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 183 | @staticmethod |
| 184 | def parse_line_into_dicts(line, attr_dict, perf_dict): |
| 185 | """Abstract method to parse a keyval line and insert it into |
| 186 | the appropriate dictionary. |
| 187 | attr_dict: generic iteration attributes |
| 188 | perf_dict: iteration performance results |
| 189 | """ |
| 190 | raise NotImplementedError |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 191 | |
| 192 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 193 | @classmethod |
| 194 | def load_from_keyval(cls, keyval_path): |
| 195 | """Load a list of iterations from an iteration keyval file. |
| 196 | Keyval data from separate iterations is separated by blank |
| 197 | lines. Makes use of the parse_line_into_dicts method to |
| 198 | actually parse the individual lines.""" |
| 199 | if not os.path.exists(keyval_path): |
| 200 | return [] |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 201 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | iterations = [] |
| 203 | index = 1 |
| 204 | attr, perf = {}, {} |
| 205 | for line in file(keyval_path): |
| 206 | line = line.strip() |
| 207 | if line: |
| 208 | cls.parse_line_into_dicts(line, attr, perf) |
| 209 | else: |
| 210 | iterations.append(cls(index, attr, perf)) |
| 211 | index += 1 |
| 212 | attr, perf = {}, {} |
| 213 | if attr or perf: |
| 214 | iterations.append(cls(index, attr, perf)) |
| 215 | return iterations |