tasks.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import os
  2. from pathlib import Path
  3. from shutil import which
  4. from invoke import task
  5. PKG_NAME = "read_more"
  6. PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}")
  7. ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
  8. VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs"))
  9. VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME / PKG_NAME)
  10. VENV = str(VENV_PATH.expanduser())
  11. TOOLS = ["poetry", "pre-commit"]
  12. POETRY = which("poetry") if which("poetry") else (VENV / Path("bin") / "poetry")
  13. PRECOMMIT = (
  14. which("pre-commit") if which("pre-commit") else (VENV / Path("bin") / "pre-commit")
  15. )
  16. @task
  17. def tests(c):
  18. """Run the test suite"""
  19. c.run(f"{VENV}/bin/pytest", pty=True)
  20. @task
  21. def black(c, check=False, diff=False):
  22. """Run Black auto-formatter, optionally with --check or --diff"""
  23. check_flag, diff_flag = "", ""
  24. if check:
  25. check_flag = "--check"
  26. if diff:
  27. diff_flag = "--diff"
  28. c.run(f"{VENV}/bin/black {check_flag} {diff_flag} {PKG_PATH} tasks.py")
  29. @task
  30. def isort(c, check=False, diff=False):
  31. check_flag, diff_flag = "", ""
  32. if check:
  33. check_flag = "-c"
  34. if diff:
  35. diff_flag = "--diff"
  36. c.run(f"{VENV}/bin/isort {check_flag} {diff_flag} .")
  37. @task
  38. def flake8(c):
  39. c.run(f"{VENV}/bin/flake8 {PKG_PATH} tasks.py")
  40. @task
  41. def lint(c):
  42. isort(c, check=True)
  43. black(c, check=True)
  44. flake8(c)
  45. @task
  46. def tools(c):
  47. """Install tools in the virtual environment if not already on PATH"""
  48. for tool in TOOLS:
  49. if not which(tool):
  50. c.run(f"{VENV}/bin/pip install {tool}")
  51. @task
  52. def precommit(c):
  53. """Install pre-commit hooks to .git/hooks/pre-commit"""
  54. c.run(f"{PRECOMMIT} install")
  55. @task
  56. def setup(c):
  57. c.run(f"{VENV}/bin/pip install -U pip")
  58. tools(c)
  59. c.run(f"{POETRY} install")
  60. precommit(c)