-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
219 lines (182 loc) · 5.86 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
import nox
# Default sessions
nox.options.sessions = ["lint", "typing", "test", "docs"]
# Other nox defaults
nox.options.default_venv_backend = "uv"
nox.options.reuse_existing_virtualenvs = True
# Pip installable dependencies for the client and server packages
PIP_DEPENDENCIES = [
("-r", "server/requirements/main.txt"),
("-r", "server/requirements/dev.txt"),
("-e", "client"),
("-e", "server"),
]
def _install(session: nox.Session) -> None:
"""Install the application and all dependencies into the session."""
session.install("--upgrade", "uv")
for deps in PIP_DEPENDENCIES:
session.install(*deps)
def _install_dev(session: nox.Session, bin_prefix: str = "") -> None:
"""Install the application and all development dependencies into the
specified virtual environment.
"""
python = f"{bin_prefix}python"
precommit = f"{bin_prefix}pre-commit"
# Install dev dependencies
session.run(python, "-m", "pip", "install", "uv", external=True)
for deps in PIP_DEPENDENCIES:
session.run(python, "-m", "uv", "pip", "install", *deps, external=True)
session.run(
python,
"-m",
"uv",
"pip",
"install",
"nox",
"pre-commit",
external=True,
)
# Install pre-commit hooks
session.run(precommit, "install", external=True)
def _make_env_vars() -> dict[str, str]:
"""Create a environment variable dictionary for test sessions that enables
the app to start up.
"""
return {
"SQUAREBOT_LOG_LEVEL": "DEBUG",
"SQUAREBOT_ENVIRONMENT_URL": "http://example.org",
"KAFKA_BOOTSTRAP_SERVERS": "localhost:9092",
"KAFKA_SECURITY_PROTOCOL": "PLAINTEXT",
"SQUAREBOT_SLACK_SIGNING": "1234",
"SQUAREBOT_SLACK_TOKEN": "1234",
"SQUAREBOT_SLACK_APP_ID": "1234",
}
@nox.session(name="venv-init")
def init_dev(session: nox.Session) -> None:
"""Set up a development venv."""
# Create a venv in the current directory, replacing any existing one
session.run("python", "-m", "venv", ".venv", "--clear")
_install_dev(session, bin_prefix=".venv/bin/")
print(
"\nTo activate this virtual env, run:\n\n\tsource .venv/bin/activate\n"
)
@nox.session(name="init", python=False)
def init(session: nox.Session) -> None:
"""Set up the development environment in the current virtual env."""
_install_dev(session, bin_prefix="")
@nox.session
def lint(session: nox.Session) -> None:
"""Run pre-commit hooks."""
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files", *session.posargs)
@nox.session
def typing(session: nox.Session) -> None:
"""Run mypy."""
_install(session)
session.install("mypy")
session.run("mypy", "noxfile.py")
with session.chdir("server"):
session.run("mypy", "src", "tests", *session.posargs)
with session.chdir("client"):
session.run("mypy", "src", *session.posargs)
@nox.session
def test(session: nox.Session) -> None:
"""Run pytest."""
_install(session)
with session.chdir("server"):
session.run(
"pytest",
"--cov=squarebot",
"--cov-branch",
*session.posargs,
env=_make_env_vars(),
)
@nox.session
def docs(session: nox.Session) -> None:
"""Build the docs."""
_install(session)
doctree_dir = (session.cache_dir / "doctrees").absolute()
with session.chdir("docs"):
session.run(
"sphinx-build",
# "-W",
"--keep-going",
"-n",
"-T",
"-b",
"html",
"-d",
str(doctree_dir),
".",
"./_build/html",
env=_make_env_vars(),
)
@nox.session(name="docs-linkcheck")
def docs_linkcheck(session: nox.Session) -> None:
"""Linkcheck the docs."""
_install(session)
doctree_dir = (session.cache_dir / "doctrees").absolute()
with session.chdir("docs"):
session.run(
"sphinx-build",
# "-W",
"--keep-going",
"-n",
"-T",
"-b" "linkcheck",
"-d",
str(doctree_dir),
".",
"./_build/html",
env=_make_env_vars(),
)
@nox.session(name="scriv-create")
def scriv_create(session: nox.Session) -> None:
"""Create a scriv entry."""
session.install("scriv")
session.run("scriv", "create")
@nox.session(name="scriv-collect")
def scriv_collect(session: nox.Session) -> None:
"""Collect scriv entries."""
session.install("scriv")
session.run("scriv", "collect", "--add", "--version", *session.posargs)
@nox.session(name="update-deps")
def update_deps(session: nox.Session) -> None:
"""Update pinned server dependencies and pre-commit hooks."""
session.install(
"--upgrade", "pip-tools", "pip", "setuptools", "wheel", "pre-commit"
)
session.run("pre-commit", "autoupdate")
# Dependencies are unpinned for compatibility with the unpinned client
# dependency.
session.run(
"uv",
"pip",
"compile",
"--upgrade",
"--output-file",
"server/requirements/main.txt",
"server/requirements/main.in",
)
session.run(
"uv",
"pip",
"compile",
"--upgrade",
"--output-file",
"server/requirements/dev.txt",
"server/requirements/dev.in",
)
print("\nTo refresh the development venv, run:\n\n\tnox -s init\n")
@nox.session(name="run")
def run(session: nox.Session) -> None:
"""Run the application in development mode."""
# Note this doesn't work right now because Kafka is needed for the app.
_install(session)
with session.chdir("server"):
session.run(
"uvicorn",
"squarebot.main:app",
"--reload",
env=_make_env_vars(),
)