Skip to content

Commit

Permalink
Support custom groups (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apakottur authored Oct 17, 2024
1 parent d65027a commit 6037a58
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 86 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
# GENERAL #
###########
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-added-large-files
args: ["--maxkb=2000"]
Expand Down Expand Up @@ -53,13 +53,13 @@ repos:
- id: yamllint
args: ['--config-file', './linters/yamllint.yaml']
- repo: https://github.com/biomejs/pre-commit
rev: v0.4.0
rev: v0.5.0
hooks:
- id: biome-check
args: [--config-path, ./linters]
additional_dependencies: ["@biomejs/[email protected].0"]
additional_dependencies: ["@biomejs/[email protected].3"]
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v8.13.1
rev: v8.15.1
hooks:
- id: cspell
exclude: \.(csv|json|txt|lock)$
Expand All @@ -77,7 +77,7 @@ repos:
- id: python-no-log-warn
- id: python-use-type-annotations
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.6
rev: v0.6.9
hooks:
- id: ruff
args: [--config, ./linters/ruff.toml]
Expand All @@ -93,12 +93,12 @@ repos:
- flake8-picky-parentheses==0.5.5
- flake8-pyi==24.6.0
- pep8-naming==0.14.1
- pydoclint==0.5.7
- pydoclint==0.5.9
args: [--config, ./linters/.flake8]
types: [file]
types_or: [python, pyi]
- repo: https://github.com/jendrikseipp/vulture
rev: v2.11
rev: v2.13
hooks:
- id: vulture
args: ["src", "tests"]
1 change: 1 addition & 0 deletions linters/cspell/words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pydantic
ignorelist
docstrings
biomejs
sqla
16 changes: 8 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = []

[tool.poetry.dependencies]
python = "~3.12"
shpyx = "0.0.29"
shpyx = "0.0.30"
tomlkit = "0.13.2"
typer = "0.12.5"

Expand Down
63 changes: 38 additions & 25 deletions src/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
POETRY_CONFIG_FILE_NAME = "pyproject.toml"

"""Sections in the Poetry configuration files where dependencies reside"""
SECTIONS = ("dependencies", "dev-dependencies", "group.dev.dependencies", "")
SECTIONS = ("dependencies", "dev-dependencies")


def _run_updater_in_path(path: str) -> None:
Expand Down Expand Up @@ -40,15 +40,18 @@ def _run_updater_in_path(path: str) -> None:

# Build the mapping from projects to their path dependencies.
file_path_to_deps[file_path.resolve()] = []
for section in SECTIONS:
try:
current_poetry_section = poetry_section
for part in section.split("."):
current_poetry_section = current_poetry_section[part]
except tomlkit.exceptions.NonExistentKey:
pairs_to_check = list(poetry_section.items())
while pairs_to_check:
current_pair = pairs_to_check.pop()
if current_pair[0] in SECTIONS:
current_section = current_pair[1]
elif isinstance(current_pair[1], dict):
pairs_to_check.extend(current_pair[1].items())
continue
else:
continue

for details in current_poetry_section.values():
for details in current_section.values():
if "path" in details:
file_path_to_deps[file_path.resolve()].append((Path(root) / details["path"] / name).resolve())

Expand All @@ -75,8 +78,13 @@ def cmp(x: Path, y: Path) -> int:

print(f"TOML contents of {file_path}: {parsed_contents}")

# Construct the poetry command to get outdated packages.
poetry_cmd = "poetry show -o --no-ansi"
if group_section := parsed_contents["tool"]["poetry"].get("group"):
poetry_cmd += f" --with {','.join(group_section.keys())}"

# Get all the outdated packages.
results = shpyx.run("poetry show -o --no-ansi", exec_dir=file_path.parent)
results = shpyx.run(poetry_cmd, exec_dir=file_path.parent)

if not results.stdout:
# Nothing to update.
Expand All @@ -91,27 +99,32 @@ def cmp(x: Path, y: Path) -> int:
package_name, installed_version, new_version = formatted_result.split()[:3]

# Update the package version in the file.
for section in SECTIONS:
try:
current_poetry_section = poetry_section
for part in section.split("."):
current_poetry_section = current_poetry_section[part]
pairs_to_check = list(poetry_section.items())
while pairs_to_check:
current_pair = pairs_to_check.pop()
if current_pair[0] in SECTIONS:
current_section = current_pair[1]
elif isinstance(current_pair[1], dict):
pairs_to_check.extend(current_pair[1].items())
continue
else:
continue

try:
original_package_name, package_details = next(
(name, details)
for name, details in current_poetry_section.items()
for name, details in current_section.items()
if name.lower() == package_name.lower()
)

if isinstance(package_details, str):
written_version = package_details
else:
written_version = package_details["version"]

except (StopIteration, tomlkit.exceptions.NonExistentKey):
# Either the section is missing or the package is not in this section.
except StopIteration:
# The package is not in this section.
continue

if isinstance(package_details, str): # noqa: SIM108
written_version = package_details
else:
written_version = package_details["version"]

# Skip packages that are locked via the '==' operator.
if written_version.startswith("=="):
print("Skipping locked package:", package_name)
Expand All @@ -121,9 +134,9 @@ def cmp(x: Path, y: Path) -> int:

# Replace the old version of the package with the new one.
if isinstance(package_details, str):
current_poetry_section[original_package_name] = new_version
current_section[original_package_name] = new_version
else:
current_poetry_section[original_package_name]["version"] = new_version
current_section[original_package_name]["version"] = new_version

# Write the updated configuration file.
Path(file_path).write_text(parsed_contents.as_string())
Expand Down
Loading

0 comments on commit 6037a58

Please sign in to comment.