-
Notifications
You must be signed in to change notification settings - Fork 5
/
noxfile.py
176 lines (143 loc) · 5.2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2021-present, Ethan Henderson
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from functools import wraps
from pathlib import Path
from typing import Callable
from typing import List
from typing import Optional
import nox
REPO_DIR = Path(__file__).parent
PROJECT_NAME = REPO_DIR.stem
PROJECT_DIR = REPO_DIR / PROJECT_NAME
TEST_DIR = REPO_DIR / "tests"
EXAMPLES_DIR = REPO_DIR / "examples"
NOX_FILE = REPO_DIR / "noxfile.py"
SETUP_FILE = REPO_DIR / "setup.py"
REQUIREMENTS_FILE = REPO_DIR / "requirements/nox.txt"
SessFT = Callable[[nox.Session], None]
def install(
*,
meta: bool = False,
rfiles: Optional[List[str]] = None,
libs: Optional[List[str]] = None,
) -> Callable[[SessFT], SessFT]:
def decorator(func: SessFT) -> SessFT:
@wraps(func)
def wrapper(session: nox.Session) -> None:
deps: list[str] = []
processing = False
with open(REQUIREMENTS_FILE) as f:
for line in f:
if line.startswith("#") and line[2:].strip() == func.__name__:
processing = True
continue
if processing:
if line == "\n":
processing = False
break
deps.append(line.strip())
args = ["-U", *deps]
if meta:
args.append(".")
for x in rfiles or []:
args.extend(["-r", f"requirements/{x}.txt"])
if libs:
args.extend(libs)
session.install(*args)
func(session)
return wrapper
return decorator
def sp(*paths: Path) -> List[str]:
return [str(p) for p in paths]
@nox.session(reuse_venv=True)
@install(meta=True)
def tests(session: nox.Session) -> None:
session.run(
"pytest",
"--log-level=1",
f"--cov={PROJECT_NAME}",
"--cov-report=term-missing",
)
@nox.session(reuse_venv=True, python=["3.11"])
@install()
def dependencies(session: nox.Session) -> None:
session.run("deputil", "update", "requirements")
@nox.session(reuse_venv=True)
@install()
def formatting(session: nox.Session) -> None:
session.run(
"black",
"--check",
*sp(PROJECT_DIR, TEST_DIR, EXAMPLES_DIR, NOX_FILE, SETUP_FILE),
)
@nox.session(reuse_venv=True)
def licensing(session: nox.Session) -> None:
expd = "# Copyright (c) 2021-present, Ethan Henderson"
errors = []
for path in (
*PROJECT_DIR.rglob("*.py"),
*TEST_DIR.rglob("*.py"),
NOX_FILE,
SETUP_FILE,
):
with open(path) as f:
if not f.read().startswith(expd):
errors.append(path)
if errors:
session.error(
f"\n{len(errors):,} file(s) are missing their licenses:\n"
+ "\n".join(f" - {file}" for file in errors),
)
@nox.session(reuse_venv=True)
@install()
def linting(session: nox.Session) -> None:
# TODO: Add examples directory once errors are fixed and add linter-
# specific error code excludes.
session.run("ruff", "check", *sp(PROJECT_DIR, NOX_FILE))
@nox.session(reuse_venv=True)
@install(meta=True)
def slots(session: nox.Session) -> None:
session.run("slotscheck", "-m", "analytix")
@nox.session(reuse_venv=True)
@install()
def spelling(session: nox.Session) -> None:
session.run(
"codespell",
*sp(PROJECT_DIR, TEST_DIR, EXAMPLES_DIR, NOX_FILE, SETUP_FILE),
"-S",
"**/analytix/reports/data*",
)
@nox.session(reuse_venv=True)
@install(rfiles=["base"])
def typing(session: nox.Session) -> None:
session.run(
"mypy",
"--install-types",
"--non-interactive",
*sp(PROJECT_DIR, EXAMPLES_DIR, SETUP_FILE),
)