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.
26 """A tree to build."""
28 def __init__(self, name, scm, repo, branch, subdir="", srcdir=""):
38 return "<%s %r>" % (self.__class__.__name__, self.name)
41 def read_trees_from_conf(path):
42 """Read trees from a configuration file."""
44 cfp = ConfigParser.ConfigParser()
45 cfp.readfp(open(path))
46 for s in cfp.sections():
47 ret[s] = Tree(name=s, **dict(cfp.items(s)))
51 def lcov_extract_percentage(text):
52 m = re.search('\<td class="headerItem".*?\>Code\ \;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', text)
59 class BuildFarm(object):
65 def __init__(self, path=None):
67 path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
69 self.cachedir = os.path.join(self.path, "cache")
70 self.webdir = os.path.join(self.path, "web")
71 if not os.path.isdir(path):
72 raise Exception("web directory %s does not exist" % self.webdir)
73 self.trees = read_trees_from_conf(os.path.join(self.webdir, "trees.conf"))
74 self.builds = self._open_build_results()
75 self.upload_builds = self._open_upload_build_results()
76 self.hostdb = self._open_hostdb()
77 self.compilers = self._load_compilers()
78 self.lcovdir = os.path.join(self.path, "lcov/data")
81 return "%s(%r)" % (self.__class__.__name__, self.path)
83 def _open_build_results(self):
84 from buildfarm import data
85 return data.BuildResultStore(os.path.join(self.path, "data", "oldrevs"))
87 def _open_upload_build_results(self):
88 from buildfarm import data
89 return data.UploadBuildResultStore(os.path.join(self.path, "data", "upload"))
91 def _open_hostdb(self):
92 from buildfarm import hostdb
93 return hostdb.HostDatabase(
94 os.path.join(self.path, "hostdb.sqlite"))
96 def _load_compilers(self):
97 from buildfarm import util
98 return util.load_list(os.path.join(self.webdir, "compilers.list"))
100 def lcov_status(self, tree):
101 """get status of build"""
102 from buildfarm import data, util
103 file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
105 lcov_html = util.FileLoad(file)
107 # File does not exist
108 raise data.NoSuchBuildError(tree, self.LCOVHOST, "lcov")
110 perc = lcov_extract_percentage(lcov_html)
117 def get_build(self, tree, host, compiler, rev=None):
119 return self.builds.get_build(tree, host, compiler, rev)
121 return self.upload_builds.get_build(tree, host, compiler)
123 def get_new_builds(self):
124 from buildfarm import data
125 for host in self.hostdb.hosts():
126 for tree in self.trees:
127 for compiler in self.compilers:
128 # By building the log file name this way, using only the list of
129 # hosts, trees and compilers as input, we ensure we
132 yield self.upload_builds.get_build(host, tree, compiler)
133 except data.NoSuchBuildError:
137 class CachingBuildFarm(BuildFarm):
139 def __init__(self, path=None, cachedirname=None):
141 super(CachingBuildFarm, self).__init__(path)
144 self.cachedir = os.path.join(self.path, cachedirname)
146 self.cachedir = os.path.join(self.path, "cache2")
147 self.builds = self._open_build_results()
148 self.upload_builds = self._open_upload_build_results()
150 def _open_build_results(self):
151 from buildfarm import data
152 if not self.cachedir:
154 return data.CachingBuildResultStore(os.path.join(self.path, "data", "oldrevs"),
157 def _open_upload_build_results(self):
158 from buildfarm import data
159 if not self.cachedir:
161 return data.CachingUploadBuildResultStore(os.path.join(self.path, "data", "upload"),
164 def lcov_status(self, tree):
165 """get status of build"""
166 from buildfarm import data, util
167 cachefile = os.path.join(self.cachedir,
168 "lcov.%s.%s.status" % (self.LCOVHOST, tree))
169 file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
173 # File does not exist
174 raise data.NoSuchBuildError(tree, self.LCOVHOST, "lcov")
176 st2 = os.stat(cachefile)
178 # file does not exist
181 if st2 and st1.st_ctime <= st2.st_mtime:
182 ret = util.FileLoad(cachefile)
187 lcov_html = util.FileLoad(file)
188 perc = lcov_extract_percentage(lcov_html)
193 if not self.readonly:
194 util.FileSave(cachefile, ret)