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