Can get output returned from run_command
This commit is contained in:
parent
35da19e323
commit
f21998612f
1 changed files with 16 additions and 3 deletions
|
|
@ -59,26 +59,34 @@ class Shell:
|
||||||
)
|
)
|
||||||
return prompt.options(message, options)
|
return prompt.options(message, options)
|
||||||
|
|
||||||
def run_command(self, cmd, suppress_message=False):
|
def run_command(self, cmd, suppress_message=False, return_output=False):
|
||||||
"""
|
"""
|
||||||
Run a shell command and show the output as it runs
|
Run a shell command and show the output as it runs
|
||||||
"""
|
"""
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||||
)
|
)
|
||||||
|
full_output = ""
|
||||||
while True:
|
while True:
|
||||||
output = proc.stdout.readline()
|
output = proc.stdout.readline()
|
||||||
if len(output) == 0 and proc.poll() is not None:
|
if len(output) == 0 and proc.poll() is not None:
|
||||||
break
|
break
|
||||||
if output and not suppress_message:
|
if output:
|
||||||
self.info(output.decode("utf-8").strip())
|
decoded_output = output.decode("utf-8").strip()
|
||||||
|
if not suppress_message:
|
||||||
|
self.info(decoded_output)
|
||||||
|
full_output += decoded_output
|
||||||
r = proc.poll()
|
r = proc.poll()
|
||||||
if r == 0:
|
if r == 0:
|
||||||
|
if return_output:
|
||||||
|
return full_output
|
||||||
return True
|
return True
|
||||||
|
|
||||||
err = proc.stderr.read()
|
err = proc.stderr.read()
|
||||||
if not suppress_message:
|
if not suppress_message:
|
||||||
self.error(err.decode("utf-8"))
|
self.error(err.decode("utf-8"))
|
||||||
|
if return_output:
|
||||||
|
return full_output
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def info(self, message):
|
def info(self, message):
|
||||||
|
|
@ -366,6 +374,11 @@ class Shell:
|
||||||
service_file.write(content)
|
service_file.write(content)
|
||||||
service_file.close()
|
service_file.close()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_python3():
|
||||||
|
"Check if we are running Python 3 or later"
|
||||||
|
return int(platform.python_version()[0]) >= 3
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_linux():
|
def is_linux():
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue