Support Poetry-managed virtualenvs in Invoke tasks

This commit is contained in:
Justin Mayer 2021-05-04 05:37:47 -05:00
commit eba18bca84

View file

@ -1,79 +1,102 @@
from inspect import cleandoc
import os import os
from pathlib import Path from pathlib import Path
from shutil import which from shutil import which
import sys
from invoke import task from invoke import task
PKG_NAME = "share_post" PKG_NAME = "share_post"
PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}") PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}")
TOOLS = ("poetry", "pre-commit")
ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None) ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs")) VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs"))
VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME / PKG_NAME) VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME.expanduser() / PKG_NAME)
VENV = str(VENV_PATH.expanduser()) VENV = str(VENV_PATH.expanduser())
BIN_DIR = "bin" if os.name != "nt" else "Scripts"
TOOLS = ["poetry", "pre-commit"] VENV_BIN = Path(VENV) / Path(BIN_DIR)
POETRY = which("poetry") if which("poetry") else (VENV / Path("bin") / "poetry") POETRY = which("poetry") if which("poetry") else (VENV_BIN / "poetry")
PRECOMMIT = ( CMD_PREFIX = f"{VENV_BIN}/" if ACTIVE_VENV else f"{POETRY} run "
which("pre-commit") if which("pre-commit") else (VENV / Path("bin") / "pre-commit") PRECOMMIT = which("pre-commit") if which("pre-commit") else f"{CMD_PREFIX}pre-commit"
) PTY = True if os.name != "nt" else False
@task @task
def tests(c): def tests(c):
"""Run the test suite""" """Run the test suite."""
c.run(f"{VENV}/bin/pytest", pty=True) c.run(f"{CMD_PREFIX}pytest", pty=PTY)
@task @task
def black(c, check=False, diff=False): def black(c, check=False, diff=False):
"""Run Black auto-formatter, optionally with --check or --diff""" """Run Black auto-formatter, optionally with `--check` or `--diff`."""
check_flag, diff_flag = "", "" check_flag, diff_flag = "", ""
if check: if check:
check_flag = "--check" check_flag = "--check"
if diff: if diff:
diff_flag = "--diff" diff_flag = "--diff"
c.run(f"{VENV}/bin/black {check_flag} {diff_flag} {PKG_PATH} tasks.py") c.run(f"{CMD_PREFIX}black {check_flag} {diff_flag} {PKG_PATH} tasks.py")
@task @task
def isort(c, check=False, diff=False): def isort(c, check=False, diff=False):
"""Ensure imports are sorted according to project standards."""
check_flag, diff_flag = "", "" check_flag, diff_flag = "", ""
if check: if check:
check_flag = "-c" check_flag = "-c"
if diff: if diff:
diff_flag = "--diff" diff_flag = "--diff"
c.run(f"{VENV}/bin/isort {check_flag} {diff_flag} .") c.run(f"{CMD_PREFIX}isort {check_flag} {diff_flag} .")
@task @task
def flake8(c): def flake8(c):
c.run(f"{VENV}/bin/flake8 {PKG_PATH} tasks.py") """Check code for PEP8 compliance via Flake8."""
c.run(f"{CMD_PREFIX}flake8 {PKG_PATH} tasks.py")
@task @task
def lint(c): def lint(c, diff=False):
isort(c, check=True) """Check code style via linting tools."""
black(c, check=True) isort(c, check=True, diff=diff)
black(c, check=True, diff=diff)
flake8(c) flake8(c)
@task @task
def tools(c): def tools(c):
"""Install tools in the virtual environment if not already on PATH""" """Install development tools in the virtual environment if not already on PATH."""
for tool in TOOLS: for tool in TOOLS:
if not which(tool): if not which(tool):
c.run(f"{VENV}/bin/pip install {tool}") print(f"** Installing {tool}.")
c.run(f"{CMD_PREFIX}pip install {tool}")
@task @task
def precommit(c): def precommit(c):
"""Install pre-commit hooks to .git/hooks/pre-commit""" """Install pre-commit hooks to `.git/hooks/pre-commit`."""
print("** Installing pre-commit hooks.")
c.run(f"{PRECOMMIT} install") c.run(f"{PRECOMMIT} install")
@task @task
def setup(c): def setup(c):
c.run(f"{VENV}/bin/pip install -U pip") """Set up the development environment."""
tools(c) if which("poetry") or ACTIVE_VENV:
c.run(f"{POETRY} install") tools(c)
precommit(c) c.run(f"{CMD_PREFIX}python -m pip install --upgrade pip")
c.run(f"{POETRY} install")
precommit(c)
print("\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.
You can either manually create and activate a virtual environment, or you can
install Poetry via:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
Once you have taken one of the above two steps, run `invoke setup` again.
""" # noqa: E501
sys.exit(cleandoc(error_message))