Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #61 from ermakov-oleg/exit-code
Browse files Browse the repository at this point in the history
feat: output with a non-zero output code in case of a dependency resolution error
  • Loading branch information
MousaZeidBaker authored Sep 13, 2022
2 parents b941781 + 684b0c0 commit 564b9fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
37 changes: 37 additions & 0 deletions src/poetryup/core/cmd.py
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 10 additions & 38 deletions src/poetryup/core/pyproject.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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],
Expand All @@ -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}'")
24 changes: 14 additions & 10 deletions src/poetryup/main.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 564b9fd

Please sign in to comment.