Merge StormCachingBuildFarm and BuildFarm.
[amitay/build-farm.git] / buildfarm / __init__.py
1 #!/usr/bin/python
2 # Simple database query script for the buildfarm
3 #
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org>         2010
5 #
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.
10 #
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.
15 #
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.
19
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
25
26 import ConfigParser
27 import os
28 import re
29
30 def read_trees_from_conf(path):
31     """Read trees from a configuration file.
32
33     :param path: tree path
34     :return: Dictionary with trees
35     """
36     ret = {}
37     cfp = ConfigParser.ConfigParser()
38     cfp.read(path)
39     for s in cfp.sections():
40         ret[s] = Tree(name=s, **dict(cfp.items(s)))
41     return ret
42
43
44 def lcov_extract_percentage(f):
45     """Extract the coverage percentage from the lcov file."""
46     m = re.search('\<td class="headerItem".*?\>Code\&nbsp\;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', f.read())
47     if m:
48         return m.group(1)
49     else:
50         return None
51
52
53 class BuildFarm(object):
54
55     LCOVHOST = "magni"
56     OLDAGE = 60*60*4,
57     DEADAGE = 60*60*24*4
58
59     def __init__(self, path=None, store=None, timeout=0.5):
60         self.timeout = timeout
61         self.store = store
62         if path is None:
63             path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
64         self.path = path
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")
74
75     def __repr__(self):
76         return "%s(%r)" % (self.__class__.__name__, self.path)
77
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())
82
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)
87
88     def _open_hostdb(self):
89         return StormHostDatabase(self._get_store())
90
91     def _load_compilers(self):
92         from buildfarm import util
93         return set(util.load_list(os.path.join(self.webdir, "compilers.list")))
94
95     def commit(self):
96         if self.store is not None:
97             self.store.commit()
98
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")
103         try:
104             lcov_html = open(file, 'r')
105         except (OSError, IOError):
106             # File does not exist
107             raise NoSuchBuildError(tree, self.LCOVHOST, "lcov")
108         try:
109             return lcov_extract_percentage(lcov_html)
110         finally:
111             lcov_html.close()
112
113     def get_build(self, tree, host, compiler, rev=None, checksum=None):
114         if rev is not None:
115             return self.builds.get_build(tree, host, compiler, rev,
116                 checksum=checksum)
117         else:
118             return self.upload_builds.get_build(tree, host, compiler)
119
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):
126                 yield build
127
128     def get_last_builds(self):
129         result = self._get_store().find(StormBuild)
130         return distinct_builds(result.order_by(Desc(StormBuild.upload_time)))
131
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)))
136
137     def host_last_build(self, host):
138         return max([build.upload_time for build in self.get_host_builds(host)])
139
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)))
143
144     def _get_store(self):
145         if self.store is not None:
146             return self.store
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)
151         return self.store
152
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"))