2 # Simple database query script for the buildfarm
4 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2001-2005
5 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001
6 # Copyright (C) Vance Lankhaar <vance@samba.org> 2002-2005
7 # Copyright (C) Martin Pool <mbp@samba.org> 2001
8 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 from cStringIO import StringIO
32 class BuildSummary(object):
34 def __init__(self, host, tree, compiler, revision, status):
37 self.compiler = compiler
38 self.revision = revision
42 BuildStageResult = collections.namedtuple("BuildStageResult", "name result")
45 class MissingRevisionInfo(Exception):
46 """Revision info could not be found in the build log."""
48 def __init__(self, build=None):
52 class LogFileMissing(Exception):
53 """Log file missing."""
56 class BuildStatus(object):
58 def __init__(self, stages=None, other_failures=None):
59 if stages is not None:
60 self.stages = [BuildStageResult(n, r) for (n, r) in stages]
63 if other_failures is not None:
64 self.other_failures = other_failures
66 self.other_failures = set()
70 if self.other_failures:
72 return not all([x.result == 0 for x in self.stages])
74 def __serialize__(self):
78 def __deserialize__(cls, text):
82 if self.other_failures:
83 return ",".join(self.other_failures)
84 return "/".join([str(x.result) for x in self.stages])
86 def broken_host(self):
87 if "disk full" in self.other_failures:
91 def regressed_since(self, older):
92 """Check if this build has regressed since another build."""
93 if "disk full" in self.other_failures:
95 if ("timeout" in self.other_failures and
96 "timeout" in older.other_failures):
97 # When the timeout happens exactly can differ slightly, so it's
98 # okay if the numbers are a bit different..
100 if ("panic" in self.other_failures and
101 not "panic" in older.other_failures):
103 if len(self.stages) < len(older.stages):
104 # Less stages completed
106 for ((old_name, old_result), (new_name, new_result)) in zip(
107 older.stages, self.stages):
108 assert old_name == new_name
109 if new_result > old_result:
113 def __cmp__(self, other):
114 other_extra = other.other_failures - self.other_failures
115 self_extra = self.other_failures - other.other_failures
116 # Give more importance to other failures
122 la = len(self.stages)
123 lb = len(other.stages)
129 return cmp(other.stages, self.stages)
132 return "%s(%r, %r)" % (self.__class__.__name__, self.stages, self.other_failures)
135 def check_dir_exists(kind, path):
136 if not os.path.isdir(path):
137 raise Exception("%s directory %s does not exist" % (kind, path))
140 def build_status_from_logs(log, err):
141 """get status of build"""
142 # FIXME: Perhaps also extract revision here?
150 re_status = re.compile("^([A-Z_]+) STATUS:(\s*\d+)$")
151 re_action = re.compile("^ACTION (PASSED|FAILED):\s+test$")
154 if l.startswith("No space left on device"):
155 ret.other_failures.add("disk full")
157 if "maximum runtime exceeded" in l: # Ugh.
158 ret.other_failures.add("timeout")
160 if l.startswith("PANIC:") or l.startswith("INTERNAL ERROR:"):
161 ret.other_failures.add("panic")
163 if l.startswith("testsuite-failure: ") or l.startswith("testsuite-error: "):
166 if l.startswith("testsuite-success: "):
169 m = re_status.match(l)
171 stages.append(BuildStageResult(m.group(1), int(m.group(2).strip())))
172 if m.group(1) == "TEST":
175 m = re_action.match(l)
176 if m and not test_seen:
177 if m.group(1) == "PASSED":
178 stages.append(BuildStageResult("TEST", 0))
180 stages.append(BuildStageResult("TEST", 1))
183 # Scan err file for specific errors
185 if "No space left on device" in l:
186 ret.other_failures.add("disk full")
189 if sr.name != "TEST":
192 if test_successes + test_failures == 0:
193 # No granular test output
194 return BuildStageResult("TEST", sr.result)
195 if sr.result == 1 and test_failures == 0:
196 ret.other_failures.add("inconsistent test result")
197 return BuildStageResult("TEST", -1)
198 return BuildStageResult("TEST", test_failures)
200 ret.stages = map(map_stage, stages)
204 def revision_from_log(log):
207 if l.startswith("BUILD COMMIT REVISION: "):
208 revid = l.split(":", 1)[1].strip()
210 raise MissingRevisionInfo()
214 class NoSuchBuildError(Exception):
215 """The build with the specified name does not exist."""
217 def __init__(self, tree, host, compiler, rev=None):
220 self.compiler = compiler
225 """A single build of a tree on a particular host using a particular compiler.
228 def __init__(self, basename, tree, host, compiler, rev=None):
229 self.basename = basename
232 self.compiler = compiler
235 def __cmp__(self, other):
237 (self.upload_time, self.revision, self.host, self.tree, self.compiler),
238 (other.upload_time, other.revision, other.host, other.tree, other.compiler))
240 def __eq__(self, other):
241 return (isinstance(other, Build) and
242 self.log_checksum() == other.log_checksum())
245 if self.revision is not None:
246 return "<%s: revision %s of %s on %s using %s>" % (self.__class__.__name__, self.revision, self.tree, self.host, self.compiler)
248 return "<%s: %s on %s using %s>" % (self.__class__.__name__, self.tree, self.host, self.compiler)
250 def remove_logs(self):
251 # In general, basename.log should *always* exist.
252 if os.path.exists(self.basename+".log"):
253 os.unlink(self.basename + ".log")
254 if os.path.exists(self.basename+".err"):
255 os.unlink(self.basename+".err")
261 def upload_time(self):
262 """get timestamp of build"""
263 st = os.stat("%s.log" % self.basename)
268 """get the age of build"""
269 return time.time() - self.upload_time
272 """read full log file"""
274 return open(self.basename+".log", "r")
276 raise LogFileMissing()
279 """read full err file"""
281 return open(self.basename+".err", 'r')
286 def log_checksum(self):
289 return hashlib.sha1(f.read()).hexdigest()
294 revid = self.revision_details()
295 status = self.status()
296 return BuildSummary(self.host, self.tree, self.compiler, revid, status)
298 def revision_details(self):
299 """get the revision of build
305 return revision_from_log(f)
310 """get status of build
312 :return: tuple with build status
314 log = self.read_log()
316 err = self.read_err()
318 return build_status_from_logs(log, err)
325 """get status of build"""
326 file = self.read_err()
327 return len(file.readlines())
330 class UploadBuildResultStore(object):
332 def __init__(self, path):
333 """Open the database.
335 :param path: Build result base directory
339 def get_all_builds(self):
340 for name in os.listdir(self.path):
342 (build, tree, host, compiler, extension) = name.split(".")
345 if build != "build" or extension != "log":
347 yield self.get_build(tree, host, compiler)
349 def build_fname(self, tree, host, compiler):
350 return os.path.join(self.path, "build.%s.%s.%s" % (tree, host, compiler))
352 def has_host(self, host):
353 for name in os.listdir(self.path):
355 if name.split(".")[2] == host:
361 def get_build(self, tree, host, compiler):
362 basename = self.build_fname(tree, host, compiler)
363 logf = "%s.log" % basename
364 if not os.path.exists(logf):
365 raise NoSuchBuildError(tree, host, compiler)
366 return Build(basename, tree, host, compiler)
369 class BuildResultStore(object):
370 """The build farm build result database."""
372 def __init__(self, path):
373 """Open the database.
375 :param path: Build result base directory
379 def __contains__(self, build):
384 rev = build.revision_details()
385 self.get_build(build.tree, build.host, build.compiler, rev)
386 except NoSuchBuildError:
391 def get_build(self, tree, host, compiler, rev, checksum=None):
392 basename = self.build_fname(tree, host, compiler, rev)
393 logf = "%s.log" % basename
394 if not os.path.exists(logf):
395 raise NoSuchBuildError(tree, host, compiler, rev)
396 return Build(basename, tree, host, compiler, rev)
398 def build_fname(self, tree, host, compiler, rev):
399 """get the name of the build file"""
400 return os.path.join(self.path, "build.%s.%s.%s-%s" % (tree, host, compiler, rev))
402 def get_all_builds(self):
403 for l in os.listdir(self.path):
404 m = re.match("^build\.([0-9A-Za-z]+)\.([0-9A-Za-z]+)\.([0-9A-Za-z]+)-([0-9A-Fa-f]+).log$", l)
409 compiler = m.group(3)
411 stat = os.stat(os.path.join(self.path, l))
412 # skip the current build
413 if stat.st_nlink == 2:
415 yield self.get_build(tree, host, compiler, rev)
417 def get_old_builds(self, tree, host, compiler):
418 """get a list of old builds and their status."""
420 for build in self.get_all_builds():
421 if build.tree == tree and build.host == host and build.compiler == compiler:
423 ret.sort(lambda a, b: cmp(a.upload_time, b.upload_time))
426 def upload_build(self, build):
427 rev = build.revision_details()
429 new_basename = self.build_fname(build.tree, build.host, build.compiler, rev)
431 existing_build = self.get_build(build.tree, build.host, build.compiler, rev)
432 except NoSuchBuildError:
433 if os.path.exists(new_basename+".log"):
434 os.remove(new_basename+".log")
435 if os.path.exists(new_basename+".err"):
436 os.remove(new_basename+".err")
438 existing_build.remove_logs()
439 os.link(build.basename+".log", new_basename+".log")
440 if os.path.exists(build.basename+".err"):
441 os.link(build.basename+".err", new_basename+".err")
442 return Build(new_basename, build.tree, build.host, build.compiler, rev)
444 def get_previous_revision(self, tree, host, compiler, revision):
445 raise NoSuchBuildError(tree, host, compiler, revision)
447 def get_latest_revision(self, tree, host, compiler):
448 raise NoSuchBuildError(tree, host, compiler)