stdrun

What is stdrun ?

stdrun starts a new processes and intercepts it's stdout and stderr streams in order to apply custom actions.

The following features are supported:

  • Print stderr messages in red/bold

How to install ?

pip install stdrun

How to use ?

stdrun 'command'

Using stdrun as a Python library

Check the Run() documentation for details on how to use it as a library

 1r"""
 2# What is stdrun ?
 3
 4stdrun starts a new processes and intercepts it's stdout and stderr streams in order to apply custom actions.
 5
 6The following features are supported:
 7 - Print stderr messages in red/bold
 8
 9# How to install ?
10
11```sh
12pip install stdrun
13```
14
15# How to use ?
16```sh
17stdrun 'command'
18```
19
20# Using stdrun as a Python library
21
22Check the `Run()` documentation for details on how to use it as a library
23
24
25"""
26from .process import Run
27
28__all__ = ["Run"]
def Run( command: str, stdout_callback: Callable[[str], NoneType] = None, stderr_callback: Callable[[str], NoneType] = None, shell: bool = False) -> int:
 7def Run(
 8    command: str,
 9    stdout_callback: Callable[[str], None] = None,
10    stderr_callback: Callable[[str], None] = None,
11    shell: bool = False,
12) -> int:
13    """
14
15    - `command` contains the command to be execute
16    - `stdout_callback` is a function to be called when a line is received from the process stdout
17    - `stderr_callback` is a function to be called when a line is received from the process stderr
18
19    Returns: The exit code of the process execution
20    """
21
22    with Popen(
23        command,
24        stdout=PIPE,
25        stderr=PIPE,
26        text=True,
27        shell=shell,
28        universal_newlines=True,
29        bufsize=1,
30    ) as proc:
31
32        if stdout_callback:
33            stdout_thread = ReadlineThread(proc.stdout, stdout_callback)
34            stdout_thread.start()
35
36        if stderr_callback:
37            stderr_thread = ReadlineThread(proc.stderr, stderr_callback)
38            stderr_thread.start()
39
40        proc.wait()
41
42        if stdout_callback:
43            stdout_thread.join()
44
45        if stderr_callback:
46            stderr_thread.join()
47
48        return proc.returncode
  • command contains the command to be execute
  • stdout_callback is a function to be called when a line is received from the process stdout
  • stderr_callback is a function to be called when a line is received from the process stderr

Returns: The exit code of the process execution