-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoxfile.py
134 lines (115 loc) · 4.4 KB
/
noxfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Declaration of Nox commands which can be run for this package"""
import hashlib
from pathlib import Path
from typing import Iterable
import nox
PYTHON_VERSIONS = ["3.10", "3.11"]
TESTS_SUBFOLDER = "tests"
PACKAGE_NAME = "spotfishing"
def install_groups(
session: nox.Session,
*,
include: Iterable[str] = (),
include_self: bool = True,
) -> None:
"""Install Poetry dependency groups.
Install the given dependency groups into the session's virtual environment.
When 'include_self' is true, also installs this package and default dependencies.
We cannot use `poetry install` here, because it ignores the
session's environment and installs into Poetry's own environment.
Instead, use `poetry export` with suitable options to generate a requirements.txt
file to pass to session.install().
Auto-skip the `poetry export` step if the poetry.lock file is unchanged
(matching hash) since the last time this session was run.
"""
if isinstance(session.virtualenv, nox.virtualenv.PassthroughEnv):
session.warn(
"Running outside a Nox virtualenv! We will skip installation here, "
"and simply assume that the necessary dependency groups have "
"already been installed by other means!"
)
return
lockdata = Path("poetry.lock").read_bytes()
digest = hashlib.blake2b(lockdata).hexdigest()
requirements_txt = Path(session.cache_dir, session.name, "reqs_from_poetry.txt")
hashfile = requirements_txt.with_suffix(".hash")
if not hashfile.is_file() or hashfile.read_text() != digest:
# First, adjust settings to avoid warning that we cover in shell.nix.
# See: https://python-poetry.org/blog/announcing-poetry-1.7.0/
argv = [
"poetry",
"config",
"warnings.export",
"false",
]
session.debug(
f"Running command to silence warning since in Nix shell we install the patch: {' '.join(argv)}"
)
session.run_always(*argv, external=True)
session.log(f"Will generate requirements hashfile: {hashfile}")
requirements_txt.parent.mkdir(parents=True, exist_ok=True)
argv = [
"poetry",
"export",
"--format=requirements.txt",
f"--output={requirements_txt}",
]
if include:
option = "only" if not include_self else "with"
argv.append(f"--{option}={','.join(include)}")
session.debug(f"Running command: {' '.join(argv)}")
session.run_always(*argv, external=True)
session.debug(f"Writing requirements hashfile: {hashfile}")
hashfile.write_text(digest)
session.log("Installing requirements")
session.install("-r", str(requirements_txt))
if include_self:
session.log(f"Installing {PACKAGE_NAME}")
session.install("-e", ".")
else:
session.debug(f"Skipping installation of {PACKAGE_NAME}")
@nox.session(python=PYTHON_VERSIONS)
def tests(session):
install_groups(session, include=["test"])
session.run(
"pytest",
"-x",
"--cov",
"--log-level=debug",
"--durations=10",
"--hypothesis-show-statistics",
*session.posargs,
)
@nox.session(python=PYTHON_VERSIONS)
def lint(session):
install_groups(session, include=["lint"])
session.run("mypy")
session.run("pylint", PACKAGE_NAME)
extra_warnings_to_disable_for_tests_subfolder = [
"invalid-name",
"missing-function-docstring",
"protected-access",
"redefined-outer-name",
"too-many-arguments",
"too-many-instance-attributes",
"too-many-lines",
"unused-argument",
"use-implicit-booleaness-not-comparison",
]
session.run(
"pylint",
f"--disable={','.join(extra_warnings_to_disable_for_tests_subfolder)}",
TESTS_SUBFOLDER,
)
@nox.session
def format(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--enable-colors")
session.run("isort", PACKAGE_NAME, TESTS_SUBFOLDER, "--check", "--diff", "--color")
session.run("black", ".", "--check", "--diff", "--color")
@nox.session
def reformat(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--write-changes")
session.run("isort", PACKAGE_NAME, TESTS_SUBFOLDER)
session.run("black", ".")