blob: d76312ed1d7d5ea5359d37de9f911dc4c47a8214 [file] [log] [blame]
mbligh97894452007-10-05 15:10:33 +00001import MySQLdb, re, os, sys, types
mblighd5c33db2006-10-08 21:34:16 +00002
3class db:
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
33 self.con = MySQLdb.connect(host=host, user=user,
mbligh95fca572007-09-27 17:11:00 +000034 passwd=password, db=database)
mblighd5c33db2006-10-08 21:34:16 +000035 self.cur = self.con.cursor()
36
mbligh8e1ab172007-09-13 17:29:56 +000037 # if not present, insert statuses
38 self.status_idx = {}
39 self.status_word = {}
mblighc82f2462007-10-02 19:19:17 +000040 status_rows = self.select('status_idx, word', 'status', None)
41 for s in status_rows:
mbligh97894452007-10-05 15:10:33 +000042 self.status_idx[s[1]] = s[0]
mblighc82f2462007-10-02 19:19:17 +000043 self.status_word[s[0]] = s[1]
mbligh048e1c92007-10-07 00:10:33 +000044
45 dir = os.path.abspath(os.path.dirname(sys.argv[0]))
46 machine_map = os.path.join(dir, 'machines')
47 if os.path.exists(machine_map):
48 self.machine_map = machine_map
mblighba9c54c2007-10-26 16:41:26 +000049 else:
50 self.machine_map = None
mbligh048e1c92007-10-07 00:10:33 +000051 self.machine_group = {}
52
mbligh8e1ab172007-09-13 17:29:56 +000053
mbligh8e1ab172007-09-13 17:29:56 +000054 def dprint(self, value):
55 if self.debug:
56 sys.stderr.write('SQL: ' + str(value) + '\n')
57
mblighd5c33db2006-10-08 21:34:16 +000058
mbligh432bad42007-10-09 19:56:07 +000059 def commit(self):
60 self.con.commit()
61
62
mbligh31d29c42007-09-27 00:51:33 +000063 def select(self, fields, table, where, distinct = False):
mbligh608c3252007-08-31 13:53:00 +000064 """\
65 select fields from table where {dictionary}
66 """
mbligh31d29c42007-09-27 00:51:33 +000067 cmd = ['select']
68 if distinct:
69 cmd.append('distinct')
70 cmd += [fields, 'from', table]
mbligh608c3252007-08-31 13:53:00 +000071
mbligh31d29c42007-09-27 00:51:33 +000072 values = []
mbligh414c69e2007-10-05 15:13:06 +000073 if where and isinstance(where, types.DictionaryType):
mbligh53d14252007-09-12 16:33:14 +000074 keys = [field + '=%s' for field in where.keys()]
75 values = [where[field] for field in where.keys()]
76
mbligh31d29c42007-09-27 00:51:33 +000077 cmd.append(' where ' + ' and '.join(keys))
mbligh414c69e2007-10-05 15:13:06 +000078 elif where and isinstance(where, types.StringTypes):
79 cmd.append(' where ' + where)
mbligh53d14252007-09-12 16:33:14 +000080
mbligh31d29c42007-09-27 00:51:33 +000081 self.dprint('%s %s' % (' '.join(cmd),values))
82 self.cur.execute(' '.join(cmd), values)
mblighd5c33db2006-10-08 21:34:16 +000083 return self.cur.fetchall()
84
mbligh056d0d32006-10-08 22:31:10 +000085
mbligh414c69e2007-10-05 15:13:06 +000086 def select_sql(self, fields, table, sql, values):
87 """\
88 select fields from table "sql"
89 """
90 cmd = 'select %s from %s %s' % (fields, table, sql)
91 self.dprint(cmd)
92 self.cur.execute(cmd, values)
93 return self.cur.fetchall()
94
95
mbligh432bad42007-10-09 19:56:07 +000096 def insert(self, table, data, commit = None):
mbligh608c3252007-08-31 13:53:00 +000097 """\
98 'insert into table (keys) values (%s ... %s)', values
99
100 data:
101 dictionary of fields and data
102 """
mbligh432bad42007-10-09 19:56:07 +0000103 if commit == None:
104 commit = self.autocommit
mbligh608c3252007-08-31 13:53:00 +0000105 fields = data.keys()
106 refs = ['%s' for field in fields]
107 values = [data[field] for field in fields]
108 cmd = 'insert into %s (%s) values (%s)' % \
109 (table, ','.join(fields), ','.join(refs))
mbligh53d14252007-09-12 16:33:14 +0000110
mbligh8e1ab172007-09-13 17:29:56 +0000111 self.dprint('%s %s' % (cmd,values))
mbligh608c3252007-08-31 13:53:00 +0000112 self.cur.execute(cmd, values)
mbligh432bad42007-10-09 19:56:07 +0000113 if commit:
114 self.con.commit()
mbligh608c3252007-08-31 13:53:00 +0000115
116
mbligh96b9a5a2007-11-24 19:32:20 +0000117 def delete(self, table, where, commit = None):
118 cmd = ['delete from', table]
119 if commit == None:
120 commit = self.autocommit
121 if where and isinstance(where, types.DictionaryType):
122 keys = [field + '=%s' for field in where.keys()]
123 values = [where[field] for field in where.keys()]
124 cmd += ['where', ' and '.join(keys)]
125 self.dprint('%s %s' % (' '.join(cmd),values))
126 self.cur.execute(' '.join(cmd), values)
127 if commit:
128 self.con.commit()
129
130
mbligh432bad42007-10-09 19:56:07 +0000131 def update(self, table, data, where, commit = None):
mbligh414c69e2007-10-05 15:13:06 +0000132 """\
133 'update table set data values (%s ... %s) where ...'
134
135 data:
136 dictionary of fields and data
137 """
mbligh432bad42007-10-09 19:56:07 +0000138 if commit == None:
139 commit = self.autocommit
mbligh414c69e2007-10-05 15:13:06 +0000140 cmd = 'update %s ' % table
141 fields = data.keys()
142 data_refs = [field + '=%s' for field in fields]
143 data_values = [data[field] for field in fields]
144 cmd += ' set ' + ' and '.join(data_refs)
145
146 where_keys = [field + '=%s' for field in where.keys()]
147 where_values = [where[field] for field in where.keys()]
148 cmd += ' where ' + ' and '.join(where_keys)
149
150 print '%s %s' % (cmd, data_values + where_values)
151 self.cur.execute(cmd, data_values + where_values)
mbligh432bad42007-10-09 19:56:07 +0000152 if commit:
153 self.con.commit()
mbligh414c69e2007-10-05 15:13:06 +0000154
155
mbligh96b9a5a2007-11-24 19:32:20 +0000156 def delete_job(self, tag, commit = None):
157 job_idx = self.find_job(tag)
158 for test_idx in self.find_tests(job_idx):
159 where = {'test_idx' : test_idx}
160 self.delete('iteration_result', where)
161 self.delete('test_attributes', where)
162 where = {'job_idx' : job_idx}
163 self.delete('tests', where)
164 self.delete('jobs', where)
165
166
mbligh432bad42007-10-09 19:56:07 +0000167 def insert_job(self, tag, job, commit = None):
mbligh2aaeb672007-10-01 14:54:18 +0000168 job.machine_idx = self.lookup_machine(job.machine)
169 if not job.machine_idx:
mbligh432bad42007-10-09 19:56:07 +0000170 job.machine_idx = self.insert_machine(job.machine,
171 commit=commit)
mblighb10a60f2007-10-17 00:03:32 +0000172 self.insert('jobs', {'tag':tag,
173 'label': job.label,
174 'user': job.user,
175 'machine_idx':job.machine_idx},
176 commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000177 job.index = self.find_job(tag)
178 for test in job.tests:
mbligh432bad42007-10-09 19:56:07 +0000179 self.insert_test(job, test, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000180
mbligh96b9a5a2007-11-24 19:32:20 +0000181
mbligh432bad42007-10-09 19:56:07 +0000182 def insert_test(self, job, test, commit = None):
183 kver = self.insert_kernel(test.kernel, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000184 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000185 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000186 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000187 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh432bad42007-10-09 19:56:07 +0000188 self.insert('tests', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000189 test_idx = self.find_test(job.index, test.subdir)
190 data = { 'test_idx':test_idx }
191
192 for i in test.iterations:
193 data['iteration'] = i.index
194 for key in i.keyval:
195 data['attribute'] = key
196 data['value'] = i.keyval[key]
mbligh432bad42007-10-09 19:56:07 +0000197 self.insert('iteration_result',
198 data,
199 commit=commit)
mbligh994a23d2007-10-25 15:28:58 +0000200 data = {'test_idx':test_idx, 'attribute':'version', 'value':test.version}
201 self.insert('test_attributes', data, commit=commit)
mblighe9cf9d42007-08-31 08:56:00 +0000202
203
mbligh048e1c92007-10-07 00:10:33 +0000204 def read_machine_map(self):
205 self.machine_group = {}
206 for line in open(self.machine_map, 'r').readlines():
207 (machine, group) = line.split()
208 self.machine_group[machine] = group
209
210
mbligh432bad42007-10-09 19:56:07 +0000211 def insert_machine(self, hostname, group = None, commit = None):
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)
217
mbligh432bad42007-10-09 19:56:07 +0000218 self.insert('machines',
219 { 'hostname' : hostname ,
220 'machine_group' : group },
221 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000222 return self.lookup_machine(hostname)
223
224
225 def lookup_machine(self, hostname):
226 where = { 'hostname' : hostname }
227 rows = self.select('machine_idx', 'machines', where)
228 if rows:
229 return rows[0][0]
230 else:
231 return None
232
233
mbligh9bb92fe2007-09-12 15:54:23 +0000234 def lookup_kernel(self, kernel):
235 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000236 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000237 if rows:
238 return rows[0][0]
239 else:
240 return None
mblighe9cf9d42007-08-31 08:56:00 +0000241
242
mbligh432bad42007-10-09 19:56:07 +0000243 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000244 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000245 if kver:
246 return kver
mbligh432bad42007-10-09 19:56:07 +0000247 self.insert('kernels',
248 {'base':kernel.base,
249 'kernel_hash':kernel.kernel_hash,
250 'printable':kernel.base},
251 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000252 # WARNING - incorrectly shoving base into printable here.
253 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000254 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000255 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000256 return kver
257
258
mbligh432bad42007-10-09 19:56:07 +0000259 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000260 print patch.reference
261 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000262 self.insert('patches',
263 {'kernel_idx': kver,
264 'name':name,
265 'url':patch.reference,
266 'hash':patch.hash},
267 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000268
mbligh048e1c92007-10-07 00:10:33 +0000269
mbligh2bd48872007-09-20 18:32:25 +0000270 def find_test(self, job_idx, subdir):
271 where = { 'job_idx':job_idx , 'subdir':subdir }
272 rows = self.select('test_idx', 'tests', where)
273 if rows:
274 return rows[0][0]
275 else:
276 return None
277
mbligh056d0d32006-10-08 22:31:10 +0000278
mbligh96b9a5a2007-11-24 19:32:20 +0000279 def find_tests(self, job_idx):
280 where = { 'job_idx':job_idx }
281 rows = self.select('test_idx', 'tests', where)
282 if rows:
283 return [row[0] for row in rows]
284 else:
285 return None
286
287
mbligh056d0d32006-10-08 22:31:10 +0000288 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000289 rows = self.select('job_idx', 'jobs', {'tag': tag})
290 if rows:
291 return rows[0][0]
292 else:
293 return None