Exec

This module provides helpers to run subprocesses and get their output.

It handles subprocesses stderr and stdout in a way that doesn’t break loggers from yuio.io.

yuio.exec.exec(*args: str, cwd: None | str | Path = None, env: Dict[str, str] | None = None, input: None | str | bytes = None, level: int = 10, text: bool = True)[source]

Run an executable and return its stdout.

Command’s stderr is interactively printed to the log.

If the command fails, a CalledProcessError is raised.

Parameters:
  • args – command arguments.

  • cwd – set the current directory before the command is executed.

  • env – define the environment variables for the command.

  • input – string with command’s stdin.

  • level – logging level for stderr outputs. By default, it is set to logging.DEBUG, which hides all the output.

  • text – if True (default), decode stdout using the system default encoding.

Returns:

string (or bytes) with command’s stdout.

yuio.exec.sh(cmd: str, /, *, shell: str = '/bin/sh', cwd: str | Path | None = None, env: Dict[str, str] | None = None, input: str | bytes | None = None, level: int = 10, text: bool = True)[source]

Run command in a shell, return its stdout.

Parameters:
  • cmd – shell command.

  • shell – which shell to use. Default is /bin/sh.

  • cwd – set the current directory before the command is executed.

  • env – define the environment variables for the command.

  • input – string with command’s stdin.

  • level – logging level for stderr outputs. By default, it is set to logging.DEBUG, which hides all the output.

  • text – if True (default), decode stdout using the system default encoding.

Returns:

string (or bytes) with command’s stdout.