blob: cece69a7b0cab17a7a9caea00e81dd29414a98c5 [file] [log] [blame]
mblighaf25f062007-12-03 17:48:35 +00001import re, os, sys, types
mblighd5c33db2006-10-08 21:34:16 +00002
mblighaf25f062007-12-03 17:48:35 +00003class db_sql:
mbligh432bad42007-10-09 19:56:07 +00004 def __init__(self, debug = False, autocommit=True):
mbligh8e1ab172007-09-13 17:29:56 +00005 self.debug = debug
mbligh432bad42007-10-09 19:56:07 +00006 self.autocommit = autocommit
mbligha218d112007-10-03 20:15:09 +00007
8 path = os.path.dirname(os.path.abspath(sys.argv[0]))
mblighb32cd432007-09-25 18:20:04 +00009 try:
mbligha218d112007-10-03 20:15:09 +000010 file = os.path.join(path, '.database')
mbligh432bad42007-10-09 19:56:07 +000011 db_prefs = open(file, 'r')
mbligh728ff0a2007-09-27 17:05:12 +000012 host = db_prefs.readline().rstrip()
13 database = db_prefs.readline().rstrip()
14 except:
15 host = 'localhost'
16 database = 'tko'
17
18 try:
mbligha218d112007-10-03 20:15:09 +000019 file = os.path.join(path, '.priv_login')
20 login = open(file, 'r')
mblighb32cd432007-09-25 18:20:04 +000021 user = login.readline().rstrip()
22 password = login.readline().rstrip()
mbligh95fca572007-09-27 17:11:00 +000023 except:
24 try:
mbligha218d112007-10-03 20:15:09 +000025 file = os.path.join(path, '.unpriv_login')
mbligh432bad42007-10-09 19:56:07 +000026 login = open(file, 'r')
mbligh95fca572007-09-27 17:11:00 +000027 user = login.readline().rstrip()
28 password = login.readline().rstrip()
29 except:
30 user = 'nobody'
31 password = ''
mblighb32cd432007-09-25 18:20:04 +000032
mblighaf25f062007-12-03 17:48:35 +000033 self.con = self.connect(host, database, user, password)
mblighd5c33db2006-10-08 21:34:16 +000034 self.cur = self.con.cursor()
35
mbligh8e1ab172007-09-13 17:29:56 +000036 # if not present, insert statuses
37 self.status_idx = {}
38 self.status_word = {}
mblighc82f2462007-10-02 19:19:17 +000039 status_rows = self.select('status_idx, word', 'status', None)
40 for s in status_rows:
mbligh97894452007-10-05 15:10:33 +000041 self.status_idx[s[1]] = s[0]
mblighc82f2462007-10-02 19:19:17 +000042 self.status_word[s[0]] = s[1]
mbligh048e1c92007-10-07 00:10:33 +000043
44 dir = os.path.abspath(os.path.dirname(sys.argv[0]))
45 machine_map = os.path.join(dir, 'machines')
46 if os.path.exists(machine_map):
47 self.machine_map = machine_map
mblighba9c54c2007-10-26 16:41:26 +000048 else:
49 self.machine_map = None
mbligh048e1c92007-10-07 00:10:33 +000050 self.machine_group = {}
51
mbligh8e1ab172007-09-13 17:29:56 +000052
mbligh8e1ab172007-09-13 17:29:56 +000053 def dprint(self, value):
54 if self.debug:
55 sys.stderr.write('SQL: ' + str(value) + '\n')
56
mblighd5c33db2006-10-08 21:34:16 +000057
mbligh432bad42007-10-09 19:56:07 +000058 def commit(self):
59 self.con.commit()
60
61
mbligh31d29c42007-09-27 00:51:33 +000062 def select(self, fields, table, where, distinct = False):
mbligh608c3252007-08-31 13:53:00 +000063 """\
64 select fields from table where {dictionary}
65 """
mbligh31d29c42007-09-27 00:51:33 +000066 cmd = ['select']
67 if distinct:
68 cmd.append('distinct')
69 cmd += [fields, 'from', table]
mbligh608c3252007-08-31 13:53:00 +000070
mbligh31d29c42007-09-27 00:51:33 +000071 values = []
mbligh414c69e2007-10-05 15:13:06 +000072 if where and isinstance(where, types.DictionaryType):
mbligh53d14252007-09-12 16:33:14 +000073 keys = [field + '=%s' for field in where.keys()]
74 values = [where[field] for field in where.keys()]
75
mbligh31d29c42007-09-27 00:51:33 +000076 cmd.append(' where ' + ' and '.join(keys))
mbligh414c69e2007-10-05 15:13:06 +000077 elif where and isinstance(where, types.StringTypes):
78 cmd.append(' where ' + where)
mbligh53d14252007-09-12 16:33:14 +000079
mbligh31d29c42007-09-27 00:51:33 +000080 self.dprint('%s %s' % (' '.join(cmd),values))
81 self.cur.execute(' '.join(cmd), values)
mblighd5c33db2006-10-08 21:34:16 +000082 return self.cur.fetchall()
83
mbligh056d0d32006-10-08 22:31:10 +000084
mbligh414c69e2007-10-05 15:13:06 +000085 def select_sql(self, fields, table, sql, values):
86 """\
87 select fields from table "sql"
88 """
89 cmd = 'select %s from %s %s' % (fields, table, sql)
90 self.dprint(cmd)
91 self.cur.execute(cmd, values)
92 return self.cur.fetchall()
93
94
mbligh432bad42007-10-09 19:56:07 +000095 def insert(self, table, data, commit = None):
mbligh608c3252007-08-31 13:53:00 +000096 """\
97 'insert into table (keys) values (%s ... %s)', values
98
99 data:
100 dictionary of fields and data
101 """
mbligh432bad42007-10-09 19:56:07 +0000102 if commit == None:
103 commit = self.autocommit
mbligh608c3252007-08-31 13:53:00 +0000104 fields = data.keys()
105 refs = ['%s' for field in fields]
106 values = [data[field] for field in fields]
107 cmd = 'insert into %s (%s) values (%s)' % \
108 (table, ','.join(fields), ','.join(refs))
mbligh53d14252007-09-12 16:33:14 +0000109
mbligh8e1ab172007-09-13 17:29:56 +0000110 self.dprint('%s %s' % (cmd,values))
mbligh608c3252007-08-31 13:53:00 +0000111 self.cur.execute(cmd, values)
mbligh432bad42007-10-09 19:56:07 +0000112 if commit:
113 self.con.commit()
mbligh608c3252007-08-31 13:53:00 +0000114
115
mbligh96b9a5a2007-11-24 19:32:20 +0000116 def delete(self, table, where, commit = None):
117 cmd = ['delete from', table]
118 if commit == None:
119 commit = self.autocommit
120 if where and isinstance(where, types.DictionaryType):
121 keys = [field + '=%s' for field in where.keys()]
122 values = [where[field] for field in where.keys()]
123 cmd += ['where', ' and '.join(keys)]
124 self.dprint('%s %s' % (' '.join(cmd),values))
125 self.cur.execute(' '.join(cmd), values)
126 if commit:
127 self.con.commit()
128
129
mbligh432bad42007-10-09 19:56:07 +0000130 def update(self, table, data, where, commit = None):
mbligh414c69e2007-10-05 15:13:06 +0000131 """\
132 'update table set data values (%s ... %s) where ...'
133
134 data:
135 dictionary of fields and data
136 """
mbligh432bad42007-10-09 19:56:07 +0000137 if commit == None:
138 commit = self.autocommit
mbligh414c69e2007-10-05 15:13:06 +0000139 cmd = 'update %s ' % table
140 fields = data.keys()
141 data_refs = [field + '=%s' for field in fields]
142 data_values = [data[field] for field in fields]
143 cmd += ' set ' + ' and '.join(data_refs)
144
145 where_keys = [field + '=%s' for field in where.keys()]
146 where_values = [where[field] for field in where.keys()]
147 cmd += ' where ' + ' and '.join(where_keys)
148
149 print '%s %s' % (cmd, data_values + where_values)
150 self.cur.execute(cmd, data_values + where_values)
mbligh432bad42007-10-09 19:56:07 +0000151 if commit:
152 self.con.commit()
mbligh414c69e2007-10-05 15:13:06 +0000153
154
mbligh96b9a5a2007-11-24 19:32:20 +0000155 def delete_job(self, tag, commit = None):
156 job_idx = self.find_job(tag)
157 for test_idx in self.find_tests(job_idx):
158 where = {'test_idx' : test_idx}
159 self.delete('iteration_result', where)
160 self.delete('test_attributes', where)
161 where = {'job_idx' : job_idx}
162 self.delete('tests', where)
163 self.delete('jobs', where)
164
165
mbligh432bad42007-10-09 19:56:07 +0000166 def insert_job(self, tag, job, commit = None):
mbligh2aaeb672007-10-01 14:54:18 +0000167 job.machine_idx = self.lookup_machine(job.machine)
168 if not job.machine_idx:
mbligh7a41a862007-11-30 17:44:24 +0000169 job.machine_idx = self.insert_machine(job,
mbligh432bad42007-10-09 19:56:07 +0000170 commit=commit)
mblighb10a60f2007-10-17 00:03:32 +0000171 self.insert('jobs', {'tag':tag,
172 'label': job.label,
mbligh05067a32007-12-03 17:48:04 +0000173 'username': job.user,
mblighb10a60f2007-10-17 00:03:32 +0000174 'machine_idx':job.machine_idx},
175 commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000176 job.index = self.find_job(tag)
177 for test in job.tests:
mbligh432bad42007-10-09 19:56:07 +0000178 self.insert_test(job, test, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000179
mbligh96b9a5a2007-11-24 19:32:20 +0000180
mbligh432bad42007-10-09 19:56:07 +0000181 def insert_test(self, job, test, commit = None):
182 kver = self.insert_kernel(test.kernel, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000183 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000184 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000185 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000186 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh432bad42007-10-09 19:56:07 +0000187 self.insert('tests', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000188 test_idx = self.find_test(job.index, test.subdir)
189 data = { 'test_idx':test_idx }
190
191 for i in test.iterations:
192 data['iteration'] = i.index
193 for key in i.keyval:
194 data['attribute'] = key
195 data['value'] = i.keyval[key]
mbligh432bad42007-10-09 19:56:07 +0000196 self.insert('iteration_result',
197 data,
198 commit=commit)
mbligh994a23d2007-10-25 15:28:58 +0000199 data = {'test_idx':test_idx, 'attribute':'version', 'value':test.version}
200 self.insert('test_attributes', data, commit=commit)
mblighe9cf9d42007-08-31 08:56:00 +0000201
202
mbligh048e1c92007-10-07 00:10:33 +0000203 def read_machine_map(self):
204 self.machine_group = {}
205 for line in open(self.machine_map, 'r').readlines():
206 (machine, group) = line.split()
207 self.machine_group[machine] = group
208
209
mbligh7a41a862007-11-30 17:44:24 +0000210 def insert_machine(self, job, group = None, commit = None):
211 hostname = job.machine
mbligh048e1c92007-10-07 00:10:33 +0000212 if self.machine_map and not self.machine_group:
213 self.read_machine_map()
214
215 if not group:
216 group = self.machine_group.get(hostname, hostname)
mbligh7a41a862007-11-30 17:44:24 +0000217 if group == hostname and job.machine_owner:
218 group = job.machine_owner + '/' + hostname
219
mbligh432bad42007-10-09 19:56:07 +0000220 self.insert('machines',
221 { 'hostname' : hostname ,
mbligh7a41a862007-11-30 17:44:24 +0000222 'machine_group' : group ,
223 'owner' : job.machine_owner },
mbligh432bad42007-10-09 19:56:07 +0000224 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000225 return self.lookup_machine(hostname)
226
227
228 def lookup_machine(self, hostname):
229 where = { 'hostname' : hostname }
230 rows = self.select('machine_idx', 'machines', where)
231 if rows:
232 return rows[0][0]
233 else:
234 return None
235
236
mbligh9bb92fe2007-09-12 15:54:23 +0000237 def lookup_kernel(self, kernel):
238 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000239 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000240 if rows:
241 return rows[0][0]
242 else:
243 return None
mblighe9cf9d42007-08-31 08:56:00 +0000244
245
mbligh432bad42007-10-09 19:56:07 +0000246 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000247 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000248 if kver:
249 return kver
mbligh432bad42007-10-09 19:56:07 +0000250 self.insert('kernels',
251 {'base':kernel.base,
252 'kernel_hash':kernel.kernel_hash,
253 'printable':kernel.base},
254 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000255 # WARNING - incorrectly shoving base into printable here.
256 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000257 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000258 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000259 return kver
260
261
mbligh432bad42007-10-09 19:56:07 +0000262 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000263 print patch.reference
264 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000265 self.insert('patches',
266 {'kernel_idx': kver,
267 'name':name,
268 'url':patch.reference,
269 'hash':patch.hash},
270 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000271
mbligh048e1c92007-10-07 00:10:33 +0000272
mbligh2bd48872007-09-20 18:32:25 +0000273 def find_test(self, job_idx, subdir):
274 where = { 'job_idx':job_idx , 'subdir':subdir }
275 rows = self.select('test_idx', 'tests', where)
276 if rows:
277 return rows[0][0]
278 else:
279 return None
280
mbligh056d0d32006-10-08 22:31:10 +0000281
mbligh96b9a5a2007-11-24 19:32:20 +0000282 def find_tests(self, job_idx):
283 where = { 'job_idx':job_idx }
284 rows = self.select('test_idx', 'tests', where)
285 if rows:
286 return [row[0] for row in rows]
287 else:
mbligh2e57e7b2007-11-29 16:10:50 +0000288 return []
mbligh96b9a5a2007-11-24 19:32:20 +0000289
290
mbligh056d0d32006-10-08 22:31:10 +0000291 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000292 rows = self.select('job_idx', 'jobs', {'tag': tag})
293 if rows:
294 return rows[0][0]
295 else:
296 return None
mblighaf25f062007-12-03 17:48:35 +0000297
298
299# Use a class method as a class factory, generating a relevant database object.
300def db(*args, **dargs):
301 path = os.path.dirname(os.path.abspath(sys.argv[0]))
mbligha044e562007-12-07 16:26:13 +0000302 db_type = None
mblighaf25f062007-12-03 17:48:35 +0000303 try:
mbligha044e562007-12-07 16:26:13 +0000304 db_file = os.path.join(path, '.database')
305 db_prefs = open(db_file, 'r')
mblighaf25f062007-12-03 17:48:35 +0000306 host = db_prefs.readline().rstrip()
307 database = db_prefs.readline().rstrip()
mbligha044e562007-12-07 16:26:13 +0000308 db_type = db_prefs.readline().rstrip()
309 except:
310 pass
mblighaf25f062007-12-03 17:48:35 +0000311
mbligha044e562007-12-07 16:26:13 +0000312 if not db_type:
313 db_type = 'mysql'
314
315 db_type = 'db_' + db_type
316 exec 'import %s; db = %s.%s(*args, **dargs)' % (db_type, db_type, db_type)
mblighaf25f062007-12-03 17:48:35 +0000317
318 return db