2 # Simple database query script for the buildfarm
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
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 2 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, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 from buildfarm.sqldb import distinct_builds, Cast, StormBuild, setup_schema, StormHostDatabase
21 from buildfarm.tree import Tree
22 from storm.database import create_database
23 from storm.expr import Desc
24 from storm.store import Store
30 def read_trees_from_conf(path):
31 """Read trees from a configuration file.
33 :param path: tree path
34 :return: Dictionary with trees
37 cfp = ConfigParser.ConfigParser()
39 for s in cfp.sections():
40 ret[s] = Tree(name=s, **dict(cfp.items(s)))
44 def lcov_extract_percentage(f):
45 """Extract the coverage percentage from the lcov file."""
46 m = re.search('\<td class="headerItem".*?\>Code\ \;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', f.read())
53 class BuildFarm(object):
59 def __init__(self, path=None, store=None, timeout=0.5):
60 self.timeout = timeout
63 path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
65 self.webdir = os.path.join(self.path, "web")
66 if not os.path.isdir(path):
67 raise Exception("web directory %s does not exist" % self.webdir)
68 self.trees = read_trees_from_conf(os.path.join(self.webdir, "trees.conf"))
69 self.builds = self._open_build_results()
70 self.upload_builds = self._open_upload_build_results()
71 self.hostdb = self._open_hostdb()
72 self.compilers = self._load_compilers()
73 self.lcovdir = os.path.join(self.path, "lcov/data")
76 return "%s(%r)" % (self.__class__.__name__, self.path)
78 def _open_build_results(self):
79 path = os.path.join(self.path, "data", "oldrevs")
80 from buildfarm.build import BuildResultStore
81 return BuildResultStore(path, self._get_store())
83 def _open_upload_build_results(self):
84 from buildfarm.build import UploadBuildResultStore
85 path = os.path.join(self.path, "data", "upload")
86 return UploadBuildResultStore(path)
88 def _open_hostdb(self):
89 return StormHostDatabase(self._get_store())
91 def _load_compilers(self):
92 from buildfarm import util
93 return set(util.load_list(os.path.join(self.webdir, "compilers.list")))
96 if self.store is not None:
99 def lcov_status(self, tree):
100 """get status of build"""
101 from buildfarm.build import NoSuchBuildError
102 file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
104 lcov_html = open(file, 'r')
105 except (OSError, IOError):
106 # File does not exist
107 raise NoSuchBuildError(tree, self.LCOVHOST, "lcov")
109 return lcov_extract_percentage(lcov_html)
113 def get_build(self, tree, host, compiler, rev=None, checksum=None):
115 return self.builds.get_build(tree, host, compiler, rev,
118 return self.upload_builds.get_build(tree, host, compiler)
120 def get_new_builds(self):
121 hostnames = set([host.name for host in self.hostdb.hosts()])
122 for build in self.upload_builds.get_all_builds():
123 if (build.tree in self.trees and
124 build.compiler in self.compilers and
125 build.host in hostnames):
128 def get_last_builds(self):
129 result = self._get_store().find(StormBuild)
130 return distinct_builds(result.order_by(Desc(StormBuild.upload_time)))
132 def get_tree_builds(self, tree):
133 result = self._get_store().find(StormBuild,
134 Cast(StormBuild.tree, "TEXT") == Cast(tree, "TEXT"))
135 return distinct_builds(result.order_by(Desc(StormBuild.upload_time)))
137 def host_last_build(self, host):
138 return max([build.upload_time for build in self.get_host_builds(host)])
140 def get_host_builds(self, host):
141 result = self._get_store().find(StormBuild, StormBuild.host == host)
142 return distinct_builds(result.order_by(Desc(StormBuild.upload_time)))
144 def _get_store(self):
145 if self.store is not None:
147 db_path = os.path.join(self.path, "db", "hostdb.sqlite")
148 db = create_database("sqlite:%s?timeout=%f" % (db_path, self.timeout))
149 self.store = Store(db)
150 setup_schema(self.store)
153 def get_revision_builds(self, tree, revision=None):
154 return self._get_store().find(StormBuild,
155 Cast(StormBuild.tree, "TEXT") == Cast(tree, "TEXT"),
156 Cast(StormBuild.revision, "TEXT") == Cast(revision, "TEXT"))