|
1 | 1 | """Noxfile for the {{cookiecutter.project_name}} project.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import shlex |
| 5 | + |
2 | 6 | from pathlib import Path |
| 7 | +from textwrap import dedent |
3 | 8 | from typing import List |
4 | 9 |
|
5 | 10 | import nox |
|
23 | 28 | PACKAGE_NAME: str = "{{cookiecutter.package_name}}" |
24 | 29 |
|
25 | 30 |
|
| 31 | +def activate_virtualenv_in_precommit_hooks(session: Session) -> None: |
| 32 | + """Activate virtualenv in hooks installed by pre-commit. |
| 33 | +
|
| 34 | + This function patches git hooks installed by pre-commit to activate the |
| 35 | + session's virtual environment. This allows pre-commit to locate hooks in |
| 36 | + that environment when invoked from git. |
| 37 | +
|
| 38 | + Args: |
| 39 | + session: The Session object. |
| 40 | + """ |
| 41 | + assert session.bin is not None # nosec |
| 42 | + |
| 43 | + # Only patch hooks containing a reference to this session's bindir. Support |
| 44 | + # quoting rules for Python and bash, but strip the outermost quotes so we |
| 45 | + # can detect paths within the bindir, like <bindir>/python. |
| 46 | + bindirs = [ |
| 47 | + bindir[1:-1] if bindir[0] in "'\"" else bindir for bindir in (repr(session.bin), shlex.quote(session.bin)) |
| 48 | + ] |
| 49 | + |
| 50 | + virtualenv = session.env.get("VIRTUAL_ENV") |
| 51 | + if virtualenv is None: |
| 52 | + return |
| 53 | + |
| 54 | + headers = { |
| 55 | + # pre-commit < 2.16.0 |
| 56 | + "python": f"""\ |
| 57 | + import os |
| 58 | + os.environ["VIRTUAL_ENV"] = {virtualenv!r} |
| 59 | + os.environ["PATH"] = os.pathsep.join(( |
| 60 | + {session.bin!r}, |
| 61 | + os.environ.get("PATH", ""), |
| 62 | + )) |
| 63 | + """, |
| 64 | + # pre-commit >= 2.16.0 |
| 65 | + "bash": f"""\ |
| 66 | + VIRTUAL_ENV={shlex.quote(virtualenv)} |
| 67 | + PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
| 68 | + """, |
| 69 | + # pre-commit >= 2.17.0 on Windows forces sh shebang |
| 70 | + "/bin/sh": f"""\ |
| 71 | + VIRTUAL_ENV={shlex.quote(virtualenv)} |
| 72 | + PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
| 73 | + """, |
| 74 | + } |
| 75 | + |
| 76 | + hookdir: Path = Path(".git") / "hooks" |
| 77 | + if not hookdir.is_dir(): |
| 78 | + return |
| 79 | + |
| 80 | + for hook in hookdir.iterdir(): |
| 81 | + if hook.name.endswith(".sample") or not hook.is_file(): |
| 82 | + continue |
| 83 | + |
| 84 | + if not hook.read_bytes().startswith(b"#!"): |
| 85 | + continue |
| 86 | + |
| 87 | + text: str = hook.read_text() |
| 88 | + |
| 89 | + if not any((Path("A") == Path("a") and bindir.lower() in text.lower()) or bindir in text for bindir in bindirs): |
| 90 | + continue |
| 91 | + |
| 92 | + lines: list[str] = text.splitlines() |
| 93 | + |
| 94 | + for executable, header in headers.items(): |
| 95 | + if executable in lines[0].lower(): |
| 96 | + lines.insert(1, dedent(header)) |
| 97 | + hook.write_text("\n".join(lines)) |
| 98 | + break |
| 99 | + |
| 100 | + |
26 | 101 | @nox.session(python=DEFAULT_PYTHON_VERSION, name="pre-commit") |
27 | | -def pre_commit(session: Session) -> None: |
28 | | - """Run pre-commit checks.""" |
| 102 | +def precommit(session: Session) -> None: |
| 103 | + """Lint using pre-commit.""" |
| 104 | + args: list[str] = session.posargs or ["run", "--all-files", "--hook-stage=manual", "--show-diff-on-failure"] |
| 105 | + |
29 | 106 | session.log("Installing pre-commit dependencies...") |
30 | 107 | session.install("-e", ".", "--group", "dev") |
31 | 108 |
|
| 109 | + session.run("pre-commit", *args) |
| 110 | + if args and args[0] == "install": |
| 111 | + activate_virtualenv_in_precommit_hooks(session) |
| 112 | + |
32 | 113 |
|
33 | 114 | @nox.session(python=DEFAULT_PYTHON_VERSION, name="format-python") |
34 | 115 | def format_python(session: Session) -> None: |
|
0 commit comments