blob: a696c3a1ea05633f233dd287b173c60f42d1467c [file] [log] [blame]
mbligh97894452007-10-05 15:10:33 +00001import MySQLdb, re, os, sys, types
mblighd5c33db2006-10-08 21:34:16 +00002
3class db:
mbligh728ff0a2007-09-27 17:05:12 +00004 def __init__(self, debug = False):
mbligh8e1ab172007-09-13 17:29:56 +00005 self.debug = debug
mbligha218d112007-10-03 20:15:09 +00006
7 path = os.path.dirname(os.path.abspath(sys.argv[0]))
mblighb32cd432007-09-25 18:20:04 +00008 try:
mbligha218d112007-10-03 20:15:09 +00009 file = os.path.join(path, '.database')
10 db_prefs = open(path, 'r')
mbligh728ff0a2007-09-27 17:05:12 +000011 host = db_prefs.readline().rstrip()
12 database = db_prefs.readline().rstrip()
13 except:
14 host = 'localhost'
15 database = 'tko'
16
17 try:
mbligha218d112007-10-03 20:15:09 +000018 file = os.path.join(path, '.priv_login')
19 login = open(file, 'r')
mblighb32cd432007-09-25 18:20:04 +000020 user = login.readline().rstrip()
21 password = login.readline().rstrip()
mbligh95fca572007-09-27 17:11:00 +000022 except:
23 try:
mbligha218d112007-10-03 20:15:09 +000024 file = os.path.join(path, '.unpriv_login')
25 login = open(path, 'r')
mbligh95fca572007-09-27 17:11:00 +000026 user = login.readline().rstrip()
27 password = login.readline().rstrip()
28 except:
29 user = 'nobody'
30 password = ''
mblighb32cd432007-09-25 18:20:04 +000031
32 self.con = MySQLdb.connect(host=host, user=user,
mbligh95fca572007-09-27 17:11:00 +000033 passwd=password, db=database)
mblighd5c33db2006-10-08 21:34:16 +000034 self.cur = self.con.cursor()
35
mbligh8e1ab172007-09-13 17:29:56 +000036 # if not present, insert statuses
37 self.status_idx = {}
38 self.status_word = {}
mblighc82f2462007-10-02 19:19:17 +000039 status_rows = self.select('status_idx, word', 'status', None)
40 for s in status_rows:
mbligh97894452007-10-05 15:10:33 +000041 self.status_idx[s[1]] = s[0]
mblighc82f2462007-10-02 19:19:17 +000042 self.status_word[s[0]] = s[1]
mbligh8e1ab172007-09-13 17:29:56 +000043
44
mbligh8e1ab172007-09-13 17:29:56 +000045 def dprint(self, value):
46 if self.debug:
47 sys.stderr.write('SQL: ' + str(value) + '\n')
48
mblighd5c33db2006-10-08 21:34:16 +000049
mbligh31d29c42007-09-27 00:51:33 +000050 def select(self, fields, table, where, distinct = False):
mbligh608c3252007-08-31 13:53:00 +000051 """\
52 select fields from table where {dictionary}
53 """
mbligh31d29c42007-09-27 00:51:33 +000054 cmd = ['select']
55 if distinct:
56 cmd.append('distinct')
57 cmd += [fields, 'from', table]
mbligh608c3252007-08-31 13:53:00 +000058
mbligh31d29c42007-09-27 00:51:33 +000059 values = []
mbligh414c69e2007-10-05 15:13:06 +000060 if where and isinstance(where, types.DictionaryType):
mbligh53d14252007-09-12 16:33:14 +000061 keys = [field + '=%s' for field in where.keys()]
62 values = [where[field] for field in where.keys()]
63
mbligh31d29c42007-09-27 00:51:33 +000064 cmd.append(' where ' + ' and '.join(keys))
mbligh414c69e2007-10-05 15:13:06 +000065 elif where and isinstance(where, types.StringTypes):
66 cmd.append(' where ' + where)
mbligh53d14252007-09-12 16:33:14 +000067
mbligh31d29c42007-09-27 00:51:33 +000068 self.dprint('%s %s' % (' '.join(cmd),values))
69 self.cur.execute(' '.join(cmd), values)
mblighd5c33db2006-10-08 21:34:16 +000070 return self.cur.fetchall()
71
mbligh056d0d32006-10-08 22:31:10 +000072
mbligh414c69e2007-10-05 15:13:06 +000073 def select_sql(self, fields, table, sql, values):
74 """\
75 select fields from table "sql"
76 """
77 cmd = 'select %s from %s %s' % (fields, table, sql)
78 self.dprint(cmd)
79 self.cur.execute(cmd, values)
80 return self.cur.fetchall()
81
82
mbligh608c3252007-08-31 13:53:00 +000083 def insert(self, table, data):
84 """\
85 'insert into table (keys) values (%s ... %s)', values
86
87 data:
88 dictionary of fields and data
89 """
90 fields = data.keys()
91 refs = ['%s' for field in fields]
92 values = [data[field] for field in fields]
93 cmd = 'insert into %s (%s) values (%s)' % \
94 (table, ','.join(fields), ','.join(refs))
mbligh53d14252007-09-12 16:33:14 +000095
mbligh8e1ab172007-09-13 17:29:56 +000096 self.dprint('%s %s' % (cmd,values))
mbligh608c3252007-08-31 13:53:00 +000097 self.cur.execute(cmd, values)
98 self.con.commit()
99
100
mbligh414c69e2007-10-05 15:13:06 +0000101 def update(self, table, data, where):
102 """\
103 'update table set data values (%s ... %s) where ...'
104
105 data:
106 dictionary of fields and data
107 """
108 cmd = 'update %s ' % table
109 fields = data.keys()
110 data_refs = [field + '=%s' for field in fields]
111 data_values = [data[field] for field in fields]
112 cmd += ' set ' + ' and '.join(data_refs)
113
114 where_keys = [field + '=%s' for field in where.keys()]
115 where_values = [where[field] for field in where.keys()]
116 cmd += ' where ' + ' and '.join(where_keys)
117
118 print '%s %s' % (cmd, data_values + where_values)
119 self.cur.execute(cmd, data_values + where_values)
120 self.con.commit()
121
122
mbligh056d0d32006-10-08 22:31:10 +0000123 def insert_job(self, tag, job):
mbligh2aaeb672007-10-01 14:54:18 +0000124 job.machine_idx = self.lookup_machine(job.machine)
125 if not job.machine_idx:
126 job.machine_idx = self.insert_machine(job.machine)
127 self.insert('jobs', {'tag':tag, 'machine_idx':job.machine_idx})
mbligh608c3252007-08-31 13:53:00 +0000128 job.index = self.find_job(tag)
129 for test in job.tests:
130 self.insert_test(job, test)
131
mbligh237bed32007-09-05 13:05:57 +0000132
mbligh608c3252007-08-31 13:53:00 +0000133 def insert_test(self, job, test):
mbligh8e1ab172007-09-13 17:29:56 +0000134 kver = self.insert_kernel(test.kernel)
mbligh608c3252007-08-31 13:53:00 +0000135 data = {'job_idx':job.index, 'test':test.testname,
mbligh2bd48872007-09-20 18:32:25 +0000136 'subdir':test.subdir, 'kernel_idx':kver,
mbligh8e1ab172007-09-13 17:29:56 +0000137 'status':self.status_idx[test.status],
mbligh2aaeb672007-10-01 14:54:18 +0000138 'reason':test.reason, 'machine_idx':job.machine_idx }
mbligh608c3252007-08-31 13:53:00 +0000139 self.insert('tests', data)
mbligh2bd48872007-09-20 18:32:25 +0000140 test_idx = self.find_test(job.index, test.subdir)
141 data = { 'test_idx':test_idx }
142
143 for i in test.iterations:
144 data['iteration'] = i.index
145 for key in i.keyval:
146 data['attribute'] = key
147 data['value'] = i.keyval[key]
148 self.insert('iteration_result', data)
mblighe9cf9d42007-08-31 08:56:00 +0000149
150
mbligh2aaeb672007-10-01 14:54:18 +0000151 def insert_machine(self, hostname):
152 self.insert('machines', { 'hostname' : hostname })
153 return self.lookup_machine(hostname)
154
155
156 def lookup_machine(self, hostname):
157 where = { 'hostname' : hostname }
158 rows = self.select('machine_idx', 'machines', where)
159 if rows:
160 return rows[0][0]
161 else:
162 return None
163
164
mbligh9bb92fe2007-09-12 15:54:23 +0000165 def lookup_kernel(self, kernel):
166 rows = self.select('kernel_idx', 'kernels',
167 {'kernel_hash':kernel.kernel_hash})
mbligh237bed32007-09-05 13:05:57 +0000168 if rows:
169 return rows[0][0]
170 else:
171 return None
mblighe9cf9d42007-08-31 08:56:00 +0000172
173
mbligh8e1ab172007-09-13 17:29:56 +0000174 def insert_kernel(self, kernel):
mbligh9bb92fe2007-09-12 15:54:23 +0000175 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000176 if kver:
177 return kver
mbligh9bb92fe2007-09-12 15:54:23 +0000178 self.insert('kernels', {'base':kernel.base,
mbligh95fca572007-09-27 17:11:00 +0000179 'kernel_hash':kernel.kernel_hash,
180 'printable':kernel.base})
mbligh9bb92fe2007-09-12 15:54:23 +0000181 # WARNING - incorrectly shoving base into printable here.
182 kver = self.lookup_kernel(kernel)
mbligh237bed32007-09-05 13:05:57 +0000183 for patch in kernel.patches:
184 self.insert_patch(kver, patch)
185 return kver
186
187
188 def insert_patch(self, kver, patch):
189 print patch.reference
190 name = os.path.basename(patch.reference)[:80]
mbligh9bb92fe2007-09-12 15:54:23 +0000191 self.insert('patches', {'kernel_idx': kver,
mbligh237bed32007-09-05 13:05:57 +0000192 'name':name,
193 'url':patch.reference,
194 'hash':patch.hash})
mbligh056d0d32006-10-08 22:31:10 +0000195
mbligh2bd48872007-09-20 18:32:25 +0000196 def find_test(self, job_idx, subdir):
197 where = { 'job_idx':job_idx , 'subdir':subdir }
198 rows = self.select('test_idx', 'tests', where)
199 if rows:
200 return rows[0][0]
201 else:
202 return None
203
mbligh056d0d32006-10-08 22:31:10 +0000204
205 def find_job(self, tag):
mbligh608c3252007-08-31 13:53:00 +0000206 rows = self.select('job_idx', 'jobs', {'tag': tag})
207 if rows:
208 return rows[0][0]
209 else:
210 return None