Use PDM & Ruff instead of Poetry+Black+Flake8+isort

This commit is contained in:
Justin Mayer 2024-07-03 22:45:20 +02:00
commit d7c2f2bf1f
6 changed files with 168 additions and 156 deletions

View file

@ -1,14 +1,15 @@
from inspect import cleandoc
import logging
import os
from pathlib import Path
from shutil import which
import sys
from invoke import task
logger = logging.getLogger(__name__)
PKG_NAME = "share_post"
PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}")
TOOLS = ("poetry", "pre-commit")
ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs"))
@ -16,52 +17,50 @@ VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME.expanduser() / PKG_
VENV = str(VENV_PATH.expanduser())
BIN_DIR = "bin" if os.name != "nt" else "Scripts"
VENV_BIN = Path(VENV) / Path(BIN_DIR)
POETRY = which("poetry") if which("poetry") else (VENV_BIN / "poetry")
CMD_PREFIX = f"{VENV_BIN}/" if ACTIVE_VENV else f"{POETRY} run "
TOOLS = ("pdm", "pre-commit")
PDM = which("pdm") if which("pdm") else (VENV_BIN / "pdm")
CMD_PREFIX = f"{VENV_BIN}/" if ACTIVE_VENV else f"{PDM} run "
PRECOMMIT = which("pre-commit") if which("pre-commit") else f"{CMD_PREFIX}pre-commit"
PTY = True if os.name != "nt" else False
PTY = os.name != "nt"
@task
def tests(c):
"""Run the test suite."""
c.run(f"{CMD_PREFIX}pytest", pty=PTY)
def tests(c, deprecations=False):
"""Run the test suite, optionally with `--deprecations`."""
deprecations_flag = "" if deprecations else "-W ignore::DeprecationWarning"
c.run(f"{CMD_PREFIX}pytest {deprecations_flag}", pty=PTY)
@task
def black(c, check=False, diff=False):
"""Run Black auto-formatter, optionally with `--check` or `--diff`."""
def format(c, check=False, diff=False):
"""Run Ruff's auto-formatter, optionally with `--check` or `--diff`."""
check_flag, diff_flag = "", ""
if check:
check_flag = "--check"
if diff:
diff_flag = "--diff"
c.run(f"{CMD_PREFIX}black {check_flag} {diff_flag} {PKG_PATH} tasks.py")
c.run(
f"{CMD_PREFIX}ruff format {check_flag} {diff_flag} {PKG_PATH} tasks.py", pty=PTY
)
@task
def isort(c, check=False, diff=False):
"""Ensure imports are sorted according to project standards."""
check_flag, diff_flag = "", ""
if check:
check_flag = "-c"
def ruff(c, fix=False, diff=False):
"""Run Ruff to ensure code meets project standards."""
diff_flag, fix_flag = "", ""
if fix:
fix_flag = "--fix"
if diff:
diff_flag = "--diff"
c.run(f"{CMD_PREFIX}isort {check_flag} {diff_flag} .")
c.run(f"{CMD_PREFIX}ruff check {diff_flag} {fix_flag} .", pty=PTY)
@task
def flake8(c):
"""Check code for PEP8 compliance via Flake8."""
c.run(f"{CMD_PREFIX}flake8 {PKG_PATH} tasks.py")
@task
def lint(c, diff=False):
def lint(c, fix=False, diff=False):
"""Check code style via linting tools."""
isort(c, check=True, diff=diff)
black(c, check=True, diff=diff)
flake8(c)
ruff(c, fix=fix, diff=diff)
format(c, check=(not fix), diff=diff)
@task
@ -69,34 +68,34 @@ def tools(c):
"""Install development tools in the virtual environment if not already on PATH."""
for tool in TOOLS:
if not which(tool):
print(f"** Installing {tool}.")
logger.info(f"** Installing {tool} **")
c.run(f"{CMD_PREFIX}pip install {tool}")
@task
def precommit(c):
"""Install pre-commit hooks to `.git/hooks/pre-commit`."""
print("** Installing pre-commit hooks.")
"""Install pre-commit hooks to .git/hooks/pre-commit."""
logger.info("** Installing pre-commit hooks **")
c.run(f"{PRECOMMIT} install")
@task
def setup(c):
"""Set up the development environment."""
if which("poetry") or ACTIVE_VENV:
if which("pdm") or ACTIVE_VENV:
tools(c)
c.run(f"{CMD_PREFIX}python -m pip install --upgrade pip")
c.run(f"{POETRY} install")
c.run(f"{CMD_PREFIX}python -m pip install --upgrade pip", pty=PTY)
c.run(f"{PDM} update --dev", pty=PTY)
precommit(c)
print("\nDevelopment environment should now be set up and ready!\n")
logger.info("\nDevelopment environment should now be set up and ready!\n")
else:
error_message = """
Poetry is not installed, and there is no active virtual environment available.
PDM is not installed, and there is no active virtual environment available.
You can either manually create and activate a virtual environment, or you can
install Poetry via:
install PDM via:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python3 -
Once you have taken one of the above two steps, run `invoke setup` again.
""" # noqa: E501
sys.exit(cleandoc(error_message))
raise SystemExit(cleandoc(error_message))