Skip to content

Commit 35233a5

Browse files
committed
run: Support system signal as a coverage report dump trigger.
Signed-off-by: Arkady Gilinsky <9481855+ark-g@users.noreply.github.com>
1 parent dd0b354 commit 35233a5

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

coverage/cmdline.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import os
1111
import os.path
1212
import shlex
13+
import signal
1314
import sys
1415
import textwrap
1516
import traceback
17+
import types
1618

1719
from typing import cast, Any, NoReturn
1820

@@ -227,7 +229,15 @@ class Opts:
227229
"", "--version", action="store_true",
228230
help="Display version information and exit.",
229231
)
230-
232+
dump_signal = optparse.make_option(
233+
'', '--dump_signal', action='store', metavar='DUMP_SIGNAL',
234+
choices = ['USR1', 'USR2'],
235+
help=(
236+
"Define a system signal that will trigger coverage report dump. " +
237+
"It is important that target script do not intercept this signal. " +
238+
"Currently supported options are: USR1, USR2."
239+
),
240+
)
231241

232242
class CoverageOptionParser(optparse.OptionParser):
233243
"""Base OptionParser for coverage.py.
@@ -251,6 +261,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
251261
data_file=None,
252262
debug=None,
253263
directory=None,
264+
dump_signal=None,
254265
fail_under=None,
255266
format=None,
256267
help=None,
@@ -525,6 +536,7 @@ def get_prog_name(self) -> str:
525536
Opts.parallel_mode,
526537
Opts.source,
527538
Opts.timid,
539+
Opts.dump_signal,
528540
] + GLOBAL_ARGS,
529541
usage="[options] <pyfile> [program options]",
530542
description="Run a Python program, measuring code execution.",
@@ -807,6 +819,11 @@ def do_help(
807819

808820
return False
809821

822+
def do_dump(self, _signum: int, _frame: types.FrameType | None) -> None:
823+
""" Signal handler to dump coverage report """
824+
print("Dumping coverage data ...")
825+
self.coverage.save()
826+
810827
def do_run(self, options: optparse.Values, args: list[str]) -> int:
811828
"""Implementation of 'coverage run'."""
812829

@@ -851,6 +868,15 @@ def do_run(self, options: optparse.Values, args: list[str]) -> int:
851868
if options.append:
852869
self.coverage.load()
853870

871+
if options.dump_signal:
872+
if options.dump_signal.upper() == 'USR1':
873+
signal.signal(signal.SIGUSR1, self.do_dump)
874+
elif options.dump_signal.upper() == 'USR2':
875+
signal.signal(signal.SIGUSR2, self.do_dump)
876+
else:
877+
show_help(f"Unsupported signal for dump coverage report: {options.dump_signal}")
878+
return ERR
879+
854880
# Run the script.
855881
self.coverage.start()
856882
code_ran = True

doc/cmd.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,18 @@ There are many options:
137137
A list of directories or importable names of code to
138138
measure.
139139
--timid Use the slower Python trace function core.
140+
--dump_signal=DUMP_SIGNAL
141+
Define a system signal that will trigger coverage
142+
report dump. It is important that target script do not
143+
intercept this signal. Currently supported options
144+
are: USR1, USR2.
140145
--debug=OPTS Debug options, separated by commas. [env:
141146
COVERAGE_DEBUG]
142147
-h, --help Get help on this command.
143148
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
144149
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
145150
tried. [env: COVERAGE_RCFILE]
146-
.. [[[end]]] (sum: saD//ido/B)
151+
.. [[[end]]] (sum: kxkJi2xQZv)
147152
148153
If you want :ref:`branch coverage <branch>` measurement, use the ``--branch``
149154
flag. Otherwise only statement coverage is measured.
@@ -215,6 +220,9 @@ and may change in the future.
215220
These options can also be set in the :ref:`config_run` section of your
216221
.coveragerc file.
217222

223+
In case if you are specifying ``--dump_signal``, please make sure that
224+
your target script doesn't intercept this signal. Otherwise the coverage
225+
reports will not be generated.
218226

219227
.. _cmd_warnings:
220228

0 commit comments

Comments
 (0)