|
| 1 | +"""Developer task automation.""" |
| 2 | +import os |
| 3 | + |
| 4 | +import nox |
| 5 | + |
| 6 | +nox.options.sessions = [ |
| 7 | + "check_code_formatting", |
| 8 | + "check_types", |
| 9 | + "run_tests", |
| 10 | +] |
| 11 | + |
| 12 | +PYTHON = ["3.12"] |
| 13 | + |
| 14 | + |
| 15 | +@nox.session(python=PYTHON, reuse_venv=True) |
| 16 | +def run_tests(session: nox.Session): |
| 17 | + """Run unit tests.""" |
| 18 | + session.install(".[dev]") |
| 19 | + pytest_args = session.posargs if session.posargs else [] |
| 20 | + session.run("pytest", *pytest_args) |
| 21 | + |
| 22 | + |
| 23 | +@nox.session(python=PYTHON, reuse_venv=True) |
| 24 | +def format_code(session: nox.Session): |
| 25 | + """Lint code and re-format where necessary.""" |
| 26 | + session.install(".[dev]") |
| 27 | + session.run("black", "--config=pyproject.toml", ".") |
| 28 | + session.run("ruff", "check", ".", "--config=pyproject.toml", "--fix") |
| 29 | + |
| 30 | + |
| 31 | +@nox.session(python=PYTHON, reuse_venv=True) |
| 32 | +def check_code_formatting(session: nox.Session): |
| 33 | + """Check code for formatting errors.""" |
| 34 | + session.install(".[dev]") |
| 35 | + session.run("black", "--config=pyproject.toml", "--check", ".") |
| 36 | + session.run("ruff", "check", ".", "--config=pyproject.toml") |
| 37 | + |
| 38 | + |
| 39 | +@nox.session(python=PYTHON, reuse_venv=True) |
| 40 | +def check_types(session: nox.Session): |
| 41 | + """Run static type checking.""" |
| 42 | + session.install(".[dev]") |
| 43 | + session.run("mypy") |
| 44 | + |
| 45 | + |
| 46 | +@nox.session(python=PYTHON, reuse_venv=True) |
| 47 | +def build_and_deploy(session: nox.Session): |
| 48 | + """Build wheel and deploy to PyPI.""" |
| 49 | + try: |
| 50 | + from dotenv import load_dotenv |
| 51 | + |
| 52 | + load_dotenv() |
| 53 | + except ModuleNotFoundError: |
| 54 | + session.warn("Expecting PYPI_USR and PYPI_PWD in local environment variables.") |
| 55 | + |
| 56 | + try: |
| 57 | + PYPI_USR = os.environ["PYPI_USR"] |
| 58 | + PYPI_PWD = os.environ["PYPI_PWD"] |
| 59 | + except KeyError as e: |
| 60 | + session.error(f"{str(e)} not found in local environment variables.") |
| 61 | + session.install(".[deploy]") |
| 62 | + session.run("rm", "-rf", "dist") |
| 63 | + session.run("python", "-m", "build") |
| 64 | + session.run("twine", "upload", "dist/*", "-u", PYPI_USR, "-p", PYPI_PWD) |
0 commit comments