tests/killdaemons.py
author David Soria Parra <dsp@experimentalworks.net>
Thu, 29 Aug 2013 09:22:13 -0700
branchstable
changeset 1295 8e66ac9c0f0e
parent 1128 7f09e7ac63a7
child 1329 10d0d9d5c0f0
permissions -rw-r--r--
shelve: add a shelve extension to save/restore working changes This extension saves shelved changes using a temporary draft commit, and bundles the temporary commit and its draft ancestors, then strips them. This strategy makes it possible to use Mercurial's bundle and merge machinery to resolve conflicts if necessary when unshelving, even when the destination commit or its ancestors have been amended, squashed, or evolved. (Once a change has been unshelved, its associated unbundled commits are either rolled back or stripped.) Storing the shelved change as a bundle also avoids the difficulty that hidden commits would cause, of making it impossible to amend the parent if it is a draft commits (a common scenario). Although this extension shares its name and some functionality with the third party hgshelve extension, it has little else in common. Notably, the hgshelve extension shelves changes as unified diffs, which makes conflict resolution a matter of finding .rej files and conflict markers, and cleaning up the mess by hand. We do not yet allow hunk-level choosing of changes to record. Compared to the hgshelve extension, this is a small regression in usability, but we hope to integrate that at a later point, once the record machinery becomes more reusable and robust. [ 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