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