Move more functionality to buildfarm.BuildFarm.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 10 Nov 2010 07:32:38 +0000 (08:32 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 10 Nov 2010 07:32:38 +0000 (08:32 +0100)
buildfarm/__init__.py
buildfarm/data.py
web/build.py

index 467b22a9347cf181e620dc006c18a5197738d13c..f78b6fafe3759a5f2c226a6639ed3eb45337a1cf 100644 (file)
@@ -47,8 +47,20 @@ def read_trees_from_conf(path):
     return ret
 
 
+def lcov_extract_percentage(text):
+    m = re.search('\<td class="headerItem".*?\>Code\&nbsp\;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', text)
+    if m:
+        return m.group(1)
+    else:
+        return None
+
+
 class BuildFarm(object):
 
+    LCOVHOST = "magni"
+    OLDAGE = 60*60*4,
+    DEADAGE = 60*60*24*4
+
     def __init__(self, path=None):
         if path is None:
             path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
@@ -57,12 +69,18 @@ class BuildFarm(object):
         if not os.path.isdir(path):
             raise Exception("web directory %s does not exist" % self.webdir)
         self.trees = read_trees_from_conf(os.path.join(self.webdir, "trees.conf"))
+        self.builds = self._open_build_results()
         self.hostdb = self._open_hostdb()
         self.compilers = self._load_compilers()
+        self.lcovdir = os.path.join(self.path, "lcov/data")
 
     def __repr__(self):
         return "%s(%r)" % (self.__class__.__name__, self.path)
 
+    def _open_build_results(self):
+        from buildfarm import data
+        return data.BuildResultStore(self.path)
+
     def _open_hostdb(self):
         from buildfarm import hostdb
         return hostdb.HostDatabase(
@@ -71,3 +89,36 @@ class BuildFarm(object):
     def _load_compilers(self):
         from buildfarm import util
         return util.load_list(os.path.join(self.webdir, "compilers.list"))
+
+    def lcov_status(self, tree):
+        """get status of build"""
+        from buildfarm import data, util
+        cachefile = os.path.join(self.builds.cachedir, "lcov.%s.%s.status" % (
+            self.LCOVHOST, tree))
+        file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
+        try:
+            st1 = os.stat(file)
+        except OSError:
+            # File does not exist
+            raise data.NoSuchBuildError(tree, self.LCOVHOST, "lcov")
+        try:
+            st2 = os.stat(cachefile)
+        except OSError:
+            # file does not exist
+            st2 = None
+
+        if st2 and st1.st_ctime <= st2.st_mtime:
+            ret = util.FileLoad(cachefile)
+            if ret == "":
+                return None
+            return ret
+
+        lcov_html = util.FileLoad(file)
+        perc = lcov_extract_percentage(lcov_html)
+        if perc is None:
+            ret = ""
+        else:
+            ret = perc
+        if self.readonly:
+            util.FileSave(cachefile, ret)
+        return perc
index 40dcbb039828e265ecfbe3ec28347d00f6c4b290..7a32429531be1de8b776d7acbe0a875323dc0155 100644 (file)
@@ -155,14 +155,6 @@ def build_status_from_logs(log, err):
     return ret
 
 
-def lcov_extract_percentage(text):
-    m = re.search('\<td class="headerItem".*?\>Code\&nbsp\;covered\:\<\/td\>.*?\n.*?\<td class="headerValue".*?\>([0-9.]+) \%', text)
-    if m:
-        return m.group(1)
-    else:
-        return None
-
-
 class NoSuchBuildError(Exception):
     """The build with the specified name does not exist."""
 
@@ -359,10 +351,6 @@ class CachingBuild(Build):
 class BuildResultStore(object):
     """The build farm build result database."""
 
-    OLDAGE = 60*60*4,
-    DEADAGE = 60*60*24*4
-    LCOVHOST = "magni"
-
     def __init__(self, basedir, readonly=False):
         """Open the database.
 
@@ -379,9 +367,6 @@ class BuildResultStore(object):
         self.cachedir = os.path.join(basedir, "cache")
         check_dir_exists("cache", self.cachedir)
 
-        self.lcovdir = os.path.join(basedir, "lcov/data")
-        check_dir_exists("lcov", self.lcovdir)
-
     def get_build(self, tree, host, compiler, rev=None):
         logf = self.build_fname(tree, host, compiler, rev) + ".log"
         if not os.path.exists(logf):
@@ -400,38 +385,6 @@ class BuildResultStore(object):
             return os.path.join(self.datadir, "oldrevs/build.%s.%s.%s-%s" % (tree, host, compiler, rev))
         return os.path.join(self.datadir, "upload/build.%s.%s.%s" % (tree, host, compiler))
 
-    def lcov_status(self, tree):
-        """get status of build"""
-        cachefile = os.path.join(self.cachedir, "lcov.%s.%s.status" % (
-            self.LCOVHOST, tree))
-        file = os.path.join(self.lcovdir, self.LCOVHOST, tree, "index.html")
-        try:
-            st1 = os.stat(file)
-        except OSError:
-            # File does not exist
-            raise NoSuchBuildError(tree, self.LCOVHOST, "lcov")
-        try:
-            st2 = os.stat(cachefile)
-        except OSError:
-            # file does not exist
-            st2 = None
-
-        if st2 and st1.st_ctime <= st2.st_mtime:
-            ret = util.FileLoad(cachefile)
-            if ret == "":
-                return None
-            return ret
-
-        lcov_html = util.FileLoad(file)
-        perc = lcov_extract_percentage(lcov_html)
-        if perc is None:
-            ret = ""
-        else:
-            ret = perc
-        if self.readonly:
-            util.FileSave(cachefile, ret)
-        return perc
-
     def get_old_revs(self, tree, host, compiler):
         """get a list of old builds and their status."""
         ret = []
index c69cfa7e8128bdc4e0bb43be3b64753fc34bef94..67546a9b787d8b161f7e882c7899e4fd2f024b9b 100755 (executable)
@@ -55,7 +55,7 @@ compilers = buildfarm.compilers
 # host.properties are unicode object and the framework expect string object
 hosts = dict([(host.name.encode("utf-8"), host) for host in hostsdb.hosts()])
 trees = buildfarm.trees
-OLDAGE = db.OLDAGE
+OLDAGE = buildfarm.OLDAGE
 
 UNPACKED_BASE = "http://svn.samba.org/ftp/unpacked"
 GITWEB_BASE = "http://gitweb.samba.org"
@@ -210,12 +210,12 @@ def view_summary(myself, output_type):
                     yield "<td>"
             yield "%d</td>" % panic_count[tree]
             try:
-                lcov_status = db.lcov_status(tree)
+                lcov_status = buildfarm.lcov_status(tree)
             except data.NoSuchBuildError:
                 yield "<td></td>"
             else:
                 if lcov_status is not None:
-                    yield "<td><a href=\"/lcov/data/%s/%s\">%s %%</a></td>" % (db.LCOVHOST, tree, lcov_status)
+                    yield "<td><a href=\"/lcov/data/%s/%s\">%s %%</a></td>" % (buildfarm.LCOVHOST, tree, lcov_status)
                 else:
                     yield "<td></td>"
             yield "</tr>"