reprospect.utils.subprocess_helpers module

reprospect.utils.subprocess_helpers.popen_stream(*, args: Sequence[str | Path], **kwargs) Generator[str, None, None]View on GitHub

Yield lines from a subprocess.Popen lazily, with robust error handling and resource cleanup.

Parameters:
  • args – Command to run.

  • kwargs – Additional arguments to pass to subprocess.Popen.

  1. Launch a subprocess with both stdout and stderr captured as text streams.

  2. Lazily yield each line from stdout as it becomes available.

  3. After stdout is exhausted, wait for the process to finish.

  4. If the process exits with a nonzero return code, raise subprocess.CalledProcessError with captured stderr.

  5. Guarantee resource cleanup:

    1. If the process hasn’t finished, it is terminated.

    2. If termination fails (within 2 seconds), the process is forcibly killed.