tests/killdaemons.py
author Mads Kiilerich <madski@unity3d.com>
Tue, 15 Jan 2013 23:30:10 +0100
branchstable
changeset 1196 c0daf79e5680
parent 1128 7f09e7ac63a7
child 1329 10d0d9d5c0f0
permissions -rw-r--r--
tests: run with PYTHONHASHSEED=random Python set and dict iteration order is in principle undefined but usually 'quite stable'. Setting PYTHONHASHSEED=random will make the iteration order more random in Python 2.6.8 and 2.7.3 and where it has been backported. This can thus help spot dependencies on undefined behaviour and prevent future problems. [ original upstream message ]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1123
17bcbb020dda tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
#!/usr/bin/env python
17bcbb020dda tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
1128
7f09e7ac63a7 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 1127
diff changeset
     3
import os, sys, time, errno, signal
1123
17bcbb020dda tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
1127
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
     5
if os.name =='nt':
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
     6
    import ctypes
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
     7
    def kill(pid, logfn, tryhard=True):
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
     8
        logfn('# Killing daemon process %d' % pid)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
     9
        PROCESS_TERMINATE = 1
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    10
        handle = ctypes.windll.kernel32.OpenProcess(
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    11
                PROCESS_TERMINATE, False, pid)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    12
        ctypes.windll.kernel32.TerminateProcess(handle, -1)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    13
        ctypes.windll.kernel32.CloseHandle(handle)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    14
else:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    15
    def kill(pid, logfn, tryhard=True):
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    16
        try:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    17
            os.kill(pid, 0)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    18
            logfn('# Killing daemon process %d' % pid)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    19
            os.kill(pid, signal.SIGTERM)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    20
            if tryhard:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    21
                for i in range(10):
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    22
                    time.sleep(0.05)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    23
                    os.kill(pid, 0)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    24
            else:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    25
                time.sleep(0.1)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    26
                os.kill(pid, 0)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    27
            logfn('# Daemon process %d is stuck - really killing it' % pid)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    28
            os.kill(pid, signal.SIGKILL)
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    29
        except OSError, err:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    30
            if err.errno != errno.ESRCH:
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    31
                raise
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    32
1126
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    33
def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    34
    if not logfn:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    35
        logfn = lambda s: s
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    36
    # Kill off any leftover daemon processes
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    37
    try:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    38
        fp = open(pidfile)
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    39
        for line in fp:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    40
            try:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    41
                pid = int(line)
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    42
            except ValueError:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    43
                continue
1127
79d7aa832cf4 killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 1126
diff changeset
    44
            kill(pid, logfn, tryhard)
1126
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    45
        fp.close()
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    46
        if remove:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    47
            os.unlink(pidfile)
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    48
    except IOError:
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    49
        pass
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    50
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    51
if __name__ == '__main__':
1128
7f09e7ac63a7 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 1127
diff changeset
    52
    path, = sys.argv[1:]
7f09e7ac63a7 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 1127
diff changeset
    53
    killdaemons(path)
1126
dbcb11553a3b run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 1125
diff changeset
    54