Updated exec_cmd to allow capturing while in debug mode
This commit is contained in:
@@ -68,7 +68,7 @@ class DEBPackage(Package):
|
|||||||
def get_installed_version(self, name):
|
def get_installed_version(self, name):
|
||||||
"""Get installed package version."""
|
"""Get installed package version."""
|
||||||
code, output = utils.exec_cmd(
|
code, output = utils.exec_cmd(
|
||||||
"dpkg -s {} | grep Version".format(name), capture_output=True)
|
"dpkg -s {} | grep Version".format(name))
|
||||||
match = re.match(r"Version: (\d:)?(.+)-\d", output.decode())
|
match = re.match(r"Version: (\d:)?(.+)-\d", output.decode())
|
||||||
if match:
|
if match:
|
||||||
return match.group(2)
|
return match.group(2)
|
||||||
@@ -97,7 +97,7 @@ class RPMPackage(Package):
|
|||||||
def get_installed_version(self, name):
|
def get_installed_version(self, name):
|
||||||
"""Get installed package version."""
|
"""Get installed package version."""
|
||||||
code, output = utils.exec_cmd(
|
code, output = utils.exec_cmd(
|
||||||
"rpm -qi {} | grep Version".format(name), capture_output=True)
|
"rpm -qi {} | grep Version".format(name))
|
||||||
match = re.match(r"Version\s+: (.+)", output.decode())
|
match = re.match(r"Version\s+: (.+)", output.decode())
|
||||||
if match:
|
if match:
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
|
|||||||
@@ -42,13 +42,15 @@ def user_input(message):
|
|||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
|
||||||
def exec_cmd(cmd, sudo_user=None, pinput=None, login=True, **kwargs):
|
def exec_cmd(cmd, sudo_user=None, login=True, **kwargs):
|
||||||
"""Execute a shell command.
|
"""
|
||||||
|
Execute a shell command.
|
||||||
|
|
||||||
Run a command using the current user. Set :keyword:`sudo_user` if
|
Run a command using the current user. Set :keyword:`sudo_user` if
|
||||||
you need different privileges.
|
you need different privileges.
|
||||||
|
|
||||||
:param str cmd: the command to execute
|
:param str cmd: the command to execute
|
||||||
:param str sudo_user: a valid system username
|
:param str sudo_user: a valid system username
|
||||||
:param str pinput: data to send to process's stdin
|
|
||||||
:rtype: tuple
|
:rtype: tuple
|
||||||
:return: return code, command output
|
:return: return code, command output
|
||||||
"""
|
"""
|
||||||
@@ -57,23 +59,21 @@ def exec_cmd(cmd, sudo_user=None, pinput=None, login=True, **kwargs):
|
|||||||
cmd = "sudo {}-u {} {}".format("-i " if login else "", sudo_user, cmd)
|
cmd = "sudo {}-u {} {}".format("-i " if login else "", sudo_user, cmd)
|
||||||
if "shell" not in kwargs:
|
if "shell" not in kwargs:
|
||||||
kwargs["shell"] = True
|
kwargs["shell"] = True
|
||||||
if pinput is not None:
|
capture_output = True
|
||||||
kwargs["stdin"] = subprocess.PIPE
|
|
||||||
capture_output = False
|
|
||||||
if "capture_output" in kwargs:
|
if "capture_output" in kwargs:
|
||||||
capture_output = kwargs.pop("capture_output")
|
capture_output = kwargs.pop("capture_output")
|
||||||
elif not ENV.get("debug"):
|
|
||||||
capture_output = True
|
|
||||||
if capture_output:
|
if capture_output:
|
||||||
kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
output = None
|
kwargs["universal_newlines"] = True
|
||||||
process = subprocess.Popen(cmd, **kwargs)
|
output: str = ""
|
||||||
if pinput or capture_output:
|
with subprocess.Popen(cmd, **kwargs) as process:
|
||||||
c_args = [pinput] if pinput is not None else []
|
if capture_output:
|
||||||
output = process.communicate(*c_args)[0]
|
for line in process.stdout:
|
||||||
else:
|
output += line
|
||||||
process.wait()
|
if ENV.get("debug"):
|
||||||
return process.returncode, output
|
sys.stdout.write(line)
|
||||||
|
|
||||||
|
return process.returncode, output.encode()
|
||||||
|
|
||||||
|
|
||||||
def dist_info():
|
def dist_info():
|
||||||
@@ -135,7 +135,6 @@ def settings(**kwargs):
|
|||||||
|
|
||||||
|
|
||||||
class ConfigFileTemplate(string.Template):
|
class ConfigFileTemplate(string.Template):
|
||||||
|
|
||||||
"""Custom class for configuration files."""
|
"""Custom class for configuration files."""
|
||||||
|
|
||||||
delimiter = "%"
|
delimiter = "%"
|
||||||
|
|||||||
Reference in New Issue
Block a user