blob: 7b1f265c758f03d38262b6b3934ec0df4a35c970 [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
mbligh432bad42007-10-09 19:56:07 +0000117 def update(self, table, data, where, commit = None):
mbligh414c69e2007-10-05 15:13:06 +0000118 """\
119 'update table set data values (%s ... %s) where ...'
120
121 data:
122 dictionary of fields and data
123 """
mbligh432bad42007-10-09 19:56:07 +0000124 if commit == None:
125 commit = self.autocommit
mbligh414c69e2007-10-05 15:13:06 +0000126 cmd = 'update %s ' % table
127 fields = data.keys()
128 data_refs = [field + '=%s' for field in fields]
129 data_values = [data[field] for field in fields]
130 cmd += ' set ' + ' and '.join(data_refs)
131
132 where_keys = [field + '=%s' for field in where.keys()]
133 where_values = [where[field] for field in where.keys()]
134 cmd += ' where ' + ' and '.join(where_keys)
135
136 print '%s %s' % (cmd, data_values + where_values)
137 self.cur.execute(cmd, data_values + where_values)
mbligh432bad42007-10-09 19:56:07 +0000138 if commit:
139 self.con.commit()
mbligh414c69e2007-10-05 15:13:06 +0000140
141
mbligh432bad42007-10-09 19:56:07 +0000142 def insert_job(self, tag, job, commit = None):
mbligh2aaeb672007-10-01 14:54:18 +0000143 job.machine_idx = self.lookup_machine(job.machine)
144 if not job.machine_idx:
mbligh432bad42007-10-09 19:56:07 +0000145 job.machine_idx = self.insert_machine(job.machine,
146 commit=commit)
mblighb10a60f2007-10-17 00:03:32 +0000147 self.insert('jobs', {'tag':tag,
148 'label': job.label,
149 'user': job.user,
150 'machine_idx':job.machine_idx},
151 commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000152 job.index = self.find_job(tag)
153 for test in job.tests:
mbligh432bad42007-10-09 19:56:07 +0000154 self.insert_test(job, test, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000155
mbligh432bad42007-10-09 19:56:07 +0000156 def insert_test(self, job, test, commit = None):
157 kver = self.insert_kernel(test.kernel, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000158 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000159 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000160 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000161 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh432bad42007-10-09 19:56:07 +0000162 self.insert('tests', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000163 test_idx = self.find_test(job.index, test.subdir)
164 data = { 'test_idx':test_idx }
165
166 for i in test.iterations:
167 data['iteration'] = i.index
168 for key in i.keyval:
169 data['attribute'] = key
170 data['value'] = i.keyval[key]
mbligh432bad42007-10-09 19:56:07 +0000171 self.insert('iteration_result',
172 data,
173 commit=commit)
mbligh994a23d2007-10-25 15:28:58 +0000174 data = {'test_idx':test_idx, 'attribute':'version', 'value':test.version}
175 self.insert('test_attributes', data, commit=commit)
mblighe9cf9d42007-08-31 08:56:00 +0000176
177
mbligh048e1c92007-10-07 00:10:33 +0000178 def read_machine_map(self):
179 self.machine_group = {}
180 for line in open(self.machine_map, 'r').readlines():
181 (machine, group) = line.split()
182 self.machine_group[machine] = group
183
184
mbligh432bad42007-10-09 19:56:07 +0000185 def insert_machine(self, hostname, group = None, commit = None):
mbligh048e1c92007-10-07 00:10:33 +0000186 if self.machine_map and not self.machine_group:
187 self.read_machine_map()
188
189 if not group:
190 group = self.machine_group.get(hostname, hostname)
191
mbligh432bad42007-10-09 19:56:07 +0000192 self.insert('machines',
193 { 'hostname' : hostname ,
194 'machine_group' : group },
195 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000196 return self.lookup_machine(hostname)
197
198
199 def lookup_machine(self, hostname):
200 where = { 'hostname' : hostname }
201 rows = self.select('machine_idx', 'machines', where)
202 if rows:
203 return rows[0][0]
204 else:
205 return None
206
207
mbligh9bb92fe2007-09-12 15:54:23 +0000208 def lookup_kernel(self, kernel):
209 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000210 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000211 if rows:
212 return rows[0][0]
213 else:
214 return None
mblighe9cf9d42007-08-31 08:56:00 +0000215
216
mbligh432bad42007-10-09 19:56:07 +0000217 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000218 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000219 if kver:
220 return kver
mbligh432bad42007-10-09 19:56:07 +0000221 self.insert('kernels',
222 {'base':kernel.base,
223 'kernel_hash':kernel.kernel_hash,
224 'printable':kernel.base},
225 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000226 # WARNING - incorrectly shoving base into printable here.
227 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000228 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000229 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000230 return kver
231
232
mbligh432bad42007-10-09 19:56:07 +0000233 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000234 print patch.reference
235 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000236 self.insert('patches',
237 {'kernel_idx': kver,
238 'name':name,
239 'url':patch.reference,
240 'hash':patch.hash},
241 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000242
mbligh048e1c92007-10-07 00:10:33 +0000243
mbligh2bd48872007-09-20 18:32:25 +0000244 def find_test(self, job_idx, subdir):
245 where = { 'job_idx':job_idx , 'subdir':subdir }
246 rows = self.select('test_idx', 'tests', where)
247 if rows:
248 return rows[0][0]
249 else:
250 return None
251
mbligh056d0d32006-10-08 22:31:10 +0000252
253 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000254 rows = self.select('job_idx', 'jobs', {'tag': tag})
255 if rows:
256 return rows[0][0]
257 else:
258 return None