annotate mercurial/testing/ps_util.py @ 52164:e01e84e5e426

rust-revlog: add a Rust-only `InnerRevlog` This mirrors the Python `InnerRevlog` and will be used in a future patch to replace said Python implementation. This allows us to start doing more things in pure Rust, in particular reading and writing operations. A lot of changes have to be introduced all at once, it wouldn't be very useful to separate this patch IMO since all of them are either interlocked or only useful with the rest.
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 10 Oct 2024 10:34:51 +0200
parents 625cf9621551
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51996
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.
625cf9621551 tests: add a module that can perform the equivalent of `SIGKILL` on any OS
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
3 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
4 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
5 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
6 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
7 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
8
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 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
10 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
11 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
12 )
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
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 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
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 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
18 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
19 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
20 )
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 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
22 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
23 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
24 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
25
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 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
27 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
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 # 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
30 # 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
31 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
32
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 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
34 [
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 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
36 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
37 % 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
38 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
39 ],
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 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
41 )
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
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 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
45 """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
46 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
47 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
48 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
49 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
50 os.kill(pid, signal.SIGKILL)