lib.shell module

Helper utilities related to the subprocess module and the shell.

class Process(popen, *, timeout=15)[source]

Bases: object

A wrapper around a subprocess.Popen(…) object.

Examples

>>> from subprocess import PIPE, Popen
>>> echo_factory = lambda x: Popen(["echo", x], stdout=PIPE)
>>> echo_popen = echo_factory("foo")
>>> echo_proc = Process(echo_popen)
>>> echo_proc.out
'foo'
>>> echo_popen = echo_factory("bar")
>>> out, _err = Process(echo_popen)
>>> out
'bar'
Parameters
  • popen (Popen) –

  • timeout (float) –

to_error(*, up=0)[source]

Converts a Process object into an Err(…) object..

Parameters

up (int) –

Return type

Err[Process, BugyiError]

exception StillAliveException(pid)[source]

Bases: Exception

Raised when Old Instance of Script is Still Running

Parameters

pid (int) –

command_exists(cmd)[source]

Returns True iff the shell command cmd exists.

Parameters

cmd (str) –

Return type

bool

create_pidfile(*, up=0)[source]

Writes PID to file, which is created if necessary.

Raises

StillAliveException – if old instance of script is still alive.

Parameters

up (int) –

Return type

None

safe_popen(cmd_parts, *, up=0, timeout=15, **kwargs)[source]

Wrapper for subprocess.Popen(…).

Return type

Union[Ok[Process, BugyiError], Err[Process, BugyiError]]

Returns

Ok(Process) if the command is successful.

OR

Err(BugyiError) otherwise.

Parameters
  • cmd_parts (Iterable[str]) –

  • up (int) –

  • timeout (float) –

  • kwargs (Any) –

unsafe_popen(cmd_parts, *, timeout=15, **kwargs)[source]

Wrapper for subprocess.Popen(…)

You can use unsafe_popen() instead of safe_popen() when you don’t care whether or not the command succeeds.

Return type

Process

Returns

A Process(…) object.

Parameters
  • cmd_parts (Iterable[str]) –

  • timeout (float) –

  • kwargs (Any) –