Skip to content

Commit

Permalink
Ensure we display all information about failed processes (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 9, 2025
1 parent bf5452e commit 2c8e60d
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/ansible_dev_environment/subcommands/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
from ansible_dev_environment.output import Output


def format_process(exc: subprocess.CalledProcessError) -> str:
"""Format the subprocess exception.
Args:
exc: The exception.
Returns:
The formatted exception.
"""
result = f"Got {exc.returncode} return code from: {exc.cmd}\n"
if exc.stdout: # pragma: no cover
result += f"stdout:\n{exc.stdout}"
if exc.stderr:
result += f"stderr:\n{exc.stderr}"
return result


class Installer:
"""The installer class.
Expand Down Expand Up @@ -116,7 +133,7 @@ def _install_core(self) -> None:
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to install ansible-core: {exc}"
err = f"Failed to install ansible-core: {format_process(exc)}"
self._output.critical(err)

def _install_dev_tools(self) -> None:
Expand All @@ -138,7 +155,7 @@ def _install_dev_tools(self) -> None:
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to install ansible-dev-tools: {exc}"
err = f"Failed to install ansible-dev-tools: {format_process(exc)}"
self._output.critical(err)

def _install_galaxy_collections(
Expand Down Expand Up @@ -188,7 +205,7 @@ def _install_galaxy_collections(
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to install collection: {exc}\n{exc.stderr}"
err = f"Failed to install collection: {format_process(exc)}"
self._output.critical(err)
raise SystemError(err) from exc # pragma: no cover # critical exits
installed = self.RE_GALAXY_INSTALLED.findall(proc.stdout)
Expand Down Expand Up @@ -231,7 +248,7 @@ def _install_galaxy_requirements(self) -> None:
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to install collections: {exc} {exc.stderr}"
err = f"Failed to install collections: {format_process(exc)}"
self._output.critical(err)

installed = self.RE_GALAXY_INSTALLED.findall(proc.stdout)
Expand Down Expand Up @@ -267,7 +284,7 @@ def _find_files_using_git_ls_files(
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to list collection using git ls-files: {exc} {exc.stderr}"
err = f"Failed to list collection using git ls-files: {format_process(exc)}"
self._output.info(err)
return None, None

Expand Down Expand Up @@ -299,7 +316,7 @@ def _find_files_using_ls(
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to list collection using ls: {exc} {exc.stderr}"
err = f"Failed to list collection using ls: {format_process(exc)}"
self._output.debug(err)
return None, None

Expand Down Expand Up @@ -401,7 +418,7 @@ def _install_local_collection(
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to build collection: {exc} {exc.stderr}"
err = f"Failed to build collection: {format_process(exc)}"
self._output.critical(err)

built = [
Expand Down Expand Up @@ -456,7 +473,7 @@ def _install_local_collection(
output=self._output,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to install collection: {exc} {exc.stderr}"
err = f"Failed to install collection: {format_process(exc)}"
self._output.critical(err)
raise SystemError(err) from exc # pragma: no cover # critical exits

Expand Down Expand Up @@ -505,7 +522,7 @@ def _pip_install(self) -> None:
msg = "Installing python requirements."
self._output.info(msg)

command = f"{self._config.pip_cmd} install" f" -r {self._config.discovered_python_reqs}"
command = f"{self._config.pip_cmd} install -r {self._config.discovered_python_reqs}"

msg = f"Installing python requirements from {self._config.discovered_python_reqs}"
self._output.debug(msg)
Expand All @@ -520,7 +537,7 @@ def _pip_install(self) -> None:
except subprocess.CalledProcessError as exc:
err = (
"Failed to install requirements from"
f" {self._config.discovered_python_reqs}: {exc} {exc.stderr}"
f" {self._config.discovered_python_reqs}: {format_process(exc)}"
)
self._output.critical(err)
else:
Expand Down

0 comments on commit 2c8e60d

Please sign in to comment.