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 #50 from MousaZeidBaker/feat/name-group-flags
Browse files Browse the repository at this point in the history
feat: add name & group flags to include specific dependencies
  • Loading branch information
MousaZeidBaker authored Jun 14, 2022
2 parents f27a777 + 4241efb commit 137b3d8
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 26 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,33 @@ is that the latter also modifies the `pyproject.toml` file.
![poetryup_demo](https://raw.githubusercontent.com/MousaZeidBaker/poetryup/master/media/poetryup_demo.gif)

## Usage

Show help message and exit
```shell
poetryup --help
```

Update all dependencies with respect to their version constraints specified in the
`pyproject.toml` file
```shell
poetryup
```

Update all dependencies to their latest available version
```shell
poetryup --latest
```

Update dependencies in the `default` and `dev` group to their latest available version
```shell
poetryup --latest --group defaut --group dev
```

Update the `foo` and `bar` dependencies to their latest available version
```shell
poetryup --latest --name foo --name bar
```

## Automate Dependency Updates with GitHub Actions
Use PoetryUp with GitHub actions to automate the process of updating
dependencies, for reference see this project's [workflow
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.7.2"
version = "0.8.0"
description = "Update dependencies and bump their version in the pyproject.toml file"
authors = ["Mousa Zeid Baker"]
packages = [
Expand Down
68 changes: 58 additions & 10 deletions src/poetryup/core/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,34 +201,76 @@ def search_dependency(
if dependency.name == name or dependency.normalized_name == name:
return dependency

def filter_dependencies(
self,
dependencies: List[Dependency],
without_constraints: List[Constraint] = [],
names: List[str] = [],
groups: List[str] = [],
) -> Union[Dependency, None]:
"""Search for a dependency by name given a list of dependencies
Args:
dependencies: A list of dependencies to filter
without_constraints: The dependency constraints to ignore
names: The dependency names to include
groups: The dependency groups to include
Returns:
A list of dependencies
"""

if without_constraints:
# remove deps whom constraint is in the provided constraint list
dependencies = [
x
for x in dependencies
if x.constraint not in without_constraints
]
if names:
# remove deps whom name is NOT in the provided name list
dependencies = [x for x in dependencies if x.name in names]
if groups:
# remove deps whom group is NOT in the provided group list
dependencies = [x for x in dependencies if x.group in groups]

return dependencies

def update_dependencies(
self,
latest: bool = False,
skip_exact: bool = False,
without_constraints: List[Constraint] = [],
names: List[str] = [],
groups: List[str] = [],
) -> None:
"""Update dependencies and bump their version in pyproject
Args:
latest: Whether to update dependencies to their latest version
skip_exact: Whether to skip dependencies with an exact version
without_constraints: The dependency constraints to ignore
names: The dependency names to include
groups: The dependency groups to include
"""

if latest:
logging.info("Updating dependencies to their latest version")
dependencies = self.filter_dependencies(
self.dependencies,
without_constraints,
names,
groups,
)
# sort dependencies into their groups and add them at once in order
# to avoid version solver error in case dependencies depend on each
# other
groups = {}
for dependency in self.dependencies:
if skip_exact and dependency.constraint == Constraint.EXACT:
# skip dependencies with an exact version
continue
dependency_groups = {}
for dependency in dependencies:
if isinstance(dependency.version, str):
groups[dependency.group] = groups.get(
dependency_groups[dependency.group] = dependency_groups.get(
dependency.group, []
) + [f"{dependency.name}@latest"]

for group, packages in groups.items():
for group, packages in dependency_groups.items():
self.__run_poetry_add(
packages=packages,
group=group,
Expand All @@ -238,8 +280,14 @@ def update_dependencies(
self.__run_poetry_update()

# bump versions in pyproject
bumped_dependencies = self.filter_dependencies(
self.bumped_dependencies,
without_constraints,
names,
groups,
)
table = self.pyproject["tool"]["poetry"]
for dependency in self.bumped_dependencies:
for dependency in bumped_dependencies:
if dependency.group == "default":
table["dependencies"][dependency.name] = dependency.version
elif (
Expand Down
13 changes: 12 additions & 1 deletion src/poetryup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import os
import subprocess
from pathlib import Path
from typing import List

import typer

from poetryup.core.pyproject import Pyproject
from poetryup.models.dependency import Constraint

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO").upper())

Expand All @@ -24,6 +26,14 @@ def poetryup(
default=False,
help="Whether to skip dependencies with an exact version.",
),
name: List[str] = typer.Option(
default=[],
help="The dependency names to include.",
),
group: List[str] = typer.Option(
default=[],
help="The dependency groups to include.",
),
):
"""Update dependencies and bump their version in pyproject.toml file"""

Expand All @@ -35,7 +45,8 @@ def poetryup(
)

pyproject = Pyproject(pyproject_str)
pyproject.update_dependencies(latest, skip_exact)
without_constraint = [Constraint.EXACT] if skip_exact else []
pyproject.update_dependencies(latest, without_constraint, name, group)
Path("pyproject.toml").write_text(pyproject.dumps())
# refresh the lock file after changes in pyproject.toml
subprocess.run(["poetry", "lock", "--no-update"])
Expand Down
Loading

0 comments on commit 137b3d8

Please sign in to comment.