Shell

class shell_logger.shell.Shell(pwd=None, *, login_shell=False)[source]

Bases: object

Manage interactions with the underlying shell.

Spawns a shell subprocess that inherits five unnamed pipes (stdout, stderr, stdin, aux_stdout, aux_stderr).

login_shell

Whether or not the spawned shell should be a login shell.

Type:

bool

aux_stdin_rfd

The read file descriptor for stdin.

Type:

int

aux_stdin_wfd

The write file descriptor for stdin.

Type:

int

aux_stdout_rfd

The read file descriptor for stdout.

Type:

int

aux_stdout_wfd

The write file descriptor for stdout.

Type:

int

aux_stderr_rfd

The read file descriptor for stderr.

Type:

int

aux_stderr_wfd

The write file descriptor for stderr.

Type:

int

shell_subprocess

The subprocess for interacting with the shell.

Type:

Popen[str]

Initialize a Shell object.

Parameters:
  • pwd (Optional[Path]) – The directory to change to when starting the Shell.

  • login_shell (bool) – Whether or not the spawned shell should be a login shell.

auxiliary_command(**kwargs)[source]

Run auxiliary commands like umask, pwd, env, etc.

Parameters:

**kwargs – Additional arguments.

Note: This is effectively the same as run(), but:
  1. The stdout and stderr get redirected to the auxiliary file descriptors.

  2. You don’t tee the stdout or stderr.

Todo

  • Maybe combine this with run() with extra flags.

  • Replace **kwargs with function arguments.

Return type:

Tuple[Optional[str], Optional[str]]

Returns:

The stdout and stderr of the command run.

cd(path)[source]

Change to the given directory.

Parameters:

path (Path) – The directory to change to.

Return type:

None

pwd()[source]

Get the current working directory.

Return type:

str

Returns:

The current working directory.

run(command, **kwargs)[source]

Run a command in the underlying shell.

Write a command to the Shell class’ shell subprocess’ stdin, and pull the stdout and stderr.

Parameters:
  • command (str) – The command to run in the shell subprocess.

  • **kwargs – Any additional arguments to pass to tee().

Return type:

SimpleNamespace

Returns:

The command run, along with its return code, stdout, stderr, start/stop time, and duration.

static tee(stdout, stderr, **kwargs)[source]

Write output/error streams to multiple files.

Split stdout and stderr file objects to write to multiple files.

Parameters:
  • stdout (Optional[IO[bytes]]) – The stdout file object to be split.

  • stderr (Optional[IO[bytes]]) – The stderr file object to be split.

  • **kwargs – Additional arguments.

Return type:

SimpleNamespace

Returns:

The stdout and stderr as strings.

Todo

  • Replace **kwargs with function arguments.