diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 41107d6..f4edf80 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -42,7 +42,7 @@ jobs: run: | source $(poetry env info --path)/bin/activate # activate virtual environment pytest tests - poetryup + poetryup -vv - name: Validate Project Version run: | diff --git a/pyproject.toml b/pyproject.toml index be1e923..d34e142 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "poetryup" -version = "0.10.0" +version = "0.11.0" description = "Update dependencies and bump their version in the pyproject.toml file" authors = ["Mousa Zeid Baker"] packages = [ diff --git a/src/poetryup/core/cmd.py b/src/poetryup/core/cmd.py new file mode 100644 index 0000000..2542020 --- /dev/null +++ b/src/poetryup/core/cmd.py @@ -0,0 +1,37 @@ +import logging +import subprocess +from typing import List + + +class CommandError(Exception): + def __init__(self, cmd: str, return_code: int) -> None: + self.cmd = cmd + self.return_code = return_code + + +def cmd_run(cmd: List) -> str: + """Run command with subprocess + + Args: + cmd: The command to run + + Returns: + The output from the command + + Raises: + CommandError when command exists with non-zero exit code + """ + + logging.debug(f"Run command: '{' '.join(cmd)}'") + process = subprocess.run( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + if process.returncode != 0: + logging.debug( + f"Command '{' '.join(cmd)}' exited with non-zero" + f"exit code '{process.return_code}'" + ) + raise CommandError(cmd="".join(cmd), return_code=process.returncode) + return process.stdout.decode() diff --git a/src/poetryup/core/pyproject.py b/src/poetryup/core/pyproject.py index 4590c61..19ebc5a 100644 --- a/src/poetryup/core/pyproject.py +++ b/src/poetryup/core/pyproject.py @@ -1,12 +1,11 @@ import logging import re -import subprocess -import sys from typing import Dict, List, Optional, Union import tomlkit from packaging import version as version_ +from poetryup.core.cmd import cmd_run from poetryup.models.dependency import Constraint, Dependency @@ -327,24 +326,10 @@ def __get_poetry_version() -> str: Returns: The poetry version installed """ - logging.debug("Execute: 'poetry --version'") - if sys.version_info < (3, 7): - return subprocess.check_output( - ["poetry", "--version"], - encoding="UTF-8", - ) - - return ( - subprocess.run( - ["poetry", "--version"], - capture_output=True, - ) - .stdout.decode() # command returns: 'Poetry version x.y.z' - .rsplit(" ", 1) - .pop() - .strip() - ) + output = cmd_run(["poetry", "--version"]) + # output is: 'Poetry version x.y.z' + return output.rsplit(" ", 1).pop().strip() @staticmethod def __run_poetry_show() -> str: @@ -353,26 +338,16 @@ def __run_poetry_show() -> str: Returns: The output from the poetry show command """ - logging.debug("Execute: 'poetry show --tree'") - if sys.version_info < (3, 7): - return subprocess.check_output( - ["poetry", "show", "--tree"], - encoding="UTF-8", - ) - - return subprocess.run( - ["poetry", "show", "--tree"], - capture_output=True, - ).stdout.decode() + return cmd_run(["poetry", "show", "--tree"]) @staticmethod def __run_poetry_update() -> None: """Run poetry update command""" - logging.debug("Execute: 'poetry update'") - subprocess.run(["poetry", "update"]) + cmd_run(["poetry", "update"]) + @staticmethod def __run_poetry_add( self, packages: List[str], @@ -386,13 +361,10 @@ def __run_poetry_add( """ if group is None or group == "default": - logging.debug(f"Execute: 'poetry add {packages}'") - subprocess.run(["poetry", "add", *packages]) + cmd_run(["poetry", "add", *packages]) elif group == "dev" and self.poetry_version < version_.parse("1.2.0"): - logging.debug(f"Execute: 'poetry add {packages} --{group}'") - subprocess.run(["poetry", "add", *packages, f"--{group}"]) + cmd_run(["poetry", "add", *packages, f"--{group}"]) elif self.poetry_version >= version_.parse("1.2.0"): - logging.debug(f"Execute: 'poetry add {packages} --group {group}'") - subprocess.run(["poetry", "add", *packages, f"--group {group}"]) + cmd_run(["poetry", "add", *packages, f"--group {group}"]) else: logging.warning(f"Couldn't add package(s) '{packages}'") diff --git a/src/poetryup/main.py b/src/poetryup/main.py index fc6efc3..f21450b 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -1,12 +1,12 @@ #!/usr/bin/env python import logging -import subprocess from pathlib import Path from typing import List import typer +from poetryup.core.cmd import CommandError, cmd_run from poetryup.core.pyproject import Pyproject from poetryup.models.dependency import Constraint @@ -64,17 +64,21 @@ def poetryup( pyproject = Pyproject(pyproject_str) without_constraint = [Constraint.EXACT] if skip_exact else [] - pyproject.update_dependencies( - latest, - without_constraint, - name, - exclude_name, - group, - ) + + try: + pyproject.update_dependencies( + latest, + without_constraint, + name, + exclude_name, + group, + ) + except CommandError as e: + raise typer.Exit(e.return_code) + Path("pyproject.toml").write_text(pyproject.dumps()) # refresh the lock file after changes in pyproject.toml - logging.debug("Execute: 'poetry lock --no-update'") - subprocess.run(["poetry", "lock", "--no-update"]) + cmd_run(["poetry", "lock", "--no-update"]) if __name__ == "__main__":