2019-11-09 08:36:45 -08:00
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from shutil import which
|
|
|
|
|
|
|
|
|
|
from invoke import task
|
|
|
|
|
|
|
|
|
|
PKG_NAME = "pelican"
|
2021-02-18 12:29:18 +01:00
|
|
|
PKG_PATH = Path(PKG_NAME)
|
2019-11-12 08:36:22 -08:00
|
|
|
DOCS_PORT = os.environ.get("DOCS_PORT", 8000)
|
2020-04-24 15:21:05 +02:00
|
|
|
BIN_DIR = "bin" if os.name != "nt" else "Scripts"
|
2023-10-28 21:06:24 +01:00
|
|
|
PTY = os.name != "nt"
|
2019-11-09 08:36:45 -08:00
|
|
|
ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
|
|
|
|
|
VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/virtualenvs"))
|
|
|
|
|
VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME / PKG_NAME)
|
|
|
|
|
VENV = str(VENV_PATH.expanduser())
|
2020-04-24 15:21:05 +02:00
|
|
|
VENV_BIN = Path(VENV) / Path(BIN_DIR)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
2023-10-28 17:43:16 +02:00
|
|
|
TOOLS = ["pdm", "pre-commit", "psutil"]
|
|
|
|
|
PDM = which("pdm") or VENV_BIN / "pdm"
|
2023-10-28 21:06:24 +01:00
|
|
|
PRECOMMIT = which("pre-commit") or VENV_BIN / "pre-commit"
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
2019-11-12 08:36:22 -08:00
|
|
|
@task
|
|
|
|
|
def docbuild(c):
|
|
|
|
|
"""Build documentation"""
|
2021-10-08 08:59:35 +02:00
|
|
|
c.run(f"{VENV_BIN}/sphinx-build -W docs docs/_build", pty=PTY)
|
2019-11-12 08:36:22 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task(docbuild)
|
|
|
|
|
def docserve(c):
|
|
|
|
|
"""Serve docs at http://localhost:$DOCS_PORT/ (default port is 8000)"""
|
|
|
|
|
from livereload import Server
|
|
|
|
|
|
|
|
|
|
server = Server()
|
|
|
|
|
server.watch("docs/conf.py", lambda: docbuild(c))
|
|
|
|
|
server.watch("CONTRIBUTING.rst", lambda: docbuild(c))
|
|
|
|
|
server.watch("docs/*.rst", lambda: docbuild(c))
|
|
|
|
|
server.serve(port=DOCS_PORT, root="docs/_build")
|
|
|
|
|
|
|
|
|
|
|
2019-11-09 08:36:45 -08:00
|
|
|
@task
|
|
|
|
|
def tests(c):
|
|
|
|
|
"""Run the test suite"""
|
2021-04-18 22:20:54 -06:00
|
|
|
c.run(f"{VENV_BIN}/pytest", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
2023-10-28 17:01:33 -05:00
|
|
|
@task
|
|
|
|
|
def coverage(c):
|
|
|
|
|
"""Generate code coverage of running the test suite."""
|
2024-06-16 19:16:58 +02:00
|
|
|
c.run(
|
|
|
|
|
f"{VENV_BIN}/pytest --cov=pelican --cov-report term-missing "
|
2024-06-25 10:54:24 +02:00
|
|
|
"--cov-fail-under 75",
|
2024-06-16 19:16:58 +02:00
|
|
|
pty=PTY,
|
|
|
|
|
)
|
2023-10-28 17:01:33 -05:00
|
|
|
c.run(f"{VENV_BIN}/coverage html", pty=PTY)
|
|
|
|
|
|
|
|
|
|
|
2019-11-09 08:36:45 -08:00
|
|
|
@task
|
2023-10-29 09:50:01 -07:00
|
|
|
def format(c, check=False, diff=False):
|
|
|
|
|
"""Run Ruff's auto-formatter, optionally with --check or --diff"""
|
2019-11-09 08:36:45 -08:00
|
|
|
check_flag, diff_flag = "", ""
|
|
|
|
|
if check:
|
|
|
|
|
check_flag = "--check"
|
|
|
|
|
if diff:
|
|
|
|
|
diff_flag = "--diff"
|
2023-10-29 09:50:01 -07:00
|
|
|
c.run(
|
|
|
|
|
f"{VENV_BIN}/ruff format {check_flag} {diff_flag} {PKG_PATH} tasks.py", pty=PTY
|
|
|
|
|
)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2023-10-28 10:54:09 -07:00
|
|
|
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"{VENV_BIN}/ruff check {diff_flag} {fix_flag} .", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2023-10-28 10:54:09 -07:00
|
|
|
def lint(c, fix=False, diff=False):
|
|
|
|
|
"""Check code style via linting tools."""
|
|
|
|
|
ruff(c, fix=fix, diff=diff)
|
2023-10-29 09:50:01 -07:00
|
|
|
format(c, check=not fix, diff=diff)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def tools(c):
|
|
|
|
|
"""Install tools in the virtual environment if not already on PATH"""
|
|
|
|
|
for tool in TOOLS:
|
|
|
|
|
if not which(tool):
|
2021-10-08 08:59:35 +02:00
|
|
|
c.run(f"{VENV_BIN}/python -m pip install {tool}", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def precommit(c):
|
|
|
|
|
"""Install pre-commit hooks to .git/hooks/pre-commit"""
|
2021-10-08 08:59:35 +02:00
|
|
|
c.run(f"{PRECOMMIT} install", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def setup(c):
|
2021-10-08 08:59:35 +02:00
|
|
|
c.run(f"{VENV_BIN}/python -m pip install -U pip", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
tools(c)
|
2023-10-28 17:43:16 +02:00
|
|
|
c.run(f"{PDM} install", pty=PTY)
|
2019-11-09 08:36:45 -08:00
|
|
|
precommit(c)
|
2019-11-12 07:40:05 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def update_functional_tests(c):
|
|
|
|
|
"""Update the generated functional test output"""
|
|
|
|
|
c.run(
|
2021-10-08 08:59:35 +02:00
|
|
|
f"bash -c 'LC_ALL=en_US.utf8 pelican -o {PKG_PATH}/tests/output/custom/ \
|
|
|
|
|
-s samples/pelican.conf.py samples/content/'",
|
|
|
|
|
pty=PTY,
|
2019-11-12 07:40:05 -08:00
|
|
|
)
|
|
|
|
|
c.run(
|
2021-10-08 08:59:35 +02:00
|
|
|
f"bash -c 'LC_ALL=fr_FR.utf8 pelican -o {PKG_PATH}/tests/output/custom_locale/ \
|
|
|
|
|
-s samples/pelican.conf_FR.py samples/content/'",
|
|
|
|
|
pty=PTY,
|
2019-11-12 07:40:05 -08:00
|
|
|
)
|
|
|
|
|
c.run(
|
2021-10-08 08:59:35 +02:00
|
|
|
f"bash -c 'LC_ALL=en_US.utf8 pelican -o \
|
|
|
|
|
{PKG_PATH}/tests/output/basic/ samples/content/'",
|
|
|
|
|
pty=PTY,
|
2019-11-12 07:40:05 -08:00
|
|
|
)
|