scenario.subprocess module

SubProcess class definition.

class SubProcess

Bases: object

Sub-process execution.

__init__(*args)
Parameters:

args – Command line arguments. May be the first arguments only, then rely on the addargs() method to add others.

cmd_line

Sub-process command line arguments.

See addargs().

env

See setenv().

cwd

See setcwd().

_logger

See setlogger().

_stdout_line_handler

Handler to call on each stdout line.

_stderr_line_handler

Handler to call on each stderr line.

_exit_on_error_code

See exitonerror().

returncode

Sub-process return code.

stdout

Standard output as a string.

stderr

Standard error as a string.

time

Time statistics.

_popen

subprocess.Popen instance.

_async

Tells whether the run() method should wait for the end of the sub-process.

_stdout_reader

Stdout reader thread routine.

_stderr_reader

Stderr reader thread routine.

__repr__()

Canonical string representation.

__str__()

Human readable string representation.

tostring()

Human readable full string representation.

addargs(*args)

Extra arguments addition.

Parameters:

args – Extra arguments.

Returns:

self

hasargs(*args)

Determines whether the command line contains the given sequence of consecutive arguments.

Parameters:

args – Sequence of arguments being searched.

Returns:

True when the arguments have been found, False otherwise.

setenv(**kwargs)

Sets extra environment variables.

Parameters:

kwargs – Extra environment variables.

Returns:

self

setcwd(cwd)

Sets the current working directory.

Parameters:

cwd – Current working directory.

Returns:

self

setlogger(logger)

Directs log lines to the given logger instance.

Parameters:

logger – Logger instance to use.

Returns:

`` self``

onstdoutline(handler)

Installs a handler to be called on each stdout line.

Parameters:

handler – Handler to call on each stdout line.

Returns:

self

onstderrline(handler)

Installs a handler to be called on each stderr line.

Parameters:

handler – Handler to call on each stderr line.

Returns:

self

exitonerror(exit_on_error_code)

Tells whether the main program should stop (sys.exit()) in case of an error.

Parameters:

exit_on_error_code – Set to None to keep executing in case of an error (default behaviour). Set to a ErrorCode value to make the main program stop with the given error code. True is an equivalent for errcodes.ErrorCode.INTERNAL_ERROR, False is an equivalent for None.

Returns:

self

The return code is available through the returncode attribute.

run(timeout=None)

Sub-process execution.

Parameters:

timeout – Waiting timeout, in seconds.``None`` to wait infinitely.

Returns:

self

The sub-process return code is available through the returncode attribute.

runasync()

Launches the sub-process asynchronously.

Contrary to run(), this method launches the sub-process, then returns without waiting for the end of it.

_readstdoutthread()

Stdout reader thread routine.

_readstderrthread()

Stderr reader thread routine.

isrunning()

Tells whether the sub-process is currently running.

Returns:

True when the sub-process is still running. False otherwise.

wait(timeout=None)

Waits for the sub-process to terminate.

Parameters:

timeout – Waiting timeout, in seconds. None to wait infinitely.

Returns:

self

Raises:

TimeoutError – When the sub-process did not terminate within timeout seconds.

kill()

Kills the sub-process.

Returns:

self

_onerror(error_message, *args)

Error management.

Optionally logs the error and terminates the main process.

Parameters:
  • error_message – Error message.

  • args – Error message arguments.

_log(level, message, *args)

Pushes a log line to the attached logger, if any.

Parameters:
  • level – Log level.

  • message – Log message.

  • args – Format arguments.