[PATCH 1 of 4 v2] run-tests: do not duplicate killdaemons() code

Patrick Mezard patrick at mezard.eu
Mon Aug 20 15:42:55 CDT 2012


# HG changeset patch
# User Patrick Mezard <patrick at mezard.eu>
# Date 1345387269 -7200
# Node ID c581659d731a035d6768b7ffb33296ebdfc3cae7
# Parent  a10f7eeb2588ae469b996288b0d2554ccbe409da
run-tests: do not duplicate killdaemons() code

diff --git a/tests/killdaemons.py b/tests/killdaemons.py
--- a/tests/killdaemons.py
+++ b/tests/killdaemons.py
@@ -2,24 +2,39 @@
 
 import os, time, errno, signal
 
-# Kill off any leftover daemon processes
-try:
-    fp = open(os.environ['DAEMON_PIDS'])
-    for line in fp:
-        try:
-            pid = int(line)
-        except ValueError:
-            continue
-        try:
-            os.kill(pid, 0)
-            os.kill(pid, signal.SIGTERM)
-            for i in range(10):
-                time.sleep(0.05)
+def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
+    if not logfn:
+        logfn = lambda s: s
+    # Kill off any leftover daemon processes
+    try:
+        fp = open(pidfile)
+        for line in fp:
+            try:
+                pid = int(line)
+            except ValueError:
+                continue
+            try:
                 os.kill(pid, 0)
-            os.kill(pid, signal.SIGKILL)
-        except OSError, err:
-            if err.errno != errno.ESRCH:
-                raise
-    fp.close()
-except IOError:
-    pass
+                logfn('# Killing daemon process %d' % pid)
+                os.kill(pid, signal.SIGTERM)
+                if tryhard:
+                    for i in range(10):
+                        time.sleep(0.05)
+                        os.kill(pid, 0)
+                else:
+                    time.sleep(0.1)
+                    os.kill(pid, 0)
+                logfn('# Daemon process %d is stuck - really killing it' % pid)
+                os.kill(pid, signal.SIGKILL)
+            except OSError, err:
+                if err.errno != errno.ESRCH:
+                    raise
+        fp.close()
+        if remove:
+            os.unlink(pidfile)
+    except IOError:
+        pass
+
+if __name__ == '__main__':
+    killdaemons(os.environ['DAEMON_PIDS'])
+
diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -54,6 +54,7 @@
 import time
 import re
 import threading
+import killdaemons as killmod
 
 processlock = threading.Lock()
 
@@ -348,29 +349,8 @@
         pass
 
 def killdaemons():
-    # Kill off any leftover daemon processes
-    try:
-        fp = open(DAEMON_PIDS)
-        for line in fp:
-            try:
-                pid = int(line)
-            except ValueError:
-                continue
-            try:
-                os.kill(pid, 0)
-                vlog('# Killing daemon process %d' % pid)
-                os.kill(pid, signal.SIGTERM)
-                time.sleep(0.1)
-                os.kill(pid, 0)
-                vlog('# Daemon process %d is stuck - really killing it' % pid)
-                os.kill(pid, signal.SIGKILL)
-            except OSError, err:
-                if err.errno != errno.ESRCH:
-                    raise
-        fp.close()
-        os.unlink(DAEMON_PIDS)
-    except IOError:
-        pass
+    return killmod.killdaemons(DAEMON_PIDS, tryhard=False, remove=True,
+                               logfn=vlog)
 
 def cleanup(options):
     if not options.keep_tmpdir:


More information about the Mercurial-devel mailing list