[util/cli] Add new CLI utilities
For now, this just allows executing commands
This commit is contained in:
parent
f9267f2131
commit
9d4936b596
2 changed files with 53 additions and 0 deletions
20
util/cli.py
Normal file
20
util/cli.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
import shlex
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
def execute(cmd, wait=True):
|
||||
args = shlex.split(cmd)
|
||||
logging.debug(cmd)
|
||||
try:
|
||||
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
except FileNotFoundError as e:
|
||||
raise RuntimeError('{} not found'.format(cmd))
|
||||
|
||||
if wait:
|
||||
out, _ = proc.communicate()
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError('{} exited with {}'.format(cmd, proc.returncode))
|
||||
return out.decode('utf-8')
|
||||
return ''
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
Loading…
Add table
Add a link
Reference in a new issue