Skip to content

Commit

Permalink
feat: add subprocess module with cmd and shell
Browse files Browse the repository at this point in the history
These helper functions makes it easy to run arbitrary commands on the
user config file.
  • Loading branch information
kriansa committed Jul 30, 2022
1 parent e4e352e commit 981bc88
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions wmcompanion/utils/subprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) 2022 Daniel Pereira
#
# SPDX-License-Identifier: Apache-2.0

import asyncio
from logging import getLogger

logger = getLogger(__name__)

async def cmd(cmd: str, *args: list[str], env: dict = None, output_encoding: str = "utf-8"):
"""
Run a command in the existing thread event loop and return its return code and outputs.
"""
proc = await asyncio.create_subprocess_exec(
cmd, *args, env=env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

stdout, stderr = await proc.communicate()

if proc.returncode != 0:
stderr_str = stderr.decode(output_encoding).strip()
logger.warn(f"Process '{cmd}' returned {proc.returncode}: {stderr_str}")

return dict(
rc=proc.returncode,
stderr=stderr.decode(output_encoding),
stdout=stdout.decode(output_encoding),
)

async def shell(cmd: str, env: dict = None, output_encoding: str = "utf-8"):
"""
Run a shell command in the existing thread event loop and return its return code and outputs.
"""
proc = await asyncio.create_subprocess_shell(
cmd, env=env,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

stdout, stderr = await proc.communicate()

if proc.returncode != 0:
stderr_str = stderr.decode(output_encoding).strip()
logger.warn(f"Shell command '{cmd}' returned {proc.returncode}: {stderr_str}")

return dict(
rc=proc.returncode,
stderr=stderr.decode(output_encoding),
stdout=stdout.decode(output_encoding),
)

0 comments on commit 981bc88

Please sign in to comment.