pass env to aioproc

This commit is contained in:
Devaev Maxim 2021-04-09 06:19:34 +03:00
parent 6f60118320
commit 312605a70f

View File

@ -27,6 +27,8 @@ import logging
from typing import Tuple from typing import Tuple
from typing import List from typing import List
from typing import Dict
from typing import Optional
import setproctitle import setproctitle
@ -34,23 +36,39 @@ from .logging import get_logger
# ===== # =====
async def run_process(cmd: List[str], err_to_null: bool=False) -> asyncio.subprocess.Process: # pylint: disable=no-member async def run_process(
cmd: List[str],
err_to_null: bool=False,
env: Optional[Dict[str, str]]=None,
) -> asyncio.subprocess.Process: # pylint: disable=no-member
return (await asyncio.create_subprocess_exec( return (await asyncio.create_subprocess_exec(
*cmd, *cmd,
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=(asyncio.subprocess.DEVNULL if err_to_null else asyncio.subprocess.STDOUT), stderr=(asyncio.subprocess.DEVNULL if err_to_null else asyncio.subprocess.STDOUT),
preexec_fn=os.setpgrp, preexec_fn=os.setpgrp,
env=env,
)) ))
async def read_process(cmd: List[str], err_to_null: bool=False) -> Tuple[asyncio.subprocess.Process, str]: # pylint: disable=no-member async def read_process(
proc = await run_process(cmd, err_to_null) cmd: List[str],
err_to_null: bool=False,
env: Optional[Dict[str, str]]=None,
) -> Tuple[asyncio.subprocess.Process, str]: # pylint: disable=no-member
proc = await run_process(cmd, err_to_null, env)
(stdout, _) = await proc.communicate() (stdout, _) = await proc.communicate()
return (proc, stdout.decode(errors="ignore").strip()) return (proc, stdout.decode(errors="ignore").strip())
async def log_process(cmd: List[str], logger: logging.Logger) -> asyncio.subprocess.Process: # pylint: disable=no-member async def log_process(
(proc, stdout) = await read_process(cmd) cmd: List[str],
logger: logging.Logger,
env: Optional[Dict[str, str]]=None,
) -> asyncio.subprocess.Process: # pylint: disable=no-member
(proc, stdout) = await read_process(cmd, env=env)
if stdout: if stdout:
log = (logger.info if proc.returncode == 0 else logger.error) log = (logger.info if proc.returncode == 0 else logger.error)
for line in stdout.split("\n"): for line in stdout.split("\n"):