mirror of
https://github.com/ansible/ansible-documentation.git
synced 2026-03-27 13:28:51 +07:00
This enforce the Python linters in the noxfile on the Python files in
docs/bin.
(cherry picked from commit 320d1bed6f)
102 lines
2.6 KiB
Python
102 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from glob import iglob
|
|
from pathlib import Path
|
|
|
|
import nox
|
|
|
|
LINT_FILES: tuple[str, ...] = (
|
|
"hacking/pr_labeler/label.py",
|
|
"noxfile.py",
|
|
*iglob("docs/bin/*.py"),
|
|
)
|
|
PINNED = os.environ.get("PINNED", "true").lower() in {"1", "true"}
|
|
nox.options.sessions = ("clone-core", "lint")
|
|
|
|
|
|
def install(session: nox.Session, *args, req: str, **kwargs):
|
|
if PINNED:
|
|
kwargs.setdefault("env", {})["PIP_CONSTRAINT"] = f"tests/{req}.txt"
|
|
session.install("-r", f"tests/{req}.in", *args, **kwargs)
|
|
|
|
|
|
@nox.session
|
|
def static(session: nox.Session):
|
|
"""
|
|
Run static checkers
|
|
"""
|
|
install(session, req="static")
|
|
session.run("ruff", *session.posargs, *LINT_FILES)
|
|
|
|
|
|
@nox.session
|
|
def formatters(session: nox.Session):
|
|
"""
|
|
Reformat code
|
|
"""
|
|
install(session, req="formatters")
|
|
session.run("isort", *session.posargs, *LINT_FILES)
|
|
session.run("black", *session.posargs, *LINT_FILES)
|
|
|
|
|
|
@nox.session
|
|
def formatters_check(session: nox.Session):
|
|
"""
|
|
Check code formatting without making changes
|
|
"""
|
|
install(session, req="formatters")
|
|
session.run("isort", "--check", *session.posargs, *LINT_FILES)
|
|
session.run("black", "--check", *session.posargs, *LINT_FILES)
|
|
|
|
|
|
@nox.session
|
|
def typing(session: nox.Session):
|
|
install(session, req="typing")
|
|
session.run("mypy", *session.posargs, *LINT_FILES)
|
|
|
|
|
|
@nox.session
|
|
def lint(session: nox.Session):
|
|
session.notify("typing")
|
|
session.notify("static")
|
|
session.notify("formatters")
|
|
|
|
|
|
requirements_files = list(
|
|
{path.name.replace(".in", "") for path in Path("tests").glob("*in")}
|
|
- {"constraints", "constraints-base"}
|
|
)
|
|
|
|
|
|
@nox.session(name="pip-compile", python=["3.10"])
|
|
@nox.parametrize(["req"], requirements_files, requirements_files)
|
|
def pip_compile(session: nox.Session, req: str):
|
|
# .pip-tools.toml was introduced in v7
|
|
session.install("pip-tools >= 7")
|
|
|
|
# Use --upgrade by default unless a user passes -P.
|
|
args = list(session.posargs)
|
|
if not any(
|
|
arg.startswith("-P") or arg.startswith("--upgrade-package") for arg in args
|
|
):
|
|
args.append("--upgrade")
|
|
|
|
# fmt: off
|
|
session.run(
|
|
"pip-compile",
|
|
"--output-file", f"tests/{req}.txt",
|
|
*args,
|
|
f"tests/{req}.in",
|
|
)
|
|
# fmt: on
|
|
|
|
|
|
@nox.session(name="clone-core", venv_backend="none")
|
|
def clone_core(session: nox.Session):
|
|
"""
|
|
Clone relevant portions of ansible-core from ansible/ansible into the current
|
|
source tree to facilitate building docs.
|
|
"""
|
|
session.run_always("python", "docs/bin/clone-core.py")
|