mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 1 | import re, os, sys, types |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 2 | from common import global_config |
| 3 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame^] | 4 | class MySQLTooManyRows(Exception): |
| 5 | pass |
| 6 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 7 | |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 8 | class db_sql: |
mbligh | 2bb6dfd | 2008-01-10 16:38:30 +0000 | [diff] [blame] | 9 | def __init__(self, debug = False, autocommit=True, host = None, |
| 10 | database = None, user = None, password = None): |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 11 | self.debug = debug |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 12 | self.autocommit = autocommit |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 13 | |
mbligh | 6f075f0 | 2008-01-25 16:36:16 +0000 | [diff] [blame] | 14 | path = os.path.dirname(__file__) |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 15 | |
| 16 | # grab the global config |
| 17 | c = global_config.global_config |
| 18 | |
| 19 | # grab the host, database |
| 20 | if not host: |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 21 | host = c.get_config_value("TKO", "host") |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 22 | if not database: |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 23 | database = c.get_config_value("TKO", "database") |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 24 | |
| 25 | # grab the user and password |
| 26 | if not user: |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 27 | user = c.get_config_value("TKO", "user") |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 28 | if not password: |
mbligh | 104e9ce | 2008-03-11 22:01:44 +0000 | [diff] [blame] | 29 | password = c.get_config_value("TKO", "password") |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 30 | |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 31 | self.con = self.connect(host, database, user, password) |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 32 | self.cur = self.con.cursor() |
| 33 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 34 | # if not present, insert statuses |
| 35 | self.status_idx = {} |
| 36 | self.status_word = {} |
mbligh | c82f246 | 2007-10-02 19:19:17 +0000 | [diff] [blame] | 37 | status_rows = self.select('status_idx, word', 'status', None) |
| 38 | for s in status_rows: |
mbligh | 9789445 | 2007-10-05 15:10:33 +0000 | [diff] [blame] | 39 | self.status_idx[s[1]] = s[0] |
mbligh | c82f246 | 2007-10-02 19:19:17 +0000 | [diff] [blame] | 40 | self.status_word[s[0]] = s[1] |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 41 | |
mbligh | 6f075f0 | 2008-01-25 16:36:16 +0000 | [diff] [blame] | 42 | dir = os.path.dirname(__file__) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 43 | machine_map = os.path.join(dir, 'machines') |
| 44 | if os.path.exists(machine_map): |
| 45 | self.machine_map = machine_map |
mbligh | ba9c54c | 2007-10-26 16:41:26 +0000 | [diff] [blame] | 46 | else: |
| 47 | self.machine_map = None |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 48 | self.machine_group = {} |
| 49 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 50 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 51 | def dprint(self, value): |
| 52 | if self.debug: |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 53 | sys.stdout.write('SQL: ' + str(value) + '\n') |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 54 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 55 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 56 | def commit(self): |
| 57 | self.con.commit() |
| 58 | |
| 59 | |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 60 | def get_last_autonumber_value(self): |
| 61 | self.cur.execute('SELECT LAST_INSERT_ID()', []) |
| 62 | return self.cur.fetchall()[0][0] |
| 63 | |
| 64 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame^] | 65 | def select(self, fields, table, where, wherein={}, |
| 66 | distinct = False, group_by = None, max_rows = None): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 67 | """\ |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 68 | This selects all the fields requested from a |
| 69 | specific table with a particular where clause. |
| 70 | The where clause can either be a dictionary of |
| 71 | field=value pairs, a string, or a tuple of (string, |
| 72 | a list of values). The last option is what you |
| 73 | should use when accepting user input as it'll |
| 74 | protect you against sql injection attacks (if |
| 75 | all user data is placed in the array rather than |
| 76 | the raw SQL). |
| 77 | |
| 78 | For example: |
| 79 | where = ("a = %s AND b = %s", ['val', 'val']) |
| 80 | is better than |
| 81 | where = "a = 'val' AND b = 'val'" |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 82 | """ |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 83 | cmd = ['select'] |
| 84 | if distinct: |
| 85 | cmd.append('distinct') |
| 86 | cmd += [fields, 'from', table] |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 87 | |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 88 | values = [] |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 89 | if where and isinstance(where, types.DictionaryType): |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 90 | # key/value pairs (which should be equal) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 91 | keys = [field + '=%s' for field in where.keys()] |
| 92 | values = [where[field] for field in where.keys()] |
| 93 | |
mbligh | 31d29c4 | 2007-09-27 00:51:33 +0000 | [diff] [blame] | 94 | cmd.append(' where ' + ' and '.join(keys)) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 95 | elif where and isinstance(where, types.StringTypes): |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 96 | # the exact string |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 97 | cmd.append(' where ' + where) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 98 | elif where and isinstance(where, types.TupleType): |
| 99 | # preformatted where clause + values |
| 100 | (sql, vals) = where |
| 101 | values = vals |
| 102 | cmd.append(' where (%s) ' % sql) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 103 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 104 | # TODO: this assumes there's a where clause...bad |
mbligh | 85952b4 | 2007-12-07 16:28:33 +0000 | [diff] [blame] | 105 | if wherein and isinstance(wherein, types.DictionaryType): |
| 106 | keys_in = [] |
| 107 | for field_in in wherein.keys(): |
| 108 | keys_in += [field_in + ' in (' + ','.join(wherein[field_in])+') '] |
| 109 | |
| 110 | cmd.append(' and '+' and '.join(keys_in)) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 111 | if group_by: |
| 112 | cmd.append(' GROUP BY ' + group_by) |
| 113 | |
| 114 | self.dprint('%s %s' % (' '.join(cmd), values)) |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame^] | 115 | numRec = self.cur.execute(' '.join(cmd), values) |
| 116 | if max_rows != None and numRec > max_rows: |
| 117 | msg = 'Exceeded allowed number of records' |
| 118 | raise MySQLTooManyRows(msg) |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 119 | return self.cur.fetchall() |
| 120 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 121 | |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 122 | def select_sql(self, fields, table, sql, values): |
| 123 | """\ |
| 124 | select fields from table "sql" |
| 125 | """ |
| 126 | cmd = 'select %s from %s %s' % (fields, table, sql) |
| 127 | self.dprint(cmd) |
| 128 | self.cur.execute(cmd, values) |
| 129 | return self.cur.fetchall() |
| 130 | |
| 131 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 132 | def insert(self, table, data, commit = None): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 133 | """\ |
| 134 | 'insert into table (keys) values (%s ... %s)', values |
| 135 | |
| 136 | data: |
| 137 | dictionary of fields and data |
| 138 | """ |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 139 | if commit == None: |
| 140 | commit = self.autocommit |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 141 | fields = data.keys() |
| 142 | refs = ['%s' for field in fields] |
| 143 | values = [data[field] for field in fields] |
| 144 | cmd = 'insert into %s (%s) values (%s)' % \ |
| 145 | (table, ','.join(fields), ','.join(refs)) |
mbligh | 53d1425 | 2007-09-12 16:33:14 +0000 | [diff] [blame] | 146 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 147 | self.dprint('%s %s' % (cmd,values)) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 148 | self.cur.execute(cmd, values) |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 149 | if commit: |
| 150 | self.con.commit() |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 151 | |
| 152 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 153 | def delete(self, table, where, commit = None): |
| 154 | cmd = ['delete from', table] |
| 155 | if commit == None: |
| 156 | commit = self.autocommit |
| 157 | if where and isinstance(where, types.DictionaryType): |
| 158 | keys = [field + '=%s' for field in where.keys()] |
| 159 | values = [where[field] for field in where.keys()] |
| 160 | cmd += ['where', ' and '.join(keys)] |
| 161 | self.dprint('%s %s' % (' '.join(cmd),values)) |
| 162 | self.cur.execute(' '.join(cmd), values) |
| 163 | if commit: |
| 164 | self.con.commit() |
| 165 | |
| 166 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 167 | def update(self, table, data, where, commit = None): |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 168 | """\ |
| 169 | 'update table set data values (%s ... %s) where ...' |
| 170 | |
| 171 | data: |
| 172 | dictionary of fields and data |
| 173 | """ |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 174 | if commit == None: |
| 175 | commit = self.autocommit |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 176 | cmd = 'update %s ' % table |
| 177 | fields = data.keys() |
| 178 | data_refs = [field + '=%s' for field in fields] |
| 179 | data_values = [data[field] for field in fields] |
| 180 | cmd += ' set ' + ' and '.join(data_refs) |
| 181 | |
| 182 | where_keys = [field + '=%s' for field in where.keys()] |
| 183 | where_values = [where[field] for field in where.keys()] |
| 184 | cmd += ' where ' + ' and '.join(where_keys) |
| 185 | |
| 186 | print '%s %s' % (cmd, data_values + where_values) |
| 187 | self.cur.execute(cmd, data_values + where_values) |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 188 | if commit: |
| 189 | self.con.commit() |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 190 | |
| 191 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 192 | def delete_job(self, tag, commit = None): |
| 193 | job_idx = self.find_job(tag) |
| 194 | for test_idx in self.find_tests(job_idx): |
| 195 | where = {'test_idx' : test_idx} |
| 196 | self.delete('iteration_result', where) |
| 197 | self.delete('test_attributes', where) |
| 198 | where = {'job_idx' : job_idx} |
| 199 | self.delete('tests', where) |
| 200 | self.delete('jobs', where) |
| 201 | |
| 202 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 203 | def insert_job(self, tag, job, commit = None): |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 204 | job.machine_idx = self.lookup_machine(job.machine) |
| 205 | if not job.machine_idx: |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 206 | job.machine_idx = self.insert_machine(job, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 207 | commit=commit) |
mbligh | b10a60f | 2007-10-17 00:03:32 +0000 | [diff] [blame] | 208 | self.insert('jobs', {'tag':tag, |
| 209 | 'label': job.label, |
mbligh | 05067a3 | 2007-12-03 17:48:04 +0000 | [diff] [blame] | 210 | 'username': job.user, |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 211 | 'machine_idx': job.machine_idx, |
| 212 | 'queued_time': job.queued_time, |
| 213 | 'started_time': job.started_time, |
| 214 | 'finished_time': job.finished_time}, |
mbligh | b10a60f | 2007-10-17 00:03:32 +0000 | [diff] [blame] | 215 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 216 | job.index = self.get_last_autonumber_value() |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 217 | for test in job.tests: |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 218 | self.insert_test(job, test, commit=commit) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 219 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 220 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 221 | def insert_test(self, job, test, commit = None): |
| 222 | kver = self.insert_kernel(test.kernel, commit=commit) |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 223 | data = {'job_idx':job.index, 'test':test.testname, |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 224 | 'subdir':test.subdir, 'kernel_idx':kver, |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 225 | 'status':self.status_idx[test.status], |
mbligh | c251454 | 2008-02-19 15:54:26 +0000 | [diff] [blame] | 226 | 'reason':test.reason, 'machine_idx':job.machine_idx, |
| 227 | 'finished_time':test.finished_time} |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 228 | self.insert('tests', data, commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 229 | test_idx = self.get_last_autonumber_value() |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 230 | data = { 'test_idx':test_idx } |
| 231 | |
| 232 | for i in test.iterations: |
| 233 | data['iteration'] = i.index |
| 234 | for key in i.keyval: |
| 235 | data['attribute'] = key |
| 236 | data['value'] = i.keyval[key] |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 237 | self.insert('iteration_result', |
| 238 | data, |
| 239 | commit=commit) |
mbligh | 994a23d | 2007-10-25 15:28:58 +0000 | [diff] [blame] | 240 | data = {'test_idx':test_idx, 'attribute':'version', 'value':test.version} |
| 241 | self.insert('test_attributes', data, commit=commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 242 | |
| 243 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 244 | def read_machine_map(self): |
| 245 | self.machine_group = {} |
| 246 | for line in open(self.machine_map, 'r').readlines(): |
| 247 | (machine, group) = line.split() |
| 248 | self.machine_group[machine] = group |
| 249 | |
| 250 | |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 251 | def insert_machine(self, job, group = None, commit = None): |
| 252 | hostname = job.machine |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 253 | if self.machine_map and not self.machine_group: |
| 254 | self.read_machine_map() |
| 255 | |
| 256 | if not group: |
| 257 | group = self.machine_group.get(hostname, hostname) |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 258 | if group == hostname and job.machine_owner: |
| 259 | group = job.machine_owner + '/' + hostname |
| 260 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 261 | self.insert('machines', |
| 262 | { 'hostname' : hostname , |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 263 | 'machine_group' : group , |
| 264 | 'owner' : job.machine_owner }, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 265 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 266 | return self.get_last_autonumber_value() |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | def lookup_machine(self, hostname): |
| 270 | where = { 'hostname' : hostname } |
| 271 | rows = self.select('machine_idx', 'machines', where) |
| 272 | if rows: |
| 273 | return rows[0][0] |
| 274 | else: |
| 275 | return None |
| 276 | |
| 277 | |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 278 | def lookup_kernel(self, kernel): |
| 279 | rows = self.select('kernel_idx', 'kernels', |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 280 | {'kernel_hash':kernel.kernel_hash}) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 281 | if rows: |
| 282 | return rows[0][0] |
| 283 | else: |
| 284 | return None |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 285 | |
| 286 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 287 | def insert_kernel(self, kernel, commit = None): |
mbligh | 9bb92fe | 2007-09-12 15:54:23 +0000 | [diff] [blame] | 288 | kver = self.lookup_kernel(kernel) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 289 | if kver: |
| 290 | return kver |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 291 | |
| 292 | # If this kernel has any significant patches, append their hash |
| 293 | # as diferentiator. |
| 294 | printable = kernel.base |
| 295 | patch_count = 0 |
| 296 | for patch in kernel.patches: |
| 297 | match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$', |
| 298 | patch.reference) |
| 299 | if not match: |
| 300 | patch_count += 1 |
| 301 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 302 | self.insert('kernels', |
| 303 | {'base':kernel.base, |
| 304 | 'kernel_hash':kernel.kernel_hash, |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 305 | 'printable':printable}, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 306 | commit=commit) |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 307 | kver = self.get_last_autonumber_value() |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 308 | |
| 309 | if patch_count > 0: |
| 310 | printable += ' p%d' % (kver) |
| 311 | self.update('kernels', |
| 312 | {'printable':printable}, |
| 313 | {'kernel_idx':kver}) |
| 314 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 315 | for patch in kernel.patches: |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 316 | self.insert_patch(kver, patch, commit=commit) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 317 | return kver |
| 318 | |
| 319 | |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 320 | def insert_patch(self, kver, patch, commit = None): |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 321 | print patch.reference |
| 322 | name = os.path.basename(patch.reference)[:80] |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 323 | self.insert('patches', |
| 324 | {'kernel_idx': kver, |
| 325 | 'name':name, |
| 326 | 'url':patch.reference, |
| 327 | 'hash':patch.hash}, |
| 328 | commit=commit) |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 329 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 330 | |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 331 | def find_test(self, job_idx, subdir): |
| 332 | where = { 'job_idx':job_idx , 'subdir':subdir } |
| 333 | rows = self.select('test_idx', 'tests', where) |
| 334 | if rows: |
| 335 | return rows[0][0] |
| 336 | else: |
| 337 | return None |
| 338 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 339 | |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 340 | def find_tests(self, job_idx): |
| 341 | where = { 'job_idx':job_idx } |
| 342 | rows = self.select('test_idx', 'tests', where) |
| 343 | if rows: |
| 344 | return [row[0] for row in rows] |
| 345 | else: |
mbligh | 2e57e7b | 2007-11-29 16:10:50 +0000 | [diff] [blame] | 346 | return [] |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 347 | |
| 348 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 349 | def find_job(self, tag): |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 350 | rows = self.select('job_idx', 'jobs', {'tag': tag}) |
| 351 | if rows: |
| 352 | return rows[0][0] |
| 353 | else: |
| 354 | return None |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | # Use a class method as a class factory, generating a relevant database object. |
| 358 | def db(*args, **dargs): |
mbligh | 6f075f0 | 2008-01-25 16:36:16 +0000 | [diff] [blame] | 359 | path = os.path.dirname(__file__) |
mbligh | a044e56 | 2007-12-07 16:26:13 +0000 | [diff] [blame] | 360 | db_type = None |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 361 | |
| 362 | # read db_type from global config |
| 363 | c = global_config.global_config |
mbligh | e82a93e | 2008-03-11 22:34:31 +0000 | [diff] [blame] | 364 | db_type = c.get_config_value("TKO", "db_type", default="mysql") |
mbligh | a044e56 | 2007-12-07 16:26:13 +0000 | [diff] [blame] | 365 | db_type = 'db_' + db_type |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 366 | exec ('import %s; db = %s.%s(*args, **dargs)' |
| 367 | % (db_type, db_type, db_type)) |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 368 | |
| 369 | return db |