blob: 5e6cc294676d067b5acdc725a8e2d4c6bfc2328e [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
49 self.machine_group = {}
50
mbligh8e1ab172007-09-13 17:29:56 +000051
mbligh8e1ab172007-09-13 17:29:56 +000052 def dprint(self, value):
53 if self.debug:
54 sys.stderr.write('SQL: ' + str(value) + '\n')
55
mblighd5c33db2006-10-08 21:34:16 +000056
mbligh432bad42007-10-09 19:56:07 +000057 def commit(self):
58 self.con.commit()
59
60
mbligh31d29c42007-09-27 00:51:33 +000061 def select(self, fields, table, where, distinct = False):
mbligh608c3252007-08-31 13:53:00 +000062 """\
63 select fields from table where {dictionary}
64 """
mbligh31d29c42007-09-27 00:51:33 +000065 cmd = ['select']
66 if distinct:
67 cmd.append('distinct')
68 cmd += [fields, 'from', table]
mbligh608c3252007-08-31 13:53:00 +000069
mbligh31d29c42007-09-27 00:51:33 +000070 values = []
mbligh414c69e2007-10-05 15:13:06 +000071 if where and isinstance(where, types.DictionaryType):
mbligh53d14252007-09-12 16:33:14 +000072 keys = [field + '=%s' for field in where.keys()]
73 values = [where[field] for field in where.keys()]
74
mbligh31d29c42007-09-27 00:51:33 +000075 cmd.append(' where ' + ' and '.join(keys))
mbligh414c69e2007-10-05 15:13:06 +000076 elif where and isinstance(where, types.StringTypes):
77 cmd.append(' where ' + where)
mbligh53d14252007-09-12 16:33:14 +000078
mbligh31d29c42007-09-27 00:51:33 +000079 self.dprint('%s %s' % (' '.join(cmd),values))
80 self.cur.execute(' '.join(cmd), values)
mblighd5c33db2006-10-08 21:34:16 +000081 return self.cur.fetchall()
82
mbligh056d0d32006-10-08 22:31:10 +000083
mbligh414c69e2007-10-05 15:13:06 +000084 def select_sql(self, fields, table, sql, values):
85 """\
86 select fields from table "sql"
87 """
88 cmd = 'select %s from %s %s' % (fields, table, sql)
89 self.dprint(cmd)
90 self.cur.execute(cmd, values)
91 return self.cur.fetchall()
92
93
mbligh432bad42007-10-09 19:56:07 +000094 def insert(self, table, data, commit = None):
mbligh608c3252007-08-31 13:53:00 +000095 """\
96 'insert into table (keys) values (%s ... %s)', values
97
98 data:
99 dictionary of fields and data
100 """
mbligh432bad42007-10-09 19:56:07 +0000101 if commit == None:
102 commit = self.autocommit
mbligh608c3252007-08-31 13:53:00 +0000103 fields = data.keys()
104 refs = ['%s' for field in fields]
105 values = [data[field] for field in fields]
106 cmd = 'insert into %s (%s) values (%s)' % \
107 (table, ','.join(fields), ','.join(refs))
mbligh53d14252007-09-12 16:33:14 +0000108
mbligh8e1ab172007-09-13 17:29:56 +0000109 self.dprint('%s %s' % (cmd,values))
mbligh608c3252007-08-31 13:53:00 +0000110 self.cur.execute(cmd, values)
mbligh432bad42007-10-09 19:56:07 +0000111 if commit:
112 self.con.commit()
mbligh608c3252007-08-31 13:53:00 +0000113
114
mbligh432bad42007-10-09 19:56:07 +0000115 def update(self, table, data, where, commit = None):
mbligh414c69e2007-10-05 15:13:06 +0000116 """\
117 'update table set data values (%s ... %s) where ...'
118
119 data:
120 dictionary of fields and data
121 """
mbligh432bad42007-10-09 19:56:07 +0000122 if commit == None:
123 commit = self.autocommit
mbligh414c69e2007-10-05 15:13:06 +0000124 cmd = 'update %s ' % table
125 fields = data.keys()
126 data_refs = [field + '=%s' for field in fields]
127 data_values = [data[field] for field in fields]
128 cmd += ' set ' + ' and '.join(data_refs)
129
130 where_keys = [field + '=%s' for field in where.keys()]
131 where_values = [where[field] for field in where.keys()]
132 cmd += ' where ' + ' and '.join(where_keys)
133
134 print '%s %s' % (cmd, data_values + where_values)
135 self.cur.execute(cmd, data_values + where_values)
mbligh432bad42007-10-09 19:56:07 +0000136 if commit:
137 self.con.commit()
mbligh414c69e2007-10-05 15:13:06 +0000138
139
mbligh432bad42007-10-09 19:56:07 +0000140 def insert_job(self, tag, job, commit = None):
mbligh2aaeb672007-10-01 14:54:18 +0000141 job.machine_idx = self.lookup_machine(job.machine)
142 if not job.machine_idx:
mbligh432bad42007-10-09 19:56:07 +0000143 job.machine_idx = self.insert_machine(job.machine,
144 commit=commit)
145 self.insert('jobs',
146 {'tag':tag,
147 'machine_idx':job.machine_idx},
148 commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000149 job.index = self.find_job(tag)
150 for test in job.tests:
mbligh432bad42007-10-09 19:56:07 +0000151 self.insert_test(job, test, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000152
mbligh432bad42007-10-09 19:56:07 +0000153 def insert_test(self, job, test, commit = None):
154 kver = self.insert_kernel(test.kernel, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000155 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000156 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000157 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000158 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh432bad42007-10-09 19:56:07 +0000159 self.insert('tests', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000160 test_idx = self.find_test(job.index, test.subdir)
161 data = { 'test_idx':test_idx }
162
163 for i in test.iterations:
164 data['iteration'] = i.index
165 for key in i.keyval:
166 data['attribute'] = key
167 data['value'] = i.keyval[key]
mbligh432bad42007-10-09 19:56:07 +0000168 self.insert('iteration_result',
169 data,
170 commit=commit)
mblighe9cf9d42007-08-31 08:56:00 +0000171
172
mbligh048e1c92007-10-07 00:10:33 +0000173 def read_machine_map(self):
174 self.machine_group = {}
175 for line in open(self.machine_map, 'r').readlines():
176 (machine, group) = line.split()
177 self.machine_group[machine] = group
178
179
mbligh432bad42007-10-09 19:56:07 +0000180 def insert_machine(self, hostname, group = None, commit = None):
mbligh048e1c92007-10-07 00:10:33 +0000181 if self.machine_map and not self.machine_group:
182 self.read_machine_map()
183
184 if not group:
185 group = self.machine_group.get(hostname, hostname)
186
mbligh432bad42007-10-09 19:56:07 +0000187 self.insert('machines',
188 { 'hostname' : hostname ,
189 'machine_group' : group },
190 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000191 return self.lookup_machine(hostname)
192
193
194 def lookup_machine(self, hostname):
195 where = { 'hostname' : hostname }
196 rows = self.select('machine_idx', 'machines', where)
197 if rows:
198 return rows[0][0]
199 else:
200 return None
201
202
mbligh9bb92fe2007-09-12 15:54:23 +0000203 def lookup_kernel(self, kernel):
204 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000205 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000206 if rows:
207 return rows[0][0]
208 else:
209 return None
mblighe9cf9d42007-08-31 08:56:00 +0000210
211
mbligh432bad42007-10-09 19:56:07 +0000212 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000213 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000214 if kver:
215 return kver
mbligh432bad42007-10-09 19:56:07 +0000216 self.insert('kernels',
217 {'base':kernel.base,
218 'kernel_hash':kernel.kernel_hash,
219 'printable':kernel.base},
220 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000221 # WARNING - incorrectly shoving base into printable here.
222 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000223 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000224 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000225 return kver
226
227
mbligh432bad42007-10-09 19:56:07 +0000228 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000229 print patch.reference
230 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000231 self.insert('patches',
232 {'kernel_idx': kver,
233 'name':name,
234 'url':patch.reference,
235 'hash':patch.hash},
236 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000237
mbligh048e1c92007-10-07 00:10:33 +0000238
mbligh2bd48872007-09-20 18:32:25 +0000239 def find_test(self, job_idx, subdir):
240 where = { 'job_idx':job_idx , 'subdir':subdir }
241 rows = self.select('test_idx', 'tests', where)
242 if rows:
243 return rows[0][0]
244 else:
245 return None
246
mbligh056d0d32006-10-08 22:31:10 +0000247
248 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000249 rows = self.select('job_idx', 'jobs', {'tag': tag})
250 if rows:
251 return rows[0][0]
252 else:
253 return None