blob: f83e78984b0bec5805b3810fd9b19204d911b083 [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)
mblighb10a60f2007-10-17 00:03:32 +0000145 self.insert('jobs', {'tag':tag,
146 'label': job.label,
147 'user': job.user,
148 'machine_idx':job.machine_idx},
149 commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000150 job.index = self.find_job(tag)
151 for test in job.tests:
mbligh432bad42007-10-09 19:56:07 +0000152 self.insert_test(job, test, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000153
mbligh432bad42007-10-09 19:56:07 +0000154 def insert_test(self, job, test, commit = None):
155 kver = self.insert_kernel(test.kernel, commit=commit)
mbligh608c3252007-08-31 13:53:00 +0000156 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000157 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000158 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000159 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh432bad42007-10-09 19:56:07 +0000160 self.insert('tests', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000161 test_idx = self.find_test(job.index, test.subdir)
162 data = { 'test_idx':test_idx }
163
164 for i in test.iterations:
165 data['iteration'] = i.index
166 for key in i.keyval:
167 data['attribute'] = key
168 data['value'] = i.keyval[key]
mbligh432bad42007-10-09 19:56:07 +0000169 self.insert('iteration_result',
170 data,
171 commit=commit)
mblighe9cf9d42007-08-31 08:56:00 +0000172
173
mbligh048e1c92007-10-07 00:10:33 +0000174 def read_machine_map(self):
175 self.machine_group = {}
176 for line in open(self.machine_map, 'r').readlines():
177 (machine, group) = line.split()
178 self.machine_group[machine] = group
179
180
mbligh432bad42007-10-09 19:56:07 +0000181 def insert_machine(self, hostname, group = None, commit = None):
mbligh048e1c92007-10-07 00:10:33 +0000182 if self.machine_map and not self.machine_group:
183 self.read_machine_map()
184
185 if not group:
186 group = self.machine_group.get(hostname, hostname)
187
mbligh432bad42007-10-09 19:56:07 +0000188 self.insert('machines',
189 { 'hostname' : hostname ,
190 'machine_group' : group },
191 commit=commit)
mbligh2aaeb672007-10-01 14:54:18 +0000192 return self.lookup_machine(hostname)
193
194
195 def lookup_machine(self, hostname):
196 where = { 'hostname' : hostname }
197 rows = self.select('machine_idx', 'machines', where)
198 if rows:
199 return rows[0][0]
200 else:
201 return None
202
203
mbligh9bb92fe2007-09-12 15:54:23 +0000204 def lookup_kernel(self, kernel):
205 rows = self.select('kernel_idx', 'kernels',
mbligh048e1c92007-10-07 00:10:33 +0000206 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000207 if rows:
208 return rows[0][0]
209 else:
210 return None
mblighe9cf9d42007-08-31 08:56:00 +0000211
212
mbligh432bad42007-10-09 19:56:07 +0000213 def insert_kernel(self, kernel, commit = None):
mbligh9bb92fe2007-09-12 15:54:23 +0000214 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000215 if kver:
216 return kver
mbligh432bad42007-10-09 19:56:07 +0000217 self.insert('kernels',
218 {'base':kernel.base,
219 'kernel_hash':kernel.kernel_hash,
220 'printable':kernel.base},
221 commit=commit)
mbligh9bb92fe2007-09-12 15:54:23 +0000222 # WARNING - incorrectly shoving base into printable here.
223 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000224 for patch in kernel.patches:
mbligh432bad42007-10-09 19:56:07 +0000225 self.insert_patch(kver, patch, commit=commit)
mbligh237bed32007-09-05 13:05:57 +0000226 return kver
227
228
mbligh432bad42007-10-09 19:56:07 +0000229 def insert_patch(self, kver, patch, commit = None):
mbligh237bed32007-09-05 13:05:57 +0000230 print patch.reference
231 name = os.path.basename(patch.reference)[:80]
mbligh432bad42007-10-09 19:56:07 +0000232 self.insert('patches',
233 {'kernel_idx': kver,
234 'name':name,
235 'url':patch.reference,
236 'hash':patch.hash},
237 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000238
mbligh048e1c92007-10-07 00:10:33 +0000239
mbligh2bd48872007-09-20 18:32:25 +0000240 def find_test(self, job_idx, subdir):
241 where = { 'job_idx':job_idx , 'subdir':subdir }
242 rows = self.select('test_idx', 'tests', where)
243 if rows:
244 return rows[0][0]
245 else:
246 return None
247
mbligh056d0d32006-10-08 22:31:10 +0000248
249 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000250 rows = self.select('job_idx', 'jobs', {'tag': tag})
251 if rows:
252 return rows[0][0]
253 else:
254 return None