4 # Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from buildfarm.tree import (
23 from buildfarm.build import (
28 from buildfarm.hostdb import (
37 from pysqlite2 import dbapi2 as sqlite3
40 from storm.database import create_database
41 from storm.expr import EXPR, FuncExpr, compile
42 from storm.locals import Bool, Desc, Int, RawStr, Reference, Unicode
43 from storm.store import Store
47 __slots__ = ("column", "type")
50 def __init__(self, column, type):
55 def compile_count(compile, cast, state):
56 state.push("context", EXPR)
57 column = compile(cast.column, state)
59 return "CAST(%s AS %s)" % (column, cast.type)
62 class StormHost(Host):
63 __storm_table__ = "host"
65 id = Int(primary=True)
67 owner_name = Unicode(name="owner")
68 owner_email = Unicode()
73 permission = Unicode()
74 last_dead_mail = Int()
77 def _set_owner(self, value):
79 self.owner_name = None
80 self.owner_email = None
82 (self.owner_name, self.owner_email) = value
85 if self.owner_name is None:
88 return (self.owner_name, self.owner_email)
90 owner = property(_get_owner, _set_owner)
93 class StormHostDatabase(HostDatabase):
95 def __init__(self, store=None):
97 self.store = memory_store()
101 def createhost(self, name, platform=None, owner=None, owner_email=None,
102 password=None, permission=None):
103 """See `HostDatabase.createhost`."""
104 newhost = StormHost(name, owner=owner, owner_email=owner_email,
105 password=password, permission=permission, platform=platform)
107 self.store.add(newhost)
109 except sqlite3.IntegrityError:
110 raise HostAlreadyExists(name)
113 def deletehost(self, name):
115 self.store.remove(self[name])
118 """Retrieve an iterable over all hosts."""
119 return self.store.find(StormHost).order_by(StormHost.name)
121 def __getitem__(self, name):
122 result = self.store.find(StormHost,
123 Cast(StormHost.name, "TEXT") == Cast(name, "TEXT"))
126 raise NoSuchHost(name)
133 def distinct_builds(builds):
136 key = (build.tree, build.compiler, build.host)
143 class StormTree(Tree):
144 __storm_table__ = "tree"
146 id = Int(primary=True)
155 class StormTest(Test):
156 __storm_table__ = "test"
158 id = Int(primary=True)
162 class StormTestResult(TestResult):
163 __storm_table__ = "test_result"
165 id = Int(primary=True)
166 build_id = Int(name="build")
167 build = Reference(build_id, StormBuild)
169 test_id = Int(name="test")
170 test = Reference(test_id, StormTest)
173 def setup_schema(db):
174 db.execute("PRAGMA foreign_keys = 1;", noresult=True)
176 CREATE TABLE IF NOT EXISTS host (
177 id integer primary key autoincrement,
188 );""", noresult=True)
189 db.execute("CREATE UNIQUE INDEX IF NOT EXISTS unique_hostname ON host (name);", noresult=True)
191 CREATE TABLE IF NOT EXISTS build (
192 id integer primary key autoincrement,
198 compiler blob not null,
204 FOREIGN KEY (host_id) REFERENCES host (id),
205 FOREIGN KEY (tree_id) REFERENCES tree (id),
206 FOREIGN KEY (compiler_id) REFERENCES compiler (id)
207 );""", noresult=True)
208 db.execute("CREATE UNIQUE INDEX IF NOT EXISTS unique_checksum ON build (checksum);", noresult=True)
210 CREATE TABLE IF NOT EXISTS tree (
211 id integer primary key autoincrement,
220 CREATE UNIQUE INDEX IF NOT EXISTS unique_tree_name ON tree(name);
223 CREATE TABLE IF NOT EXISTS compiler (
224 id integer primary key autoincrement,
229 CREATE UNIQUE INDEX IF NOT EXISTS unique_compiler_name ON compiler(name);
232 CREATE TABLE IF NOT EXISTS test (
233 id integer primary key autoincrement,
236 db.execute("CREATE UNIQUE INDEX IF NOT EXISTS test_name ON test(name);",
238 db.execute("""CREATE TABLE IF NOT EXISTS test_result (
242 );""", noresult=True)
243 db.execute("""CREATE UNIQUE INDEX IF NOT EXISTS build_test_result ON test_result(build, test);""", noresult=True)
247 db = create_database("sqlite:")