Process operation utils

moulinette.utils.process.check_output(args, stderr=-2, shell=True, **kwargs)[source]

Run command with arguments and return its output as a byte string

Overwrite some of the arguments to capture standard error in the result and use shell by default before calling subprocess.check_output.

moulinette.utils.process.call_async_output(args, callback, **kwargs)[source]

Run command and provide its output asynchronously

Run command with arguments and wait for it to complete to return the returncode attribute. The callback can be a method or a 2-tuple of methods - for stdout and stderr respectively - which must take one byte string argument. It will be called each time the command produces some output.

The stdout and stderr additional arguments for the Popen constructor are not allowed as they are used internally.

Keyword arguments:
  • args – String or sequence of program arguments
  • callback – Method or object to call with output as argument
  • kwargs – Additional arguments for the Popen constructor
Returns:
Exit status of the command
moulinette.utils.process.run_commands(cmds, callback=None, separate_stderr=False, shell=True, **kwargs)[source]

Run multiple commands with error management

Run a list of commands and allow to manage how to treat errors either with raise_on_error or callback arguments.

If callback is provided, it will be called when the command returns a non-zero exit code. The callback must take 3 arguments; the returncode, the command which failed and the command output. The callback should return either False to stop commands execution or True to continue. Otherwise, if raise_on_error is True a CalledProcessError exception will be raised when a command returns a non-zero exit code.

If callback is provided or raise_on_error is False, all commands will be executed and the number of failed commands will be returned.

The standard output and error of a failed command can be separated with separate_stderr set to True. In that case, the output argument passed to the callback or the output attribute of the CalledProcessError exception will be a 2-tuple containing stdout and stderr as byte strings.

Keyword arguments:
  • cmds – List of commands to run
  • callback – Method or object to call on command failure. If no
    callback is given, a “subprocess.CalledProcessError” will be raised in case of command failure.
  • separate_stderr – True to return command output as a 2-tuple
  • kwargs – Additional arguments for the Popen constructor
Returns:
Number of failed commands