squid-dl/squid_dl/util.py

43 lines
936 B
Python
Raw Normal View History

2021-10-16 01:04:43 -04:00
#!/usr/bin/env python3
import subprocess
from sys import stderr as STDERR
import typing
def eprint(errmsg):
print(errmsg, file=STDERR)
def die(errmsg, stat: int = 1):
"""Prints message and exits Python with a status of stat."""
eprint(errmsg)
exit(stat)
def runcmd(args):
"""
Run a given program/shell command and return its output.
Error Handling
==============
If the spawned proccess returns a nonzero exit status, it will print the
program's ``STDERR`` to the running Python iterpreter's ``STDERR``.
"""
proc = subprocess.Popen(
args,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
if proc.wait() == 1:
print(proc.stdout.read().decode())
eprint(proc.stderr.read().decode())
return proc.stdout.read()
except KeyboardInterrupt:
proc.terminate()
return b""