Skip to content

Commit

Permalink
try passing to python logging to fix isce-framework#15
Browse files Browse the repository at this point in the history
  • Loading branch information
scottstanie committed Dec 22, 2023
1 parent 705e2e2 commit 65a0ffa
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/snaphu/_snaphu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib.resources
import logging
import os
import re
import subprocess
Expand All @@ -15,6 +16,9 @@
]


logger = logging.getLogger(__name__)


@contextmanager
def get_snaphu_executable() -> Generator[Path, None, None]:
"""
Expand Down Expand Up @@ -70,6 +74,27 @@ def get_snaphu_version() -> str:
return match.group("version")


def logging_call(popenargs: list[str]) -> None:
process = subprocess.Popen(
popenargs,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)

def check_io() -> None:
while True:
output = process.stdout
if output:
line = output.readline().decode().strip()
logger.info(line)
else:
break

# keep checking stdout/stderr until the child exits
while process.poll() is None:
check_io()


def run_snaphu(config_file: str | os.PathLike[str]) -> None:
"""
Run SNAPHU with the specified config file.
Expand All @@ -86,7 +111,7 @@ def run_snaphu(config_file: str | os.PathLike[str]) -> None:
with get_snaphu_executable() as snaphu:
args = [os.fspath(snaphu), "-f", os.fspath(config_file)]
try:
subprocess.run(args, stderr=subprocess.PIPE, check=True, text=True)
logging_call(args)
except subprocess.CalledProcessError as e:
errmsg = e.stderr.strip()
raise RuntimeError(errmsg) from e

0 comments on commit 65a0ffa

Please sign in to comment.