Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit bdef222

Browse files
author
Thiago C. D'Ávila
authored
Merge pull request #130 from staticdev/precommit-flake-nox
Flake8 / pre-commit / nox review
2 parents 31d985b + 2d3e279 commit bdef222

8 files changed

+532
-48
lines changed

.pre-commit-config.yaml

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
repos:
2-
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.1.0
2+
- repo: local
43
hooks:
4+
- id: black
5+
name: black
6+
entry: black
7+
language: system
8+
types: [python]
9+
require_serial: true
10+
- id: check-added-large-files
11+
name: Check for added large files
12+
entry: check-added-large-files
13+
language: system
514
- id: check-toml
15+
name: Check Toml
16+
entry: check-toml
17+
language: system
18+
types: [toml]
619
- id: check-yaml
20+
name: Check Yaml
21+
entry: check-yaml
22+
language: system
23+
types: [yaml]
724
- id: end-of-file-fixer
25+
name: Fix End of Files
26+
entry: end-of-file-fixer
27+
language: system
28+
types: [text]
29+
stages: [commit, push, manual]
30+
- id: flake8
31+
name: flake8
32+
entry: flake8
33+
language: system
34+
types: [python]
35+
require_serial: true
36+
- id: reorder-python-imports
37+
name: Reorder python imports
38+
entry: reorder-python-imports
39+
language: system
40+
types: [python]
41+
args: [--application-directories=src]
842
- id: trailing-whitespace
9-
- id: check-added-large-files
43+
name: Trim Trailing Whitespace
44+
entry: trailing-whitespace-fixer
45+
language: system
46+
types: [text]
47+
stages: [commit, push, manual]
1048
# - repo: https://github.com/prettier/prettier
1149
# rev: 2.0.5
1250
# hooks:
1351
# - id: prettier
14-
- repo: https://github.com/psf/black
15-
rev: 19.10b0
16-
hooks:
17-
- id: black
18-
- repo: https://gitlab.com/pycqa/flake8
19-
rev: 3.8.1
20-
hooks:
21-
- id: flake8
22-
additional_dependencies:
23-
- flake8-bandit==2.1.2
24-
- flake8-bugbear==20.1.4
25-
- flake8-docstrings==1.5.0
26-
- pep8-naming==0.10.0
27-
- darglint==1.3.0
28-
- repo: https://github.com/asottile/reorder_python_imports
29-
rev: v2.3.0
30-
hooks:
31-
- id: reorder-python-imports
32-
args: [--application-directories=src]

noxfile.py

+68-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import tempfile
66
from pathlib import Path
7+
from textwrap import dedent
78
from typing import cast
89
from typing import Iterator
910

@@ -110,12 +111,78 @@ def install(session: Session, *args: str) -> None:
110111
session.install(f"--constraint={requirements}", *args)
111112

112113

114+
def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
115+
"""Activate virtualenv in hooks installed by pre-commit.
116+
117+
This function patches git hooks installed by pre-commit to activate the
118+
session's virtual environment. This allows pre-commit to locate hooks in
119+
that environment when invoked from git.
120+
121+
Args:
122+
session: The Session object.
123+
"""
124+
if session.bin is None:
125+
return
126+
127+
virtualenv = session.env.get("VIRTUAL_ENV")
128+
if virtualenv is None:
129+
return
130+
131+
hookdir = Path(".git") / "hooks"
132+
if not hookdir.is_dir():
133+
return
134+
135+
for hook in hookdir.iterdir():
136+
if hook.name.endswith(".sample") or not hook.is_file():
137+
continue
138+
139+
text = hook.read_text()
140+
bindir = repr(session.bin)[1:-1] # strip quotes
141+
if not (
142+
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
143+
):
144+
continue
145+
146+
lines = text.splitlines()
147+
if not (lines[0].startswith("#!") and "python" in lines[0].lower()):
148+
continue
149+
150+
header = dedent(
151+
f"""\
152+
import os
153+
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
154+
os.environ["PATH"] = os.pathsep.join((
155+
{session.bin!r},
156+
os.environ.get("PATH", ""),
157+
))
158+
"""
159+
)
160+
161+
lines.insert(1, header)
162+
hook.write_text("\n".join(lines))
163+
164+
113165
@nox.session(name="pre-commit", python="3.8")
114166
def precommit(session: Session) -> None:
115167
"""Lint using pre-commit."""
116168
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
117-
install(session, "pre-commit")
169+
install(
170+
session,
171+
"black",
172+
"darglint",
173+
"flake8",
174+
"flake8-bandit",
175+
"flake8-bugbear",
176+
"flake8-docstrings",
177+
"flake8-rst-docstrings",
178+
"pep8-naming",
179+
"pre-commit",
180+
"pre-commit-hooks",
181+
"reorder-python-imports",
182+
)
118183
session.run("pre-commit", *args)
184+
if args and args[0] == "install":
185+
activate_virtualenv_in_precommit_hooks(session)
119186

120187

121188
@nox.session(python="3.8")

0 commit comments

Comments
 (0)