mercurial/testing/ps_util.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 11 Mar 2025 02:29:42 +0100
branchstable
changeset 53042 cdd7bf612c7b
parent 52350 d4e30c15626d
permissions -rw-r--r--
bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51992
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     1
# This python code can be imported into tests in order to terminate a process
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     2
# with signal.SIGKILL on posix, or a roughly equivalent procedure on Windows.
52350
d4e30c15626d typing: add missing `from __future__ import annotations` to core modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 51992
diff changeset
     3
d4e30c15626d typing: add missing `from __future__ import annotations` to core modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 51992
diff changeset
     4
from __future__ import annotations
d4e30c15626d typing: add missing `from __future__ import annotations` to core modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 51992
diff changeset
     5
51992
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     6
import os
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     7
import signal
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     8
import subprocess
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     9
import sys
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    10
import tempfile
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    11
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    12
from .. import (
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    13
    encoding,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    14
    pycompat,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    15
)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    16
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    17
from ..utils import procutil
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    18
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    19
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    20
def kill_nt(pid: int, exit_code: int):
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    21
    fd, pidfile = tempfile.mkstemp(
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    22
        prefix=b"sigkill-", dir=encoding.environ[b"HGTMP"], text=False
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    23
    )
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    24
    try:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    25
        os.write(fd, b'%d\n' % pid)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    26
    finally:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    27
        os.close(fd)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    28
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    29
    env = dict(encoding.environ)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    30
    env[b"DAEMON_EXITCODE"] = b"%d" % exit_code
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    31
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    32
    # Simulate the message written to stderr for this process on non-Windows
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    33
    # platforms, for test consistency.
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    34
    print("Killed!", file=sys.stderr)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    35
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    36
    subprocess.run(
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    37
        [
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    38
            encoding.environ[b"PYTHON"],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    39
            b"%s/killdaemons.py"
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    40
            % encoding.environ[b'RUNTESTDIR_FORWARD_SLASH'],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    41
            pidfile,
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    42
        ],
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    43
        env=procutil.tonativeenv(env),
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    44
    )
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    45
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    46
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    47
def kill(pid: int):
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    48
    """Kill the process with the given PID with SIGKILL or equivalent."""
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    49
    if pycompat.iswindows:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    50
        exit_code = 128 + 9
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    51
        kill_nt(pid, exit_code)
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    52
    else:
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    53
        os.kill(pid, signal.SIGKILL)