40 lines
1 KiB
Python
40 lines
1 KiB
Python
"""Handle processes."""
|
|
|
|
import subprocess
|
|
|
|
from rwx import txt
|
|
|
|
|
|
def get_tuples_args(*items: str | tuple[str]) -> list[str]:
|
|
"""Turn arguments tuples into an arguments list."""
|
|
args: list[str] = []
|
|
for item in items:
|
|
if type(item) is tuple:
|
|
args.extend(item)
|
|
else:
|
|
args.append(item)
|
|
return args
|
|
|
|
|
|
def run(*items: str | tuple[str]) -> subprocess.CompletedProcess:
|
|
"""Run from a list of arguments tuples."""
|
|
return subprocess.run(
|
|
get_tuples_args(*items), capture_output=False, check=True
|
|
)
|
|
|
|
|
|
def run_line(*items: str | tuple[str], charset: str = txt.CHARSET) -> str:
|
|
"""Run and return output line."""
|
|
lines = run_lines(*items, charset=charset)
|
|
return lines[0]
|
|
|
|
|
|
def run_lines(
|
|
*items: str | tuple[str], charset: str = txt.CHARSET
|
|
) -> list[str]:
|
|
"""Run and return output lines."""
|
|
process = subprocess.run(
|
|
get_tuples_args(*items), capture_output=True, check=True
|
|
)
|
|
string = process.stdout.decode(charset)
|
|
return string.rstrip().splitlines()
|