cookiecutter-django/scripts/ruff_version.py
Bruno Alla 15cf2a64f5
Move template linting and formatting to ruff (#5613)
* Move template linting and formatting to ruff

The generated project already uses that, let's be consistent and use it everywhere

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove comments as they're wrongly placed

* Tweak multi-line comment

* Remove a couple of commented out Ruff rules

* Fix extend-exclude in Ruff config

* Adjust Ruff line length

* Run Ruff pre-commit hook

* Run Ruff with --unsafe-fixes

* Run Ruff with --add-noqa

* Run Ruff formatter

* Drop Python 2 in pre/post generation hooks

* Restore print statements in pre/post-generation hooks

* Restore print statements in scripts

* Indent toml with 2 spaces

* Exclude docs and revert most changes from Ruff

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix Ruff issue

* Disable PLR0133 in pre/post commit hooks

We seem to compare 2 constants but seem strings are in fact interpolated in Jinja.

https://docs.astral.sh/ruff/rules/comparison-of-constant/

* Migrate post-generation hook to pathlib

* Migrate post-generation hook to pathlib

* Fix typo in folder name

* Migrate test generation to pathlib

* Fix typo in folder name

* Format comment better

* Update pyproject.toml

* Disable TRY003

* Update Ruff version pre-commit config

* Apply suggestions from code review

* Remove env from tox envlist

* Align ruff in pre-commit config

* Update .pre-commit-config.yaml

* Bump Ruff version

* Bump ruff pre-commit version

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove isort tests as it's no longer used

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-08-29 09:12:25 +01:00

60 lines
2.0 KiB
Python

from __future__ import annotations
import subprocess
from pathlib import Path
import tomllib
ROOT = Path(__file__).parent.parent
TEMPLATED_ROOT = ROOT / "{{cookiecutter.project_slug}}"
REQUIREMENTS_LOCAL_TXT = TEMPLATED_ROOT / "requirements" / "local.txt"
TEMPLATE_PRE_COMMIT_CONFIG = ROOT / ".pre-commit-config.yaml"
PRE_COMMIT_CONFIG = TEMPLATED_ROOT / ".pre-commit-config.yaml"
PYPROJECT_TOML = ROOT / "pyproject.toml"
def main() -> None:
new_version = get_requirements_txt_version()
old_version = get_pyproject_toml_version()
if old_version == new_version:
return
update_ruff_version(old_version, new_version)
subprocess.run(["uv", "lock", "--no-upgrade"], cwd=ROOT, check=False) # noqa: S607
def get_requirements_txt_version() -> str:
content = REQUIREMENTS_LOCAL_TXT.read_text()
for line in content.split("\n"):
if line.startswith("ruff"):
return line.split(" ")[0].split("==")[1]
raise RuntimeError("Could not find ruff version in requirements/local.txt")
def get_pyproject_toml_version() -> str:
data = tomllib.loads(PYPROJECT_TOML.read_text())
for dependency in data["project"]["dependencies"]:
if dependency.startswith("ruff=="):
return dependency.split("==")[1]
raise RuntimeError("Could not find ruff version in pyproject.toml")
def update_ruff_version(old_version: str, new_version: str) -> None:
# Update pyproject.toml
new_content = PYPROJECT_TOML.read_text().replace(
f"ruff=={old_version}",
f"ruff=={new_version}",
)
PYPROJECT_TOML.write_text(new_content)
# Update pre-commit configs
for config_file in [PRE_COMMIT_CONFIG, TEMPLATE_PRE_COMMIT_CONFIG]:
new_content = config_file.read_text().replace(
f"repo: https://github.com/astral-sh/ruff-pre-commit\n rev: v{old_version}",
f"repo: https://github.com/astral-sh/ruff-pre-commit\n rev: v{new_version}",
)
config_file.write_text(new_content)
if __name__ == "__main__":
main()