From 166ebdb64a3cff9afe0e64dd6aa8e2123a231290 Mon Sep 17 00:00:00 2001 From: "o.ermakov" Date: Fri, 12 Aug 2022 11:06:27 +0300 Subject: [PATCH 1/7] feat: output with a non-zero output code in case of a dependency resolution error --- src/poetryup/core/pyproject.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/poetryup/core/pyproject.py b/src/poetryup/core/pyproject.py index ebc390d..a14752b 100644 --- a/src/poetryup/core/pyproject.py +++ b/src/poetryup/core/pyproject.py @@ -6,6 +6,7 @@ import tomlkit from packaging import version as version_ +from typer import Exit from poetryup.models.dependency import Constraint, Dependency @@ -355,12 +356,11 @@ def __run_poetry_show() -> str: capture_output=True, ).stdout.decode() - @staticmethod - def __run_poetry_update() -> None: + def __run_poetry_update(self) -> None: """Run poetry update command""" logging.debug("Execute: 'poetry update'") - subprocess.run(["poetry", "update"]) + self.__cmd_run(["poetry", "update"]) def __run_poetry_add( self, @@ -376,12 +376,17 @@ def __run_poetry_add( if group is None or group == "default": logging.debug(f"Execute: 'poetry add {packages}'") - subprocess.run(["poetry", "add", *packages]) + self.__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}"]) + self.__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}"]) + self.__cmd_run(["poetry", "add", *packages, f"--group {group}"]) else: logging.warning(f"Couldn't add package(s) '{packages}'") + + def __cmd_run(self, cmd: List[str]) -> None: + proc = subprocess.run(cmd, check=False) + if proc.returncode != 0: + raise Exit(proc.returncode) From 01cfdadd115e46d86f1ad44f10592c669741d8ea Mon Sep 17 00:00:00 2001 From: "o.ermakov" Date: Mon, 15 Aug 2022 11:20:21 +0300 Subject: [PATCH 2/7] Fix: abstraction leak --- src/poetryup/core/pyproject.py | 9 +++++++-- src/poetryup/main.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/poetryup/core/pyproject.py b/src/poetryup/core/pyproject.py index a14752b..40094b3 100644 --- a/src/poetryup/core/pyproject.py +++ b/src/poetryup/core/pyproject.py @@ -6,11 +6,16 @@ import tomlkit from packaging import version as version_ -from typer import Exit from poetryup.models.dependency import Constraint, Dependency +class PoetryError(Exception): + def __init__(self, cmd, return_code) -> None: + self.cmd = cmd + self.return_code = return_code + + class Pyproject: """A class to represent a pyproject.toml configuration file. @@ -389,4 +394,4 @@ def __run_poetry_add( def __cmd_run(self, cmd: List[str]) -> None: proc = subprocess.run(cmd, check=False) if proc.returncode != 0: - raise Exit(proc.returncode) + raise PoetryError(cmd=" ".join(cmd), return_code=proc.returncode) diff --git a/src/poetryup/main.py b/src/poetryup/main.py index 632853c..015c0a6 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -7,7 +7,7 @@ import typer -from poetryup.core.pyproject import Pyproject +from poetryup.core.pyproject import PoetryError, Pyproject from poetryup.models.dependency import Constraint app = typer.Typer(add_completion=False) @@ -60,7 +60,13 @@ def poetryup( pyproject = Pyproject(pyproject_str) without_constraint = [Constraint.EXACT] if skip_exact else [] - pyproject.update_dependencies(latest, without_constraint, name, group) + try: + pyproject.update_dependencies(latest, without_constraint, name, group) + except PoetryError as e: + logging.debug( + 'Execute "%s" failed with exit-code: %s', e.cmd, e.return_code + ) + 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'") From 38100cefb96a548b27d1fa3c5341452a9c736898 Mon Sep 17 00:00:00 2001 From: "o.ermakov" Date: Tue, 16 Aug 2022 13:47:06 +0300 Subject: [PATCH 3/7] bump project version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = [ From be4a924b9ffd02d80bc1b9faf47502786b1f3ed3 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Sun, 11 Sep 2022 17:50:04 +0200 Subject: [PATCH 4/7] refactor: command executions --- src/poetryup/core/cmd.py | 33 ++++++++++++++++++ src/poetryup/core/pyproject.py | 62 +++++++--------------------------- src/poetryup/main.py | 9 +++-- 3 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 src/poetryup/core/cmd.py diff --git a/src/poetryup/core/cmd.py b/src/poetryup/core/cmd.py new file mode 100644 index 0000000..2f7b91f --- /dev/null +++ b/src/poetryup/core/cmd.py @@ -0,0 +1,33 @@ +import logging +import subprocess + + +class CommandError(Exception): + def __init__(self, cmd: str, return_code: int) -> None: + self.cmd = cmd + self.return_code = return_code + + +def cmd_run(*cmd) -> str: + """Run command with subprocess + + Args: + cmd: The command to run + + Returns: + The output from the command + + Raises: + CommandError when command return code isn't 0 + """ + + logging.debug(f"Run command: '{cmd}'") + process = subprocess.run( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + ) + if process.returncode != 0: + raise CommandError(cmd="".join(cmd), return_code=process.returncode) + return process.stdout diff --git a/src/poetryup/core/pyproject.py b/src/poetryup/core/pyproject.py index 135d67f..19ebc5a 100644 --- a/src/poetryup/core/pyproject.py +++ b/src/poetryup/core/pyproject.py @@ -1,21 +1,14 @@ 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 -class PoetryError(Exception): - def __init__(self, cmd, return_code) -> None: - self.cmd = cmd - self.return_code = return_code - - class Pyproject: """A class to represent a pyproject.toml configuration file. @@ -333,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: @@ -359,25 +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"]) - def __run_poetry_update(self) -> None: + @staticmethod + def __run_poetry_update() -> None: """Run poetry update command""" - logging.debug("Execute: 'poetry update'") - self.__cmd_run(["poetry", "update"]) + cmd_run(["poetry", "update"]) + @staticmethod def __run_poetry_add( self, packages: List[str], @@ -391,18 +361,10 @@ def __run_poetry_add( """ if group is None or group == "default": - logging.debug(f"Execute: 'poetry add {packages}'") - self.__cmd_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}'") - self.__cmd_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}'") - self.__cmd_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}'") - - def __cmd_run(self, cmd: List[str]) -> None: - proc = subprocess.run(cmd, check=False) - if proc.returncode != 0: - raise PoetryError(cmd=" ".join(cmd), return_code=proc.returncode) diff --git a/src/poetryup/main.py b/src/poetryup/main.py index ed876b7..227a99b 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -7,7 +7,8 @@ import typer -from poetryup.core.pyproject import PoetryError, Pyproject +from poetryup.core.cmd import CommandError +from poetryup.core.pyproject import Pyproject from poetryup.models.dependency import Constraint app = typer.Typer(add_completion=False) @@ -64,6 +65,7 @@ def poetryup( pyproject = Pyproject(pyproject_str) without_constraint = [Constraint.EXACT] if skip_exact else [] + try: pyproject.update_dependencies( latest, @@ -72,11 +74,12 @@ def poetryup( exclude_name, group, ) - except PoetryError as e: + except CommandError as e: logging.debug( - 'Execute "%s" failed with exit-code: %s', e.cmd, e.return_code + f"Command '{e.cmd}' failed with exit code '{e.return_code}'" ) 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'") From 7750358cff9cd217b10d32ea985d095c6192efb7 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Sun, 11 Sep 2022 18:34:08 +0200 Subject: [PATCH 5/7] fix: cmd_run method --- src/poetryup/core/cmd.py | 14 +++++++++----- src/poetryup/main.py | 9 ++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/poetryup/core/cmd.py b/src/poetryup/core/cmd.py index 2f7b91f..a2b45bc 100644 --- a/src/poetryup/core/cmd.py +++ b/src/poetryup/core/cmd.py @@ -1,5 +1,6 @@ import logging import subprocess +from typing import List class CommandError(Exception): @@ -8,7 +9,7 @@ def __init__(self, cmd: str, return_code: int) -> None: self.return_code = return_code -def cmd_run(*cmd) -> str: +def cmd_run(cmd: List) -> str: """Run command with subprocess Args: @@ -18,16 +19,19 @@ def cmd_run(*cmd) -> str: The output from the command Raises: - CommandError when command return code isn't 0 + CommandError when command exists with non-zer exit code """ - logging.debug(f"Run command: '{cmd}'") + logging.debug(f"Run command: '{' '.join(cmd)}'") process = subprocess.run( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - text=True, ) 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 + return process.stdout.decode() diff --git a/src/poetryup/main.py b/src/poetryup/main.py index 227a99b..f21450b 100644 --- a/src/poetryup/main.py +++ b/src/poetryup/main.py @@ -1,13 +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 +from poetryup.core.cmd import CommandError, cmd_run from poetryup.core.pyproject import Pyproject from poetryup.models.dependency import Constraint @@ -75,15 +74,11 @@ def poetryup( group, ) except CommandError as e: - logging.debug( - f"Command '{e.cmd}' failed with exit code '{e.return_code}'" - ) 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__": From 0ba1cc69c6e57fc21b4c8e1bd39dc4d03f033027 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Sun, 11 Sep 2022 18:37:57 +0200 Subject: [PATCH 6/7] fix: verbosity in test pipeline --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: | From 684b0c09ec0c4dcf564048fdf186d557572ec8c3 Mon Sep 17 00:00:00 2001 From: Mousa Zeid Baker Date: Mon, 12 Sep 2022 00:20:30 +0200 Subject: [PATCH 7/7] fix: typo --- src/poetryup/core/cmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poetryup/core/cmd.py b/src/poetryup/core/cmd.py index a2b45bc..2542020 100644 --- a/src/poetryup/core/cmd.py +++ b/src/poetryup/core/cmd.py @@ -19,7 +19,7 @@ def cmd_run(cmd: List) -> str: The output from the command Raises: - CommandError when command exists with non-zer exit code + CommandError when command exists with non-zero exit code """ logging.debug(f"Run command: '{' '.join(cmd)}'")