|
28 | 28 | PACKAGE_NAME: str = "{{cookiecutter.package_name}}" |
29 | 29 |
|
30 | 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 | | - |
101 | 31 | @nox.session(python=DEFAULT_PYTHON_VERSION, name="pre-commit") |
102 | 32 | def precommit(session: Session) -> None: |
103 | 33 | """Lint using pre-commit.""" |
@@ -416,3 +346,73 @@ def coverage(session: Session) -> None: |
416 | 346 | session.run("coverage", "report") |
417 | 347 |
|
418 | 348 | session.log(f"Coverage reports generated in ./{coverage_html_dir} and terminal.") |
| 349 | + |
| 350 | + |
| 351 | +def activate_virtualenv_in_precommit_hooks(session: Session) -> None: |
| 352 | + """Activate virtualenv in hooks installed by pre-commit. |
| 353 | +
|
| 354 | + This function patches git hooks installed by pre-commit to activate the |
| 355 | + session's virtual environment. This allows pre-commit to locate hooks in |
| 356 | + that environment when invoked from git. |
| 357 | +
|
| 358 | + Args: |
| 359 | + session: The Session object. |
| 360 | + """ |
| 361 | + assert session.bin is not None # nosec |
| 362 | + |
| 363 | + # Only patch hooks containing a reference to this session's bindir. Support |
| 364 | + # quoting rules for Python and bash, but strip the outermost quotes so we |
| 365 | + # can detect paths within the bindir, like <bindir>/python. |
| 366 | + bindirs = [ |
| 367 | + bindir[1:-1] if bindir[0] in "'\"" else bindir for bindir in (repr(session.bin), shlex.quote(session.bin)) |
| 368 | + ] |
| 369 | + |
| 370 | + virtualenv = session.env.get("VIRTUAL_ENV") |
| 371 | + if virtualenv is None: |
| 372 | + return |
| 373 | + |
| 374 | + headers = { |
| 375 | + # pre-commit < 2.16.0 |
| 376 | + "python": f"""\ |
| 377 | + import os |
| 378 | + os.environ["VIRTUAL_ENV"] = {virtualenv!r} |
| 379 | + os.environ["PATH"] = os.pathsep.join(( |
| 380 | + {session.bin!r}, |
| 381 | + os.environ.get("PATH", ""), |
| 382 | + )) |
| 383 | + """, |
| 384 | + # pre-commit >= 2.16.0 |
| 385 | + "bash": f"""\ |
| 386 | + VIRTUAL_ENV={shlex.quote(virtualenv)} |
| 387 | + PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
| 388 | + """, |
| 389 | + # pre-commit >= 2.17.0 on Windows forces sh shebang |
| 390 | + "/bin/sh": f"""\ |
| 391 | + VIRTUAL_ENV={shlex.quote(virtualenv)} |
| 392 | + PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH" |
| 393 | + """, |
| 394 | + } |
| 395 | + |
| 396 | + hookdir: Path = Path(".git") / "hooks" |
| 397 | + if not hookdir.is_dir(): |
| 398 | + return |
| 399 | + |
| 400 | + for hook in hookdir.iterdir(): |
| 401 | + if hook.name.endswith(".sample") or not hook.is_file(): |
| 402 | + continue |
| 403 | + |
| 404 | + if not hook.read_bytes().startswith(b"#!"): |
| 405 | + continue |
| 406 | + |
| 407 | + text: str = hook.read_text() |
| 408 | + |
| 409 | + if not any((Path("A") == Path("a") and bindir.lower() in text.lower()) or bindir in text for bindir in bindirs): |
| 410 | + continue |
| 411 | + |
| 412 | + lines: list[str] = text.splitlines() |
| 413 | + |
| 414 | + for executable, header in headers.items(): |
| 415 | + if executable in lines[0].lower(): |
| 416 | + lines.insert(1, dedent(header)) |
| 417 | + hook.write_text("\n".join(lines)) |
| 418 | + break |
0 commit comments