2017-05-16 01:54:51 +00:00
|
|
|
import subprocess
|
|
|
|
|
2017-05-24 21:11:37 +00:00
|
|
|
def execute_cmd(cmd, capture=False, **kwargs):
|
2017-05-16 01:54:51 +00:00
|
|
|
"""
|
2017-05-24 21:11:37 +00:00
|
|
|
Call given command, yielding output line by line if capture=True
|
2017-05-16 01:54:51 +00:00
|
|
|
"""
|
2017-05-24 21:11:37 +00:00
|
|
|
if capture:
|
|
|
|
kwargs['stdout'] = subprocess.PIPE
|
|
|
|
kwargs['stderr'] = subprocess.STDOUT
|
|
|
|
|
|
|
|
proc = subprocess.Popen(cmd, **kwargs)
|
|
|
|
|
|
|
|
if not capture:
|
|
|
|
ret = proc.wait()
|
|
|
|
if ret != 0:
|
|
|
|
raise subprocess.CalledProcessError(ret, cmd)
|
|
|
|
return
|
2017-05-19 07:07:39 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-24 21:11:37 +00:00
|
|
|
for line in iter(proc.stdout.readline, b''):
|
|
|
|
yield line.decode('utf8', 'replace')
|
2017-05-19 07:07:39 +00:00
|
|
|
finally:
|
|
|
|
ret = proc.wait()
|
|
|
|
if ret != 0:
|
|
|
|
raise subprocess.CalledProcessError(ret, cmd)
|