peltak.core.shell

class peltak.core.shell.ExecResult(command, return_code, stdout, stderr, succeeded, failed)[source]

Encapsulates a shell.run result.

command

The command that was executed.

Type

str

return_code

The command exit code.

Type

int

stdout

The command standard output content after the execution.

Type

str

stderr

The command standard error content after the execution.

Type

str

succeeded

True if command was successful (return_code was 0 or the one that was expected).

Type

bool

failed

True if command failed.

Type

bool

peltak.core.shell.cprint(msg, *args, **kw)[source]

Print colored message to stdout.

peltak.core.shell.decolorize(text)[source]

Remove all opcodes from the text.

Parameters

text (str) – Source text that includes color opcodes as used in shell.fmt function.

Returns

The same text but with all opcodes removed.

Return type

str

peltak.core.shell.fmt(msg, *args, **kw)[source]

Generate shell color opcodes from a pretty coloring syntax.

Return type

str

peltak.core.shell.highlight(code, fmt)[source]

Highlight a given code snippet for printing in the terminal.

Assumes 256 color terminal.

Return type

str

peltak.core.shell.run(cmd, capture=False, shell=True, env=None, exit_on_error=None, never_pretend=False)[source]

Run a shell command.

Parameters
  • cmd (str) – The shell command to execute.

  • shell (bool) – Same as in subprocess.Popen.

  • capture (bool) – If set to True, it will capture the standard input/error instead of just piping it to the caller stdout/stderr.

  • env (dict[str, str]) – The subprocess environment variables.

  • exit_on_error (bool) – If set to True, on failure it will call sys.exit with the return code for the executed command.

  • never_pretend (bool) – If set to True the command will always be executed, even if context.get(‘pretend’) is set to True. If set to False or not given, if the pretend context value is True, this function will only print the command it would execute and then return a fake result.

Returns

The execution result containing the return code and output (if capture was set to True).

Return type

ExecResult