autobuild: Add --daemon option.
[ira/wip.git] / script / autobuild.py
index f1e29a7a83bb4095cc100d51641eea9dbf722b24..40635439c238e322e10e3d0bcafab9713958039d 100755 (executable)
@@ -24,14 +24,12 @@ tasks = {
                   "make install",
                   "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
 
-    "source4" : [ "./autogen.sh",
-                  "./configure.developer ${PREFIX}",
+    "source4" : [ "./configure.developer ${PREFIX}",
                   "make -j",
                   "make install",
                   "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
 
-    "source4/lib/ldb" : [ "./autogen-waf.sh",
-                          "./configure --enable-developer -C ${PREFIX}",
+    "source4/lib/ldb" : [ "./configure --enable-developer -C ${PREFIX}",
                           "make -j",
                           "make install",
                           "make test" ],
@@ -54,8 +52,7 @@ tasks = {
                       "make install",
                       "make test" ],
 
-    "lib/tevent" : [ "./autogen-waf.sh",
-                     "./configure --enable-developer -C ${PREFIX}",
+    "lib/tevent" : [ "./configure --enable-developer -C ${PREFIX}",
                      "make -j",
                      "make install",
                      "make test" ],
@@ -226,7 +223,7 @@ class buildlist:
     def start_tail(self):
         cwd = os.getcwd()
         cmd = "tail -f *.stdout *.stderr"
-        os.chdir(testbase)
+        os.chdir(gitroot)
         self.tail_proc = Popen(cmd, shell=True)
         os.chdir(cwd)
 
@@ -241,18 +238,37 @@ def cleanup():
 
 def find_git_root():
     '''get to the top of the git repo'''
-    cwd=os.getcwd()
-    while os.getcwd() != '/':
+    p=os.getcwd()
+    while p != '/':
+        if os.path.isdir(os.path.join(p, ".git")):
+            return p
+        p = os.path.abspath(os.path.join(p, '..'))
+    return None
+
+
+def daemonize(logfile):
+    pid = os.fork()
+    if pid == 0: # Parent
+        os.setsid()
+        pid = os.fork()
+        if pid != 0: # Actual daemon
+            os._exit(0)
+    else: # Grandparent
+        os._exit(0)
+
+    import resource      # Resource usage information.
+    maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
+    if maxfd == resource.RLIM_INFINITY:
+        maxfd = 1024 # Rough guess at maximum number of open file descriptors.
+    for fd in range(0, maxfd):
         try:
-            os.stat(".git")
-            ret = os.getcwd()
-            os.chdir(cwd)
-            return ret
-        except:
-            os.chdir("..")
+            os.close(fd)
+        except OSError:
             pass
-    os.chdir(cwd)
-    return None
+    os.open(logfile, os.O_RDWR | os.O_CREAT)
+    os.dup2(0, 1)
+    os.dup2(0, 2)
+
 
 def rebase_tree(url):
     print("Rebasing on %s" % url)
@@ -303,6 +319,8 @@ parser.add_option("", "--retry", help="automatically retry if master changes",
                   default=False, action="store_true")
 parser.add_option("", "--email", help="send email to the given address on failure",
                   type='str', default=None)
+parser.add_option("", "--daemon", help="daemonize after initial setup",
+                  action="store_true")
 
 
 def email_failure(status, failed_task, failed_tag, errstr):
@@ -355,6 +373,11 @@ except Exception, reason:
     raise Exception("Unable to create %s : %s" % (testbase, reason))
 cleanup_list.append(testbase)
 
+if options.daemon:
+    logfile = os.path.join(testbase, "log")
+    print "Forking into the background, writing progress to %s" % logfile
+    daemonize(logfile)
+
 while True:
     try:
         run_cmd("rm -rf %s" % test_master)