This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
forked from DisnakeDev/disnake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
281 lines (228 loc) · 7.98 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# SPDX-License-Identifier: MIT
from __future__ import annotations
import functools
import re
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Callable, List, TypeVar
import nox
if TYPE_CHECKING:
from typing_extensions import Concatenate, ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
NoxSessionFunc = Callable[Concatenate[nox.Session, P], T]
nox.options.error_on_external_run = True
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = [
"lint",
"check-manifest",
"slotscheck",
"pyright",
"test",
]
nox.needs_version = ">=2022.1.7"
# used to reset cached coverage data once for the first test run only
reset_coverage = True
REQUIREMENTS = {
".": "requirements.txt",
}
for path in Path("requirements").iterdir():
if match := re.fullmatch("requirements_(.+).txt", path.name):
REQUIREMENTS[match.group(1)] = str(path)
def depends(
*deps: str,
install_cwd: bool = False,
update: bool = True,
) -> Callable[[NoxSessionFunc[P, T]], NoxSessionFunc[P, T]]:
"""A session decorator that invokes :func:`.install` with the given parameters before running the session."""
def decorator(f: NoxSessionFunc[P, T]) -> NoxSessionFunc[P, T]:
@functools.wraps(f)
def wrapper(session: nox.Session, *args: P.args, **kwargs: P.kwargs) -> T:
install(session, *deps, update=update, install_cwd=install_cwd)
return f(session, *args, **kwargs)
return wrapper
return decorator
def install(
session: nox.Session,
*deps: str,
run: bool = False,
install_cwd: bool = False,
update: bool = True,
) -> None:
"""
Installs dependencies in a session.
Dependencies from the main ``requirements.txt`` will always be installed.
Parameters
----------
*deps: :class:`str`
Dependency group names, e.g. ``dev`` for ``requirements_dev.txt``.
run: :class:`bool`
Whether to use :func:`nox.Session.run` instead of :func:`nox.Session.install`,
useful to avoid warnings when running in the global python environment.
install_cwd: :class:`bool`
Whether the main package should be installed (in editable mode, i.e. ``-e .``).
update: :class:`bool`
Whether packages should be updated (i.e. ``-U``). Defaults to ``True``.
"""
install_args = []
if update:
install_args.append("-U")
if install_cwd:
install_args.extend(["-e", "."])
for d in dict.fromkeys((".", *deps)): # deduplicate
install_args.extend(["-r", REQUIREMENTS[d]])
if run:
session.run("python", "-m", "pip", "install", *install_args)
else:
session.install(*install_args)
def is_venv() -> bool:
# https://stackoverflow.com/a/42580137/5080607
return (
# virtualenv < v20
hasattr(sys, "real_prefix")
# virtualenv >= v20, others
or sys.base_prefix != sys.prefix
)
@nox.session()
@depends("docs")
def docs(session: nox.Session):
"""Build and generate the documentation.
If running locally, will build automatic reloading docs.
If running in CI, will build a production version of the documentation.
"""
with session.chdir("docs"):
args = ["-b", "html", "-n", ".", "_build/html", *session.posargs]
if session.interactive:
session.run(
"sphinx-autobuild",
"--ignore",
"_build",
"--watch",
"../disnake",
"--watch",
"../changelog",
"--port",
"8009",
"-j",
"auto",
*args,
)
else:
session.run(
"sphinx-build",
"-aE",
*args,
)
@nox.session(python=False)
def lint(session: nox.Session):
"""Check all files for linting errors"""
session.run("pre-commit", "run", "--all-files", *session.posargs)
@nox.session(name="check-manifest")
@depends("tools")
def check_manifest(session: nox.Session):
"""Run check-manifest."""
session.run("check-manifest", "-v", "--no-build-isolation")
@nox.session()
@depends("dev")
def slotscheck(session: nox.Session):
"""Run slotscheck."""
session.run("python", "-m", "slotscheck", "--verbose", "-m", "disnake")
@nox.session(name="codemod")
@depends("tools")
def codemod(session: nox.Session):
"""Run libcst codemods."""
if session.posargs and session.posargs[0] == "run-all" or not session.interactive:
# run all of the transformers on disnake
session.log("Running all transformers.")
res: str = session.run("python", "-m", "libcst.tool", "list", silent=True)
transformers = [line.split("-")[0].strip() for line in res.splitlines()]
session.log("Transformers: " + ", ".join(transformers))
for trans in transformers:
session.run(
"python", "-m", "libcst.tool", "codemod", trans, "disnake", "--hide-progress"
)
session.log("Finished running all transformers.")
else:
if session.posargs:
if len(session.posargs) < 2:
session.posargs.append("disnake")
session.run(
"python",
"-m",
"libcst.tool",
"codemod",
*session.posargs,
)
else:
session.run("python", "-m", "libcst.tool", "list")
@nox.session()
@depends("dev", "docs", "speed", "voice", install_cwd=True)
def pyright(session: nox.Session):
"""Run pyright."""
env = {
"PYRIGHT_PYTHON_IGNORE_WARNINGS": "1",
}
try:
session.run("python", "-m", "pyright", *session.posargs, env=env)
except KeyboardInterrupt:
pass
@nox.session(python=["3.8", "3.9", "3.10"])
@nox.parametrize(
"extras",
[
[],
# NOTE: disabled while there are no tests that would require these dependencies
# ["speed"],
# ["voice"],
],
)
def test(session: nox.Session, extras: List[str]):
"""Run tests."""
install(session, "dev", *extras)
pytest_args = ["--cov", "--cov-context=test"]
global reset_coverage
if reset_coverage:
# don't use `--cov-append` for first run
reset_coverage = False
else:
# use `--cov-append` in all subsequent runs
pytest_args.append("--cov-append")
# TODO: only run tests that depend on the different dependencies
session.run(
"pytest",
*pytest_args,
*session.posargs,
)
@nox.session()
@depends("dev")
def coverage(session: nox.Session):
"""Display coverage information from the tests."""
if "html" in session.posargs or "serve" in session.posargs:
session.run("coverage", "html", "--show-contexts")
if "serve" in session.posargs:
session.run(
"python", "-m", "http.server", "8012", "--directory", "htmlcov", "--bind", "127.0.0.1"
)
if "erase" in session.posargs:
session.run("coverage", "erase")
@nox.session(python=False)
def setup(session: nox.Session):
"""Set up the external environment."""
if session.interactive and not is_venv():
confirm = input(
"It looks like you are about to install the dependencies into your *global* python environment."
" This may overwrite other versions of the dependencies that you already have installed, including disnake itself."
" Consider using a virtual environment (virtualenv/venv) instead. Continue anyway? [y/N]"
)
if confirm.lower() != "y":
session.error("Cancelled")
session.log("Installing dependencies to the external environment.")
if session.posargs:
deps = list(session.posargs)
else:
deps = list(REQUIREMENTS.keys())
if "." not in deps:
deps.insert(0, ".") # index doesn't really matter
install(session, *deps, run=True, install_cwd=True)
if session.interactive and "dev" in deps:
session.run("pre-commit", "install", "--install-hooks")