From 0d711dba180d583e99c9a1d5d3f8e5733220940e Mon Sep 17 00:00:00 2001
From: Daniel Brown <ddb@star.sr.bham.ac.uk>
Date: Wed, 7 Aug 2013 15:22:17 +0100
Subject: [PATCH] fixing kat storing, now if the use previous build box is
 ticked a previously built kat file will be used instead of building exactly
 the same again.

---
 pykat/testing/test.py                         |  4 +-
 pykat/testing/utils.py                        |  2 +-
 pykat/testing/web/templates/finesse_test.html |  5 ++-
 pykat/testing/web/web_interface.py            | 39 ++++++++++++++++---
 4 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/pykat/testing/test.py b/pykat/testing/test.py
index 30d4b40..4d6f601 100644
--- a/pykat/testing/test.py
+++ b/pykat/testing/test.py
@@ -126,7 +126,9 @@ class FinesseTestProcess(Thread):
         self.built = False
 
         BUILD_PATH = os.path.join(self.BASE_DIR, "build")
-                    
+
+        print "NOBUILD", self.nobuild
+        
         # Firstly we need to build the latest version of finesse
         if not self.nobuild:
             
diff --git a/pykat/testing/utils.py b/pykat/testing/utils.py
index ae8b361..0e68a7f 100644
--- a/pykat/testing/utils.py
+++ b/pykat/testing/utils.py
@@ -20,7 +20,7 @@ def runcmd(args, cwd="."):
     out, err = p.communicate()
     
     if p.returncode != 0:
-        print "STDERR: " + errk
+        print "STDERR: " + err
         print "STDOUT: " + out
         raise RunException(p.returncode, args, err, out)
 
diff --git a/pykat/testing/web/templates/finesse_test.html b/pykat/testing/web/templates/finesse_test.html
index 15fcfca..487dcfd 100644
--- a/pykat/testing/web/templates/finesse_test.html
+++ b/pykat/testing/web/templates/finesse_test.html
@@ -67,7 +67,9 @@
                 </select>
                 
                 <input type="button" value="Refresh" id="btnGetLogs"/>
+                <br/>
                 
+                <input type="checkbox" id="chkNoBuild" />Use previously built kat (Will built if it can't find any previous kats of the requested version)<br>
                 <input type="button" value="Start new FINESSSE test" id="btnStartTest"/>
                 
                 <section class="container">
@@ -350,7 +352,8 @@
                 }
                 
                 list = $("#commit_list");
-                data = JSON.stringify({"git_commit": list.val(), "kats":kats});
+                
+                data = JSON.stringify({"git_commit": list.val(), "kats":kats, "nobuild":$('#chkNoBuild').is(':checked')});
                 
                 $.ajax({
                   type: "POST",
diff --git a/pykat/testing/web/web_interface.py b/pykat/testing/web/web_interface.py
index 8ab9700..51f174c 100644
--- a/pykat/testing/web/web_interface.py
+++ b/pykat/testing/web/web_interface.py
@@ -61,9 +61,9 @@ else:
 SRC_GIT_PATH = os.path.join(app.instance_path, "finesse_src",".git")
 
 # get HEAD commit to set as starting point for commit checker
-latest_data = utils.git(["log","-2",'--pretty=format:"%H"'],cwd=SRC_GIT_PATH)
-
-latest_commit_id_tested = latest_data[0].split("\n")[1].replace('"',"").replace("\\","")
+prev_commits = 10
+latest_data = utils.git(["log","-" + str(prev_commits),'--pretty=format:"%H"'],cwd=SRC_GIT_PATH)
+latest_commit_id_tested = latest_data[0].split("\n")[prev_commits-1].replace('"',"").replace("\\","")
 
 print "loading web interface"
 
@@ -327,10 +327,17 @@ def finesse_start_rerun(id):
     
 @app.route('/finesse/start_test', methods=["POST"])
 def finesse_start_test():
+    nobuild = False
+        
+    if "nobuild" in request.json:
+        if request.json["nobuild"] == True:
+            nobuild = True
+        else:
+            nobuild = False
     
-    return jsonify(__finesse_start_test(request.json["git_commit"], request.json["kats"]))
+    return jsonify(__finesse_start_test(request.json["git_commit"], request.json["kats"], nobuild))
     
-def __finesse_start_test(git_commit, kats):
+def __finesse_start_test(git_commit, kats, nobuild=False):
     global current_test, test_id
     
     try:
@@ -350,11 +357,31 @@ def __finesse_start_test(git_commit, kats):
             
         os.mkdir(TEST_RUN_PATH)
         
+        # if we are not building then check to see if we can find a previous version
+        # of kat and if so put it in the correct folder for the test to find
+        if nobuild:
+            if sys.platform == "win32":
+                EXE = ".exe"
+            else:
+                EXE = ""
+                
+            TEST_BUILD_PATH = os.path.join(TEST_RUN_PATH,"build")
+            KAT_EXE = os.path.join(app.instance_path, "kat_store", "kat_" + str(git_commit) + EXE)
+            
+            if os.path.exists(KAT_EXE):
+                print "using existing kat file " + KAT_EXE
+                utils.git(["clone","git://gitmaster.atlas.aei.uni-hannover.de/finesse/base.git",TEST_BUILD_PATH])
+                KAT_NEW_EXE = os.path.join(TEST_BUILD_PATH,"kat" + EXE)
+                shutil.copyfile(KAT_EXE, KAT_NEW_EXE)
+                
+          
+        print "nobuild", nobuild
+        
         test = finesse_test.FinesseTestProcess(os.path.join(app.instance_path, "finesse_test"), 
                                       TEST_RUN_PATH,
                                       git_commit, 
                                       run_fast=False, kats=kats, test_id=test_id,
-                                      emails="", nobuild=False)
+                                      emails="", nobuild=nobuild)
         
         db.insert(dict(t="test",
                        test_id=test.test_id,
-- 
GitLab