Skip to content

Commit

Permalink
Updated exec_cmd to allow capturing while in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tonioo committed Aug 30, 2023
1 parent 4782000 commit 23aabbf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions modoboa_installer/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def install_many(self, names):
def get_installed_version(self, name):
"""Get installed package version."""
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())
if match:
return match.group(2)
Expand Down Expand Up @@ -97,7 +97,7 @@ def install_many(self, names):
def get_installed_version(self, name):
"""Get installed package version."""
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())
if match:
return match.group(1)
Expand Down
35 changes: 17 additions & 18 deletions modoboa_installer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ def user_input(message):
return answer


def exec_cmd(cmd, sudo_user=None, pinput=None, login=True, **kwargs):
"""Execute a shell command.
def exec_cmd(cmd, sudo_user=None, login=True, **kwargs):
"""
Execute a shell command.
Run a command using the current user. Set :keyword:`sudo_user` if
you need different privileges.
:param str cmd: the command to execute
:param str sudo_user: a valid system username
:param str pinput: data to send to process's stdin
:rtype: tuple
:return: return code, command output
"""
Expand All @@ -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)
if "shell" not in kwargs:
kwargs["shell"] = True
if pinput is not None:
kwargs["stdin"] = subprocess.PIPE
capture_output = False
capture_output = True

Check warning on line 62 in modoboa_installer/utils.py

View check run for this annotation

Codecov / codecov/patch

modoboa_installer/utils.py#L62

Added line #L62 was not covered by tests
if "capture_output" in kwargs:
capture_output = kwargs.pop("capture_output")
elif not ENV.get("debug"):
capture_output = True
if capture_output:
kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = None
process = subprocess.Popen(cmd, **kwargs)
if pinput or capture_output:
c_args = [pinput] if pinput is not None else []
output = process.communicate(*c_args)[0]
else:
process.wait()
return process.returncode, output
kwargs.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
kwargs["universal_newlines"] = True
output: str = ""
with subprocess.Popen(cmd, **kwargs) as process:
if capture_output:
for line in process.stdout:
output += line
if ENV.get("debug"):
sys.stdout.write(line)

Check warning on line 74 in modoboa_installer/utils.py

View check run for this annotation

Codecov / codecov/patch

modoboa_installer/utils.py#L66-L74

Added lines #L66 - L74 were not covered by tests

return process.returncode, output.encode()

Check warning on line 76 in modoboa_installer/utils.py

View check run for this annotation

Codecov / codecov/patch

modoboa_installer/utils.py#L76

Added line #L76 was not covered by tests


def dist_info():
Expand Down Expand Up @@ -135,7 +135,6 @@ def settings(**kwargs):


class ConfigFileTemplate(string.Template):

"""Custom class for configuration files."""

delimiter = "%"
Expand Down

0 comments on commit 23aabbf

Please sign in to comment.