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