blob: 5f3498db8a723655d7cd9ebb346ac48812a93742 [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:
mbligh7a41a862007-11-30 17:44:24 +0000170 job.machine_idx = self.insert_machine(job,
mbligh432bad42007-10-09 19:56:07 +0000171 commit=commit)
mblighb10a60f2007-10-17 00:03:32 +0000172 self.insert('jobs', {'tag':tag,
173 'label': job.label,
mbligh05067a32007-12-03 17:48:04 +0000174 'username': job.user,
mblighb10a60f2007-10-17 00:03:32 +0000175 '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
mbligh7a41a862007-11-30 17:44:24 +0000211 def insert_machine(self, job, group = None, commit = None):
212 hostname = job.machine
mbligh048e1c92007-10-07 00:10:33 +0000213 if self.machine_map and not self.machine_group:
214 self.read_machine_map()
215
216 if not group:
217 group = self.machine_group.get(hostname, hostname)
mbligh7a41a862007-11-30 17:44:24 +0000218 if group == hostname and job.machine_owner:
219 group = job.machine_owner + '/' + hostname
220
mbligh432bad42007-10-09 19:56:07 +0000221 self.insert('machines',
222 { 'hostname' : hostname ,
mbligh7a41a862007-11-30 17:44:24 +0000223 'machine_group' : group ,
224 'owner' : job.machine_owner },
mbligh432bad42007-10-09 19:56:07 +0000225 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000226 return self.lookup_machine(hostname)
227
228
229 def lookup_machine(self, hostname):
230 where = { 'hostname' : hostname }
231 rows = self.select('machine_idx', 'machines', where)
232 if rows:
233 return rows[0][0]
234 else:
235 return None
236
237
mbligh9bb92fe2007-09-12 15:54:23 +0000238 def lookup_kernel(self, kernel):
239 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000240 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000241 if rows:
242 return rows[0][0]
243 else:
244 return None
mblighe9cf9d42007-08-31 08:56:00 +0000245
246
mbligh432bad42007-10-09 19:56:07 +0000247 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000248 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000249 if kver:
250 return kver
mbligh432bad42007-10-09 19:56:07 +0000251 self.insert('kernels',
252 {'base':kernel.base,
253 'kernel_hash':kernel.kernel_hash,
254 'printable':kernel.base},
255 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000256 # WARNING - incorrectly shoving base into printable here.
257 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000258 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000259 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000260 return kver
261
262
mbligh432bad42007-10-09 19:56:07 +0000263 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000264 print patch.reference
265 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000266 self.insert('patches',
267 {'kernel_idx': kver,
268 'name':name,
269 'url':patch.reference,
270 'hash':patch.hash},
271 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000272
mbligh048e1c92007-10-07 00:10:33 +0000273
mbligh2bd48872007-09-20 18:32:25 +0000274 def find_test(self, job_idx, subdir):
275 where = { 'job_idx':job_idx , 'subdir':subdir }
276 rows = self.select('test_idx', 'tests', where)
277 if rows:
278 return rows[0][0]
279 else:
280 return None
281
mbligh056d0d32006-10-08 22:31:10 +0000282
mbligh96b9a5a2007-11-24 19:32:20 +0000283 def find_tests(self, job_idx):
284 where = { 'job_idx':job_idx }
285 rows = self.select('test_idx', 'tests', where)
286 if rows:
287 return [row[0] for row in rows]
288 else:
mbligh2e57e7b2007-11-29 16:10:50 +0000289 return []
mbligh96b9a5a2007-11-24 19:32:20 +0000290
291
mbligh056d0d32006-10-08 22:31:10 +0000292 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000293 rows = self.select('job_idx', 'jobs', {'tag': tag})
294 if rows:
295 return rows[0][0]
296 else:
297 return None