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):
208 if l.startswith("BUILD COMMIT REVISION: "):
209 revid = l.split(":", 1)[1].strip()
210 elif l.startswith("BUILD COMMIT TIME"):
211 timestamp = l.split(":", 1)[1].strip()
213 raise MissingRevisionInfo()
214 return (revid, timestamp)
217 class NoSuchBuildError(Exception):
218 """The build with the specified name does not exist."""
220 def __init__(self, tree, host, compiler, rev=None):
223 self.compiler = compiler
228 """A single build of a tree on a particular host using a particular compiler.
231 def __init__(self, basename, tree, host, compiler, rev=None):
232 self.basename = basename
235 self.compiler = compiler
238 def __cmp__(self, other):
240 (self.upload_time, self.revision, self.host, self.tree, self.compiler),
241 (other.upload_time, other.revision, other.host, other.tree, other.compiler))
243 def __eq__(self, other):
244 return (isinstance(other, Build) and
245 self.log_checksum() == other.log_checksum())
248 if self.revision is not None:
249 return "<%s: revision %s of %s on %s using %s>" % (self.__class__.__name__, self.revision, self.tree, self.host, self.compiler)
251 return "<%s: %s on %s using %s>" % (self.__class__.__name__, self.tree, self.host, self.compiler)
253 def remove_logs(self):
254 # In general, basename.log should *always* exist.
255 if os.path.exists(self.basename+".log"):
256 os.unlink(self.basename + ".log")
257 if os.path.exists(self.basename+".err"):
258 os.unlink(self.basename+".err")
264 def upload_time(self):
265 """get timestamp of build"""
266 st = os.stat("%s.log" % self.basename)
271 """get the age of build"""
272 return time.time() - self.upload_time
275 """read full log file"""
277 return open(self.basename+".log", "r")
279 raise LogFileMissing()
282 """read full err file"""
284 return open(self.basename+".err", 'r')
289 def log_checksum(self):
292 return hashlib.sha1(f.read()).hexdigest()
297 (revid, timestamp) = self.revision_details()
298 status = self.status()
299 return BuildSummary(self.host, self.tree, self.compiler, revid, status)
301 def revision_details(self):
302 """get the revision of build
304 :return: Tuple with revision id and timestamp (if available)
308 return revision_from_log(f)
313 """get status of build
315 :return: tuple with build status
317 log = self.read_log()
319 err = self.read_err()
321 return build_status_from_logs(log, err)
328 """get status of build"""
329 file = self.read_err()
330 return len(file.readlines())
333 class UploadBuildResultStore(object):
335 def __init__(self, path):
336 """Open the database.
338 :param path: Build result base directory
342 def get_new_builds(self):
343 for name in os.listdir(self.path):
345 (build, tree, host, compiler, extension) = name.split(".")
348 if build != "build" or extension != "log":
350 yield self.get_build(tree, host, compiler)
352 def build_fname(self, tree, host, compiler):
353 return os.path.join(self.path, "build.%s.%s.%s" % (tree, host, compiler))
355 def has_host(self, host):
356 for name in os.listdir(self.path):
358 if name.split(".")[2] == host:
364 def get_build(self, tree, host, compiler):
365 basename = self.build_fname(tree, host, compiler)
366 logf = "%s.log" % basename
367 if not os.path.exists(logf):
368 raise NoSuchBuildError(tree, host, compiler)
369 return Build(basename, tree, host, compiler)
372 class BuildResultStore(object):
373 """The build farm build result database."""
375 def __init__(self, path):
376 """Open the database.
378 :param path: Build result base directory
382 def __contains__(self, build):
387 rev, timestamp = build.revision_details()
388 self.get_build(build.tree, build.host, build.compiler, rev)
389 except NoSuchBuildError:
394 def get_build(self, tree, host, compiler, rev, checksum=None):
395 basename = self.build_fname(tree, host, compiler, rev)
396 logf = "%s.log" % basename
397 if not os.path.exists(logf):
398 raise NoSuchBuildError(tree, host, compiler, rev)
399 return Build(basename, tree, host, compiler, rev)
401 def build_fname(self, tree, host, compiler, rev):
402 """get the name of the build file"""
403 return os.path.join(self.path, "build.%s.%s.%s-%s" % (tree, host, compiler, rev))
405 def get_all_builds(self):
406 for l in os.listdir(self.path):
407 m = re.match("^build\.([0-9A-Za-z]+)\.([0-9A-Za-z]+)\.([0-9A-Za-z]+)-([0-9A-Fa-f]+).log$", l)
412 compiler = m.group(3)
414 stat = os.stat(os.path.join(self.path, l))
415 # skip the current build
416 if stat.st_nlink == 2:
418 yield self.get_build(tree, host, compiler, rev)
420 def get_old_builds(self, tree, host, compiler):
421 """get a list of old builds and their status."""
423 for build in self.get_all_builds():
424 if build.tree == tree and build.host == host and build.compiler == compiler:
426 ret.sort(lambda a, b: cmp(a.upload_time, b.upload_time))
429 def upload_build(self, build):
430 (rev, rev_timestamp) = build.revision_details()
432 new_basename = self.build_fname(build.tree, build.host, build.compiler, rev)
434 existing_build = self.get_build(build.tree, build.host, build.compiler, rev)
435 except NoSuchBuildError:
436 if os.path.exists(new_basename+".log"):
437 os.remove(new_basename+".log")
438 if os.path.exists(new_basename+".err"):
439 os.remove(new_basename+".err")
441 existing_build.remove_logs()
442 os.link(build.basename+".log", new_basename+".log")
443 if os.path.exists(build.basename+".err"):
444 os.link(build.basename+".err", new_basename+".err")
445 return Build(new_basename, build.tree, build.host, build.compiler, rev)
447 def get_previous_revision(self, tree, host, compiler, revision):
448 raise NoSuchBuildError(tree, host, compiler, revision)
450 def get_latest_revision(self, tree, host, compiler):
451 raise NoSuchBuildError(tree, host, compiler)