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