blob: 22e6be9efe000edbbd887f814be2daa9610477ac [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'],
21 'user': ['user', 'job_idx', 'job_idx'],
22 '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])
58 return [klass(db, field_name_in_main_table, groupname) for groupname in groupnames]
59
60
61 def __init__(self, db, idx_name, name):
62 self.db = db
63 self.name = name[0]
64 self.idx_name = idx_name
65 self.idx_value = name[1]
66
mbligh2aaeb672007-10-01 14:54:18 +000067
mblighcff2d212007-10-07 00:11:10 +000068class group:
69 @classmethod
70 def select(klass, db):
71 """Return all possible machine groups"""
72 rows = db.select('distinct machine_group', 'machines',
73 'machine_group is not null')
74 groupnames = sorted([row[0] for row in rows])
75 return [klass(db, groupname) for groupname in groupnames]
mbligh2e4e5df2007-11-05 17:22:46 +000076
mblighcff2d212007-10-07 00:11:10 +000077 def __init__(self, db, name):
78 self.name = name
79 self.db = db
80
81
82 def machines(self):
83 return machine.select(self.db, { 'machine_group' : self.name })
mbligh2e4e5df2007-11-05 17:22:46 +000084
mblighcff2d212007-10-07 00:11:10 +000085
86 def tests(self, where = {}):
87 values = [self.name]
88 sql = 't inner join machines m on m.machine_idx=t.machine_idx where m.machine_group=%s'
89 for key in where.keys():
90 sql += ' and %s=%%s' % key
91 values.append(where[key])
92 return test.select_sql(self.db, sql, values)
93
mbligh2e4e5df2007-11-05 17:22:46 +000094
mbligh2aaeb672007-10-01 14:54:18 +000095class machine:
96 @classmethod
97 def select(klass, db, where = {}):
98 fields = ['machine_idx', 'hostname', 'machine_group', 'owner']
99 machines = []
100 for row in db.select(','.join(fields), 'machines', where):
101 machines.append(klass(db, *row))
102 return machines
103
104
105 def __init__(self, db, idx, hostname, group, owner):
106 self.db = db
107 self.idx = idx
108 self.hostname = hostname
109 self.group = group
110 self.owner = owner
mbligh2e4e5df2007-11-05 17:22:46 +0000111
mbligh250300e2007-09-18 00:50:57 +0000112
mbligh9bb92fe2007-09-12 15:54:23 +0000113class kernel:
mbligh8e1ab172007-09-13 17:29:56 +0000114 @classmethod
115 def select(klass, db, where = {}):
116 fields = ['kernel_idx', 'kernel_hash', 'base', 'printable']
117 kernels = []
118 for row in db.select(','.join(fields), 'kernels', where):
119 kernels.append(klass(db, *row))
120 return kernels
mbligh9bb92fe2007-09-12 15:54:23 +0000121
mbligh8e1ab172007-09-13 17:29:56 +0000122
123 def __init__(self, db, idx, hash, base, printable):
mbligh9bb92fe2007-09-12 15:54:23 +0000124 self.db = db
mbligh8e1ab172007-09-13 17:29:56 +0000125 self.idx = idx
mbligh9bb92fe2007-09-12 15:54:23 +0000126 self.hash = hash
mbligh8e1ab172007-09-13 17:29:56 +0000127 self.base = base
128 self.printable = printable
129 self.patches = [] # THIS SHOULD PULL IN PATCHES!
mbligh9bb92fe2007-09-12 15:54:23 +0000130
131
132class test:
mbligh8e1ab172007-09-13 17:29:56 +0000133 @classmethod
mbligh31d29c42007-09-27 00:51:33 +0000134 def select(klass, db, where = {}, distinct = False):
mbligh8e1ab172007-09-13 17:29:56 +0000135 fields = ['test_idx', 'job_idx', 'test', 'subdir',
mbligh2aaeb672007-10-01 14:54:18 +0000136 'kernel_idx', 'status', 'reason', 'machine_idx']
mbligh8e1ab172007-09-13 17:29:56 +0000137 tests = []
mbligh31d29c42007-09-27 00:51:33 +0000138 for row in db.select(','.join(fields), 'tests', where, distinct):
mbligh8e1ab172007-09-13 17:29:56 +0000139 tests.append(klass(db, *row))
140 return tests
141
142
mbligh414c69e2007-10-05 15:13:06 +0000143 @classmethod
144 def select_sql(klass, db, sql, values):
145 fields = ['test_idx', 'job_idx', 'test', 'subdir',
146 'kernel_idx', 'status', 'reason', 'machine_idx']
147 fields = ['t.'+field for field in fields]
148 rows = db.select_sql(','.join(fields), 'tests', sql, values)
149 return [klass(db, *row) for row in rows]
150
151
mbligh2aaeb672007-10-01 14:54:18 +0000152 def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx):
mbligh8e1ab172007-09-13 17:29:56 +0000153 self.idx = test_idx
mbligh250300e2007-09-18 00:50:57 +0000154 self.job = job(db, job_idx)
mblighde7335d2007-09-26 16:53:20 +0000155 self.testname = testname
mbligh8e1ab172007-09-13 17:29:56 +0000156 self.subdir = subdir
mbligh50a25252007-09-27 15:26:17 +0000157 self.kernel_idx = kernel_idx
158 self.__kernel = None
159 self.__iterations = None
mbligh2aaeb672007-10-01 14:54:18 +0000160 self.machine_idx = machine_idx
161 self.__machine = None
mbligh8e1ab172007-09-13 17:29:56 +0000162 self.status_num = status_num
163 self.status_word = db.status_word[status_num]
mbligh9bb92fe2007-09-12 15:54:23 +0000164 self.reason = reason
mbligh50a25252007-09-27 15:26:17 +0000165 self.db = db
mblighde7335d2007-09-26 16:53:20 +0000166 if self.subdir:
167 self.url = html_root + self.job.tag + '/' + self.subdir
168 else:
mbligh676510c2007-09-28 01:28:12 +0000169 self.url = None
mbligh16ae9262007-09-21 00:53:08 +0000170
mbligh50a25252007-09-27 15:26:17 +0000171
172
173 def iterations(self):
174 """
175 Caching function for iterations
176 """
177 if not self.__iterations:
178 self.__iterations = {}
179 # A dictionary - dict{key} = [value1, value2, ....]
180 where = {'test_idx' : self.idx}
181 for i in iteration.select(self.db, where):
182 if self.__iterations.has_key(i.key):
183 self.__iterations[i.key].append(i.value)
184 else:
185 self.__iterations[i.key] = [i.value]
186 return self.__iterations
187
188
189 def kernel(self):
190 """
191 Caching function for kernels
192 """
193 if not self.__kernel:
194 where = {'kernel_idx' : self.kernel_idx}
195 self.__kernel = kernel.select(self.db, where)[0]
196 return self.__kernel
197
mbligh250300e2007-09-18 00:50:57 +0000198
mbligh2aaeb672007-10-01 14:54:18 +0000199 def machine(self):
200 """
201 Caching function for kernels
202 """
203 if not self.__machine:
204 where = {'machine_idx' : self.machine_idx}
205 self.__machine = machine.select(self.db, where)[0]
206 return self.__machine
207
208
mbligh250300e2007-09-18 00:50:57 +0000209class job:
210 def __init__(self, db, job_idx):
211 where = {'job_idx' : job_idx}
mbligh2aaeb672007-10-01 14:54:18 +0000212 rows = db.select('tag, machine_idx', 'jobs', where)
mbligh250300e2007-09-18 00:50:57 +0000213 if not rows:
214 return None
mbligh2aaeb672007-10-01 14:54:18 +0000215 (self.tag, self.machine_idx) = rows[0]
mbligh250300e2007-09-18 00:50:57 +0000216
mbligh8e1ab172007-09-13 17:29:56 +0000217
mbligh16ae9262007-09-21 00:53:08 +0000218class iteration:
219 @classmethod
220 def select(klass, db, where):
221 fields = ['iteration', 'attribute', 'value']
222 iterations = []
223 rows = db.select(','.join(fields), 'iteration_result', where)
224 for row in rows:
225 iterations.append(klass(*row))
226 return iterations
227
228
229 def __init__(self, iteration, key, value):
230 self.iteration = iteration
231 self.key = key
232 self.value = value
233
mbligh8e1ab172007-09-13 17:29:56 +0000234# class patch:
235# def __init__(self):
236# self.spec = None