mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 1 | import re, os, sys, types, time, random |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 2 | |
| 3 | import common |
| 4 | from autotest_lib.client.common_lib import global_config |
| 5 | |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 6 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 7 | class MySQLTooManyRows(Exception): |
| 8 | pass |
| 9 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 10 | |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 11 | class db_sql: |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 12 | def __init__(self, debug=False, autocommit=True, host=None, |
| 13 | database=None, user=None, password=None): |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 14 | self.debug = debug |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 15 | self.autocommit = autocommit |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 16 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 17 | self._load_config(host, database, user, password) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 18 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 19 | self.con = None |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 20 | self._init_db() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 21 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 22 | # if not present, insert statuses |
| 23 | self.status_idx = {} |
| 24 | self.status_word = {} |
mbligh | c82f246 | 2007-10-02 19:19:17 +0000 | [diff] [blame] | 25 | status_rows = self.select('status_idx, word', 'status', None) |
| 26 | for s in status_rows: |
mbligh | 9789445 | 2007-10-05 15:10:33 +0000 | [diff] [blame] | 27 | self.status_idx[s[1]] = s[0] |
mbligh | c82f246 | 2007-10-02 19:19:17 +0000 | [diff] [blame] | 28 | self.status_word[s[0]] = s[1] |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 29 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 30 | machine_map = os.path.join(os.path.dirname(__file__), |
| 31 | 'machines') |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 32 | if os.path.exists(machine_map): |
| 33 | self.machine_map = machine_map |
mbligh | ba9c54c | 2007-10-26 16:41:26 +0000 | [diff] [blame] | 34 | else: |
| 35 | self.machine_map = None |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 36 | self.machine_group = {} |
| 37 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 38 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 39 | def _load_config(self, host, database, user, password): |
| 40 | # grab the global config |
| 41 | get_value = global_config.global_config.get_config_value |
| 42 | |
| 43 | # grab the host, database |
| 44 | if not host: |
| 45 | self.host = get_value("TKO", "host") |
| 46 | if not database: |
| 47 | self.database = get_value("TKO", "database") |
| 48 | |
| 49 | # grab the user and password |
| 50 | if not user: |
| 51 | self.user = get_value("TKO", "user") |
| 52 | if not password: |
| 53 | self.password = get_value("TKO", "password") |
| 54 | |
| 55 | # grab the timeout configuration |
| 56 | self.query_timeout = get_value("TKO", "query_timeout", |
| 57 | type=int, default=3600) |
| 58 | self.min_delay = get_value("TKO", "min_retry_delay", type=int, |
| 59 | default=20) |
| 60 | self.max_delay = get_value("TKO", "max_retry_delay", type=int, |
| 61 | default=60) |
| 62 | |
| 63 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 64 | def _init_db(self): |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 65 | # make sure we clean up any existing connection |
| 66 | if self.con: |
| 67 | self.con.close() |
| 68 | self.con = None |
| 69 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 70 | # create the db connection and cursor |
| 71 | self.con = self.connect(self.host, self.database, |
| 72 | self.user, self.password) |
| 73 | self.cur = self.con.cursor() |
| 74 | |
| 75 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 76 | def _random_delay(self): |
| 77 | delay = random.randint(self.min_delay, self.max_delay) |
| 78 | time.sleep(delay) |
| 79 | |
| 80 | |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame^] | 81 | def run_with_retry(self, function, *args, **dargs): |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 82 | """Call function(*args, **dargs) until either it passes |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame^] | 83 | without an operational error, or a timeout is reached. |
| 84 | This will re-connect to the database, so it is NOT safe |
| 85 | to use this inside of a database transaction. |
| 86 | |
| 87 | It can be safely used with transactions, but the |
| 88 | transaction start & end must be completely contained |
| 89 | within the call to 'function'.""" |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 90 | OperationalError = _get_error_class("OperationalError") |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 91 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 92 | success = False |
| 93 | start_time = time.time() |
| 94 | while not success: |
| 95 | try: |
| 96 | result = function(*args, **dargs) |
jadmanski | 60d4fa6 | 2008-05-06 22:49:41 +0000 | [diff] [blame] | 97 | except OperationalError, e: |
| 98 | self._log_operational_error(e) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 99 | stop_time = time.time() |
| 100 | elapsed_time = stop_time - start_time |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 101 | if elapsed_time > self.query_timeout: |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 102 | raise |
| 103 | else: |
| 104 | try: |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 105 | self._random_delay() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 106 | self._init_db() |
jadmanski | 60d4fa6 | 2008-05-06 22:49:41 +0000 | [diff] [blame] | 107 | except OperationalError, e: |
| 108 | self._log_operational_error(e) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 109 | else: |
| 110 | success = True |
| 111 | return result |
| 112 | |
| 113 | |
jadmanski | 60d4fa6 | 2008-05-06 22:49:41 +0000 | [diff] [blame] | 114 | def _log_operational_error(self, e): |
| 115 | msg = ("An operational error occured during a database " |
| 116 | "operation: %s" % str(e)) |
| 117 | print >> sys.stderr, msg |
| 118 | sys.stderr.flush() # we want these msgs to show up immediately |
| 119 | |
| 120 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 121 | def dprint(self, value): |
| 122 | if self.debug: |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 123 | sys.stdout.write('SQL: ' + str(value) + '\n') |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 124 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 125 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 126 | def commit(self): |
| 127 | self.con.commit() |
| 128 | |
| 129 | |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 130 | def get_last_autonumber_value(self): |
| 131 | self.cur.execute('SELECT LAST_INSERT_ID()', []) |
| 132 | return self.cur.fetchall()[0][0] |
| 133 | |
| 134 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 135 | def select(self, fields, table, where, wherein={}, |
| 136 | distinct = False, group_by = None, max_rows = None): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 137 | """\ |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 138 | This selects all the fields requested from a |
| 139 | specific table with a particular where clause. |
| 140 | The where clause can either be a dictionary of |
| 141 | field=value pairs, a string, or a tuple of (string, |
| 142 | a list of values). The last option is what you |
| 143 | should use when accepting user input as it'll |
| 144 | protect you against sql injection attacks (if |
| 145 | all user data is placed in the array rather than |
| 146 | the raw SQL). |
| 147 | |
| 148 | For example: |
| 149 | where = ("a = %s AND b = %s", ['val', 'val']) |
| 150 | is better than |
| 151 | where = "a = 'val' AND b = 'val'" |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 152 | """ |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 153 | cmd = ['select'] |
| 154 | if distinct: |
| 155 | cmd.append('distinct') |
| 156 | cmd += [fields, 'from', table] |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 157 | |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 158 | values = [] |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 159 | if where and isinstance(where, types.DictionaryType): |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 160 | # key/value pairs (which should be equal) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 161 | keys = [field + '=%s' for field in where.keys()] |
| 162 | values = [where[field] for field in where.keys()] |
| 163 | |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 164 | cmd.append(' where ' + ' and '.join(keys)) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 165 | elif where and isinstance(where, types.StringTypes): |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 166 | # the exact string |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 167 | cmd.append(' where ' + where) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 168 | elif where and isinstance(where, types.TupleType): |
| 169 | # preformatted where clause + values |
| 170 | (sql, vals) = where |
| 171 | values = vals |
| 172 | cmd.append(' where (%s) ' % sql) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 173 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 174 | # TODO: this assumes there's a where clause...bad |
mbligh | 85952b4 | 2007-12-07 16:28:33 +0000 | [diff] [blame] | 175 | if wherein and isinstance(wherein, types.DictionaryType): |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 176 | keys_in = ["%s in (%s) " % (field, ','.join(where)) |
| 177 | for field, where in wherein.iteritems()] |
mbligh | 85952b4 | 2007-12-07 16:28:33 +0000 | [diff] [blame] | 178 | cmd.append(' and '+' and '.join(keys_in)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 179 | |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 180 | if group_by: |
| 181 | cmd.append(' GROUP BY ' + group_by) |
| 182 | |
| 183 | self.dprint('%s %s' % (' '.join(cmd), values)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 184 | |
| 185 | # create a re-runable function for executing the query |
| 186 | def exec_sql(): |
| 187 | sql = ' '.join(cmd) |
| 188 | numRec = self.cur.execute(sql, values) |
| 189 | if max_rows != None and numRec > max_rows: |
| 190 | msg = 'Exceeded allowed number of records' |
| 191 | raise MySQLTooManyRows(msg) |
| 192 | return self.cur.fetchall() |
| 193 | |
| 194 | # run the query, re-trying after operational errors |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame^] | 195 | if self.autocommit: |
| 196 | return self.run_with_retry(exec_sql) |
| 197 | else: |
| 198 | return exec_sql() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 199 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 200 | |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 201 | def select_sql(self, fields, table, sql, values): |
| 202 | """\ |
| 203 | select fields from table "sql" |
| 204 | """ |
| 205 | cmd = 'select %s from %s %s' % (fields, table, sql) |
| 206 | self.dprint(cmd) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 207 | |
| 208 | # create a -re-runable function for executing the query |
| 209 | def exec_sql(): |
| 210 | self.cur.execute(cmd, values) |
| 211 | return self.cur.fetchall() |
| 212 | |
| 213 | # run the query, re-trying after operational errors |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame^] | 214 | if self.autocommit: |
| 215 | return self.run_with_retry(exec_sql) |
| 216 | else: |
| 217 | return exec_sql() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | def _exec_sql_with_commit(self, sql, values, commit): |
| 221 | if self.autocommit: |
| 222 | # re-run the query until it succeeds |
| 223 | def exec_sql(): |
| 224 | self.cur.execute(sql, values) |
| 225 | self.con.commit() |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame^] | 226 | self.run_with_retry(exec_sql) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 227 | else: |
| 228 | # take one shot at running the query |
| 229 | self.cur.execute(sql, values) |
| 230 | if commit: |
| 231 | self.con.commit() |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 232 | |
| 233 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 234 | def insert(self, table, data, commit = None): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 235 | """\ |
| 236 | 'insert into table (keys) values (%s ... %s)', values |
| 237 | |
| 238 | data: |
| 239 | dictionary of fields and data |
| 240 | """ |
| 241 | fields = data.keys() |
| 242 | refs = ['%s' for field in fields] |
| 243 | values = [data[field] for field in fields] |
| 244 | cmd = 'insert into %s (%s) values (%s)' % \ |
| 245 | (table, ','.join(fields), ','.join(refs)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 246 | self.dprint('%s %s' % (cmd, values)) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 247 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 248 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 249 | |
| 250 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 251 | def delete(self, table, where, commit = None): |
| 252 | cmd = ['delete from', table] |
| 253 | if commit == None: |
| 254 | commit = self.autocommit |
| 255 | if where and isinstance(where, types.DictionaryType): |
| 256 | keys = [field + '=%s' for field in where.keys()] |
| 257 | values = [where[field] for field in where.keys()] |
| 258 | cmd += ['where', ' and '.join(keys)] |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 259 | sql = ' '.join(cmd) |
| 260 | self.dprint('%s %s' % (sql, values)) |
| 261 | |
| 262 | self._exec_sql_with_commit(sql, values, commit) |
| 263 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 264 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 265 | def update(self, table, data, where, commit = None): |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 266 | """\ |
| 267 | 'update table set data values (%s ... %s) where ...' |
| 268 | |
| 269 | data: |
| 270 | dictionary of fields and data |
| 271 | """ |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 272 | if commit == None: |
| 273 | commit = self.autocommit |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 274 | cmd = 'update %s ' % table |
| 275 | fields = data.keys() |
| 276 | data_refs = [field + '=%s' for field in fields] |
| 277 | data_values = [data[field] for field in fields] |
| 278 | cmd += ' set ' + ' and '.join(data_refs) |
| 279 | |
| 280 | where_keys = [field + '=%s' for field in where.keys()] |
| 281 | where_values = [where[field] for field in where.keys()] |
| 282 | cmd += ' where ' + ' and '.join(where_keys) |
| 283 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 284 | values = data_values + where_values |
| 285 | print '%s %s' % (cmd, values) |
| 286 | |
| 287 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 288 | |
| 289 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 290 | def delete_job(self, tag, commit = None): |
| 291 | job_idx = self.find_job(tag) |
| 292 | for test_idx in self.find_tests(job_idx): |
| 293 | where = {'test_idx' : test_idx} |
| 294 | self.delete('iteration_result', where) |
| 295 | self.delete('test_attributes', where) |
| 296 | where = {'job_idx' : job_idx} |
| 297 | self.delete('tests', where) |
| 298 | self.delete('jobs', where) |
| 299 | |
| 300 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 301 | def insert_job(self, tag, job, commit = None): |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 302 | job.machine_idx = self.lookup_machine(job.machine) |
| 303 | if not job.machine_idx: |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 304 | job.machine_idx = self.insert_machine(job, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 305 | commit=commit) |
mbligh | b10a60f | 2007-10-17 00:03:32 +0000 | [diff] [blame] | 306 | self.insert('jobs', {'tag':tag, |
| 307 | 'label': job.label, |
mbligh | 05067a3 | 2007-12-03 17:48:04 +0000 | [diff] [blame] | 308 | 'username': job.user, |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 309 | 'machine_idx': job.machine_idx, |
| 310 | 'queued_time': job.queued_time, |
| 311 | 'started_time': job.started_time, |
| 312 | 'finished_time': job.finished_time}, |
mbligh | b10a60f | 2007-10-17 00:03:32 +0000 | [diff] [blame] | 313 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 314 | job.index = self.get_last_autonumber_value() |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 315 | for test in job.tests: |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 316 | self.insert_test(job, test, commit=commit) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 317 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 318 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 319 | def insert_test(self, job, test, commit = None): |
| 320 | kver = self.insert_kernel(test.kernel, commit=commit) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 321 | data = {'job_idx':job.index, 'test':test.testname, |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 322 | 'subdir':test.subdir, 'kernel_idx':kver, |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 323 | 'status':self.status_idx[test.status], |
mbligh | c251454 | 2008-02-19 15:54:26 +0000 | [diff] [blame] | 324 | 'reason':test.reason, 'machine_idx':job.machine_idx, |
mbligh | 9cae150 | 2008-04-18 20:40:45 +0000 | [diff] [blame] | 325 | 'started_time': test.started_time, |
mbligh | c251454 | 2008-02-19 15:54:26 +0000 | [diff] [blame] | 326 | 'finished_time':test.finished_time} |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 327 | self.insert('tests', data, commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 328 | test_idx = self.get_last_autonumber_value() |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 329 | data = { 'test_idx':test_idx } |
| 330 | |
| 331 | for i in test.iterations: |
| 332 | data['iteration'] = i.index |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 333 | for key, value in i.attr_keyval.iteritems(): |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 334 | data['attribute'] = key |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 335 | data['value'] = value |
| 336 | self.insert('iteration_attributes', data, |
| 337 | commit=commit) |
| 338 | for key, value in i.perf_keyval.iteritems(): |
| 339 | data['attribute'] = key |
| 340 | data['value'] = value |
| 341 | self.insert('iteration_result', data, |
| 342 | commit=commit) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 343 | |
| 344 | for key, value in test.attributes.iteritems(): |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 345 | data = {'test_idx': test_idx, 'attribute': key, |
| 346 | 'value': value} |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 347 | self.insert('test_attributes', data, commit=commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 348 | |
| 349 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 350 | def read_machine_map(self): |
| 351 | self.machine_group = {} |
| 352 | for line in open(self.machine_map, 'r').readlines(): |
| 353 | (machine, group) = line.split() |
| 354 | self.machine_group[machine] = group |
| 355 | |
| 356 | |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 357 | def insert_machine(self, job, group = None, commit = None): |
| 358 | hostname = job.machine |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 359 | if self.machine_map and not self.machine_group: |
| 360 | self.read_machine_map() |
| 361 | |
| 362 | if not group: |
| 363 | group = self.machine_group.get(hostname, hostname) |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 364 | if group == hostname and job.machine_owner: |
| 365 | group = job.machine_owner + '/' + hostname |
| 366 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 367 | self.insert('machines', |
| 368 | { 'hostname' : hostname , |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 369 | 'machine_group' : group , |
| 370 | 'owner' : job.machine_owner }, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 371 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 372 | return self.get_last_autonumber_value() |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 373 | |
| 374 | |
| 375 | def lookup_machine(self, hostname): |
| 376 | where = { 'hostname' : hostname } |
| 377 | rows = self.select('machine_idx', 'machines', where) |
| 378 | if rows: |
| 379 | return rows[0][0] |
| 380 | else: |
| 381 | return None |
| 382 | |
| 383 | |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 384 | def lookup_kernel(self, kernel): |
| 385 | rows = self.select('kernel_idx', 'kernels', |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 386 | {'kernel_hash':kernel.kernel_hash}) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 387 | if rows: |
| 388 | return rows[0][0] |
| 389 | else: |
| 390 | return None |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 391 | |
| 392 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 393 | def insert_kernel(self, kernel, commit = None): |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 394 | kver = self.lookup_kernel(kernel) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 395 | if kver: |
| 396 | return kver |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 397 | |
| 398 | # If this kernel has any significant patches, append their hash |
| 399 | # as diferentiator. |
| 400 | printable = kernel.base |
| 401 | patch_count = 0 |
| 402 | for patch in kernel.patches: |
| 403 | match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$', |
| 404 | patch.reference) |
| 405 | if not match: |
| 406 | patch_count += 1 |
| 407 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 408 | self.insert('kernels', |
| 409 | {'base':kernel.base, |
| 410 | 'kernel_hash':kernel.kernel_hash, |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 411 | 'printable':printable}, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 412 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 413 | kver = self.get_last_autonumber_value() |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 414 | |
| 415 | if patch_count > 0: |
| 416 | printable += ' p%d' % (kver) |
| 417 | self.update('kernels', |
| 418 | {'printable':printable}, |
| 419 | {'kernel_idx':kver}) |
| 420 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 421 | for patch in kernel.patches: |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 422 | self.insert_patch(kver, patch, commit=commit) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 423 | return kver |
| 424 | |
| 425 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 426 | def insert_patch(self, kver, patch, commit = None): |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 427 | print patch.reference |
| 428 | name = os.path.basename(patch.reference)[:80] |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 429 | self.insert('patches', |
| 430 | {'kernel_idx': kver, |
| 431 | 'name':name, |
| 432 | 'url':patch.reference, |
| 433 | 'hash':patch.hash}, |
| 434 | commit=commit) |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 435 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 436 | |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 437 | def find_test(self, job_idx, subdir): |
| 438 | where = { 'job_idx':job_idx , 'subdir':subdir } |
| 439 | rows = self.select('test_idx', 'tests', where) |
| 440 | if rows: |
| 441 | return rows[0][0] |
| 442 | else: |
| 443 | return None |
| 444 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 445 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 446 | def find_tests(self, job_idx): |
| 447 | where = { 'job_idx':job_idx } |
| 448 | rows = self.select('test_idx', 'tests', where) |
| 449 | if rows: |
| 450 | return [row[0] for row in rows] |
| 451 | else: |
mbligh | 2e57e7b | 2007-11-29 16:10:50 +0000 | [diff] [blame] | 452 | return [] |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 453 | |
| 454 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 455 | def find_job(self, tag): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 456 | rows = self.select('job_idx', 'jobs', {'tag': tag}) |
| 457 | if rows: |
| 458 | return rows[0][0] |
| 459 | else: |
| 460 | return None |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 461 | |
| 462 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 463 | def _get_db_type(): |
| 464 | """Get the database type name to use from the global config.""" |
| 465 | get_value = global_config.global_config.get_config_value |
| 466 | return "db_" + get_value("TKO", "db_type", default="mysql") |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 467 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 468 | |
| 469 | def _get_error_class(class_name): |
| 470 | """Retrieves the appropriate error class by name from the database |
| 471 | module.""" |
| 472 | db_module = __import__("autotest_lib.tko." + _get_db_type(), |
| 473 | globals(), locals(), ["driver"]) |
| 474 | return getattr(db_module.driver, class_name) |
| 475 | |
| 476 | |
| 477 | def db(*args, **dargs): |
| 478 | """Creates an instance of the database class with the arguments |
| 479 | provided in args and dargs, using the database type specified by |
| 480 | the global configuration (defaulting to mysql).""" |
| 481 | db_type = _get_db_type() |
| 482 | db_module = __import__("autotest_lib.tko." + db_type, globals(), |
| 483 | locals(), [db_type]) |
| 484 | db = getattr(db_module, db_type)(*args, **dargs) |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 485 | return db |