blob: f3fac1a405348ed2d1783038723ce5ee09fdef3e [file] [log] [blame]
mbligh9bb92fe2007-09-12 15:54:23 +00001#!/usr/bin/python
mbligh2aaeb672007-10-01 14:54:18 +00002import os, re, db, sys
mbligh9bb92fe2007-09-12 15:54:23 +00003
mbligh2aaeb672007-10-01 14:54:18 +00004tko = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
5root_url_file = os.path.join(tko, '.root_url')
6if os.path.exists(root_url_file):
7 html_root = open(root_url_file, 'r').readline().rstrip()
8else:
mblighc959f4f2007-10-25 14:47:17 +00009 html_root = '/results/'
mbligh2aaeb672007-10-01 14:54:18 +000010
mbligh2e4e5df2007-11-05 17:22:46 +000011def select(db, field, value=None, distinct=False):
12 """ returns the relevant index values where the field value matches the
13 input value to the function.
14 If there is no value passed, then it returns the index values and the
15 field values corresponsing to them. """
16
17 fields = { 'kernel': ['printable', 'kernel_idx', 'kernel_idx'],
18 'machine_group': ['machine_group', 'machine_idx', 'machine_idx'],
19 'hostname': ['hostname', 'machine_idx', 'machine_idx'],
20 'label': ['label', 'job_idx', 'job_idx'],
mbligh05067a32007-12-03 17:48:04 +000021 'user': ['username', 'job_idx', 'job_idx'],
mbligh2e4e5df2007-11-05 17:22:46 +000022 'test': ['test', 'test', 'test'],
23 'status': ['word', 'status_idx', 'status'],
24 'reason': ['reason', 'test_idx', 'test_idx'] }
25 table = { 'kernel': 'kernels',
26 'machine_group': 'machines',
27 'hostname': 'machines',
28 'label': 'jobs',
29 'user': 'jobs',
30 'test': 'tests',
31 'status': 'status',
32 'reason': 'tests' }
33
34 lookup_field, idx_field, field_name_in_main_table = fields[field]
35 tablename = table[field]
36 # select all the index values that match the given field name.
37 sql = ""
38 if distinct:
39 sql += " distinct "
40 if not value:
41 sql += " %s , %s " % (lookup_field, idx_field)
42 where = " %s is not null " % lookup_field
43 else:
44 sql += "%s " % idx_field
45 where = " %s = %s " % (lookup_field, value)
46
47 match = db.select(sql, tablename, where)
48 # returns the value and its field name
49 return match, field_name_in_main_table
50
51class anygroup:
52 @classmethod
53 def selectunique(klass, db, field):
54 """Return unique values for all possible groups within
55 the table."""
56 rows, field_name_in_main_table = select(db, field, value=None, distinct=True)
57 groupnames = sorted([row for row in rows])
mbligh2b672532007-11-05 19:24:51 +000058
59 # collapse duplicates where records have the same name but
60 # multiple index values
61 headers = {}
62 for field_name, idx_value in groupnames:
63 if headers.has_key(field_name):
64 headers[field_name].append(idx_value)
65 else:
66 headers[field_name] = [idx_value]
67 headers = headers.items()
68 headers.sort()
69 return [klass(db, field_name_in_main_table, groupname) for groupname in headers]
mbligh2e4e5df2007-11-05 17:22:46 +000070
71
72 def __init__(self, db, idx_name, name):
73 self.db = db
74 self.name = name[0]
75 self.idx_name = idx_name
76 self.idx_value = name[1]
77
mbligh2aaeb672007-10-01 14:54:18 +000078
mblighcff2d212007-10-07 00:11:10 +000079class group:
80 @classmethod
81 def select(klass, db):
82 """Return all possible machine groups"""
83 rows = db.select('distinct machine_group', 'machines',
84 'machine_group is not null')
85 groupnames = sorted([row[0] for row in rows])
86 return [klass(db, groupname) for groupname in groupnames]
mbligh2e4e5df2007-11-05 17:22:46 +000087
mblighcff2d212007-10-07 00:11:10 +000088 def __init__(self, db, name):
89 self.name = name
90 self.db = db
91
92
93 def machines(self):
94 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +000095
mblighcff2d212007-10-07 00:11:10 +000096
97 def tests(self, where = {}):
98 values = [self.name]
99 sql = 't inner join machines m on m.machine_idx=t.machine_idx where m.machine_group=%s'
100 for key in where.keys():
101 sql += ' and %s=%%s' % key
102 values.append(where[key])
103 return test.select_sql(self.db, sql, values)
104
mbligh2e4e5df2007-11-05 17:22:46 +0000105
mbligh2aaeb672007-10-01 14:54:18 +0000106class machine:
107 @classmethod
108 def select(klass, db, where = {}):
109 fields = ['machine_idx', 'hostname', 'machine_group', 'owner']
110 machines = []
111 for row in db.select(','.join(fields), 'machines', where):
112 machines.append(klass(db, *row))
113 return machines
114
115
116 def __init__(self, db, idx, hostname, group, owner):
117 self.db = db
118 self.idx = idx
119 self.hostname = hostname
120 self.group = group
mbligh8174ce32007-11-30 17:57:55 +0000121 if len(owner) > 3:
122 self.owner = owner.capitalize()
123 else:
124 # capitalize acroymns
125 self.owner = owner.upper()
mbligh2e4e5df2007-11-05 17:22:46 +0000126
mbligh250300e2007-09-18 00:50:57 +0000127
mbligh9bb92fe2007-09-12 15:54:23 +0000128class kernel:
mbligh8e1ab172007-09-13 17:29:56 +0000129 @classmethod
130 def select(klass, db, where = {}):
131 fields = ['kernel_idx', 'kernel_hash', 'base', 'printable']
132 kernels = []
133 for row in db.select(','.join(fields), 'kernels', where):
134 kernels.append(klass(db, *row))
135 return kernels
mbligh9bb92fe2007-09-12 15:54:23 +0000136
mbligh8e1ab172007-09-13 17:29:56 +0000137
138 def __init__(self, db, idx, hash, base, printable):
mbligh9bb92fe2007-09-12 15:54:23 +0000139 self.db = db
mbligh8e1ab172007-09-13 17:29:56 +0000140 self.idx = idx
mbligh9bb92fe2007-09-12 15:54:23 +0000141 self.hash = hash
mbligh8e1ab172007-09-13 17:29:56 +0000142 self.base = base
143 self.printable = printable
144 self.patches = [] # THIS SHOULD PULL IN PATCHES!
mbligh9bb92fe2007-09-12 15:54:23 +0000145
146
147class test:
mbligh8e1ab172007-09-13 17:29:56 +0000148 @classmethod
mbligh85952b42007-12-07 16:28:33 +0000149 def select(klass, db, where = {}, wherein = {}, distinct = False):
mbligh8e1ab172007-09-13 17:29:56 +0000150 fields = ['test_idx', 'job_idx', 'test', 'subdir',
mbligh2aaeb672007-10-01 14:54:18 +0000151 'kernel_idx', 'status', 'reason', 'machine_idx']
mbligh8e1ab172007-09-13 17:29:56 +0000152 tests = []
mbligh85952b42007-12-07 16:28:33 +0000153 for row in db.select(','.join(fields), 'tests', where, wherein,distinct):
mbligh8e1ab172007-09-13 17:29:56 +0000154 tests.append(klass(db, *row))
155 return tests
156
157
mbligh414c69e2007-10-05 15:13:06 +0000158 @classmethod
159 def select_sql(klass, db, sql, values):
160 fields = ['test_idx', 'job_idx', 'test', 'subdir',
161 'kernel_idx', 'status', 'reason', 'machine_idx']
162 fields = ['t.'+field for field in fields]
163 rows = db.select_sql(','.join(fields), 'tests', sql, values)
164 return [klass(db, *row) for row in rows]
165
166
mbligh2aaeb672007-10-01 14:54:18 +0000167 def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx):
mbligh8e1ab172007-09-13 17:29:56 +0000168 self.idx = test_idx
mbligh250300e2007-09-18 00:50:57 +0000169 self.job = job(db, job_idx)
mblighde7335d2007-09-26 16:53:20 +0000170 self.testname = testname
mbligh8e1ab172007-09-13 17:29:56 +0000171 self.subdir = subdir
mbligh50a25252007-09-27 15:26:17 +0000172 self.kernel_idx = kernel_idx
173 self.__kernel = None
174 self.__iterations = None
mbligh2aaeb672007-10-01 14:54:18 +0000175 self.machine_idx = machine_idx
176 self.__machine = None
mbligh8e1ab172007-09-13 17:29:56 +0000177 self.status_num = status_num
178 self.status_word = db.status_word[status_num]
mbligh9bb92fe2007-09-12 15:54:23 +0000179 self.reason = reason
mbligh50a25252007-09-27 15:26:17 +0000180 self.db = db
mblighde7335d2007-09-26 16:53:20 +0000181 if self.subdir:
182 self.url = html_root + self.job.tag + '/' + self.subdir
183 else:
mbligh676510c2007-09-28 01:28:12 +0000184 self.url = None
mbligh16ae9262007-09-21 00:53:08 +0000185
mbligh50a25252007-09-27 15:26:17 +0000186
187
188 def iterations(self):
189 """
190 Caching function for iterations
191 """
192 if not self.__iterations:
193 self.__iterations = {}
194 # A dictionary - dict{key} = [value1, value2, ....]
195 where = {'test_idx' : self.idx}
196 for i in iteration.select(self.db, where):
197 if self.__iterations.has_key(i.key):
198 self.__iterations[i.key].append(i.value)
199 else:
200 self.__iterations[i.key] = [i.value]
201 return self.__iterations
202
203
204 def kernel(self):
205 """
206 Caching function for kernels
207 """
208 if not self.__kernel:
209 where = {'kernel_idx' : self.kernel_idx}
210 self.__kernel = kernel.select(self.db, where)[0]
211 return self.__kernel
212
mbligh250300e2007-09-18 00:50:57 +0000213
mbligh2aaeb672007-10-01 14:54:18 +0000214 def machine(self):
215 """
216 Caching function for kernels
217 """
218 if not self.__machine:
219 where = {'machine_idx' : self.machine_idx}
220 self.__machine = machine.select(self.db, where)[0]
221 return self.__machine
222
223
mbligh250300e2007-09-18 00:50:57 +0000224class job:
225 def __init__(self, db, job_idx):
226 where = {'job_idx' : job_idx}
mbligh2aaeb672007-10-01 14:54:18 +0000227 rows = db.select('tag, machine_idx', 'jobs', where)
mbligh250300e2007-09-18 00:50:57 +0000228 if not rows:
229 return None
mbligh2aaeb672007-10-01 14:54:18 +0000230 (self.tag, self.machine_idx) = rows[0]
mbligh2b672532007-11-05 19:24:51 +0000231 self.job_idx = job_idx
mbligh250300e2007-09-18 00:50:57 +0000232
mbligh8e1ab172007-09-13 17:29:56 +0000233
mbligh16ae9262007-09-21 00:53:08 +0000234class iteration:
235 @classmethod
236 def select(klass, db, where):
237 fields = ['iteration', 'attribute', 'value']
238 iterations = []
239 rows = db.select(','.join(fields), 'iteration_result', where)
240 for row in rows:
241 iterations.append(klass(*row))
242 return iterations
243
244
245 def __init__(self, iteration, key, value):
246 self.iteration = iteration
247 self.key = key
248 self.value = value
249
mbligh8e1ab172007-09-13 17:29:56 +0000250# class patch:
251# def __init__(self):
252# self.spec = None