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 from buildfarm.history import GitBranch
39 return GitBranch(self.repo, self.branch)
42 return "<%s %r>" % (self.__class__.__name__, self.name)
45 def read_trees_from_conf(path):
46 """Read trees from a configuration file.
48 :param path: tree path
49 :return: Dictionary with trees
52 cfp = ConfigParser.ConfigParser()
53 cfp.readfp(open(path))
54 for s in cfp.sections():
55 ret[s] = Tree(name=s, **dict(cfp.items(s)))
59 def lcov_extract_percentage(text):
60 """Extract the coverage percentage from the lcov file."""
61 m = re.search('\<td class="headerItem".*?\>Code\ \;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', text)
68 class BuildFarm(object):
74 def __init__(self, path=None):
76 path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
78 self.webdir = os.path.join(self.path, "web")
79 if not os.path.isdir(path):
80 raise Exception("web directory %s does not exist" % self.webdir)
81 self.trees = read_trees_from_conf(os.path.join(self.webdir, "trees.conf"))
82 self.builds = self._open_build_results()
83 self.upload_builds = self._open_upload_build_results()
84 self.hostdb = self._open_hostdb()
85 self.compilers = self._load_compilers()
86 self.lcovdir = os.path.join(self.path, "lcov/data")
89 return "%s(%r)" % (self.__class__.__name__, self.path)
91 def _open_build_results(self):
92 from buildfarm import data
93 return data.BuildResultStore(os.path.join(self.path, "data", "oldrevs"))
95 def _open_upload_build_results(self):
96 from buildfarm import data
97 return data.UploadBuildResultStore(os.path.join(self.path, "data", "upload"))
99 def _open_hostdb(self):
100 from buildfarm import hostdb
101 return hostdb.HostDatabase(
102 os.path.join(self.path, "hostdb.sqlite"))
104 def _load_compilers(self):
105 from buildfarm import util
106 return util.load_list(os.path.join(self.webdir, "compilers.list"))
108 def lcov_status(self, tree):
109 """get status of build"""
110 from buildfarm import data, util
111 file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
113 lcov_html = util.FileLoad(file)
114 except (OSError, IOError):
115 # File does not exist
116 raise data.NoSuchBuildError(tree, self.LCOVHOST, "lcov")
118 perc = lcov_extract_percentage(lcov_html)
125 def get_build(self, tree, host, compiler, rev=None):
127 return self.builds.get_build(tree, host, compiler, rev)
129 return self.upload_builds.get_build(tree, host, compiler)
131 def get_new_builds(self):
132 from buildfarm import data
133 for host in self.hostdb.hosts():
134 for tree in self.trees:
135 for compiler in self.compilers:
136 # By building the log file name this way, using only the list of
137 # hosts, trees and compilers as input, we ensure we
140 yield self.upload_builds.get_build(host, tree, compiler)
141 except data.NoSuchBuildError:
145 class CachingBuildFarm(BuildFarm):
147 def __init__(self, path=None, readonly=False, cachedirname=None):
148 self._cachedirname = cachedirname
149 self.readonly = readonly
150 super(CachingBuildFarm, self).__init__(path)
152 def _get_cachedir(self):
153 if self._cachedirname is not None:
154 return os.path.join(self.path, self._cachedirname)
156 return os.path.join(self.path, "cache")
158 def _open_build_results(self):
159 from buildfarm import data
160 return data.CachingBuildResultStore(os.path.join(self.path, "data", "oldrevs"),
161 self._get_cachedir(), readonly=self.readonly)
163 def _open_upload_build_results(self):
164 from buildfarm import data
165 return data.CachingUploadBuildResultStore(os.path.join(self.path, "data", "upload"),
166 self._get_cachedir(), readonly=self.readonly)
168 def lcov_status(self, tree):
169 """get status of build"""
170 from buildfarm import data, util
171 cachefile = os.path.join(self._get_cachedir(),
172 "lcov.%s.%s.status" % (self.LCOVHOST, tree))
173 file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
177 # File does not exist
178 raise data.NoSuchBuildError(tree, self.LCOVHOST, "lcov")
180 st2 = os.stat(cachefile)
182 # file does not exist
185 if st2 and st1.st_ctime <= st2.st_mtime:
186 ret = util.FileLoad(cachefile)
191 perc = super(CachingBuildFarm, self).lcov_status(tree)
192 if not self.readonly:
193 util.FileSave(cachefile, perc)