forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
389 lines (306 loc) · 11 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from __future__ import annotations
import functools
import os
import re
from pathlib import Path
from typing import Any, Callable, TypeVar
import nox
from nox.sessions import Session
ROOT = Path(__file__).parent
SRC = ROOT / "src"
POSARGS_PATTERN = re.compile(r"^(\w+)\[(.+)\]$")
TRUE_VALUES = {"true", "True", "TRUE", "1"}
_Return = TypeVar("_Return")
def do_first(
first_session_func: Callable[[Session], None]
) -> Callable[[Callable[[Session], _Return]], Callable[[Session], _Return]]:
"""Decorator for functions defining session actions that should happen first
>>> @do_first
>>> def setup(session):
>>> ... # do some setup
>>>
>>> @setup
>>> def the_actual_session(session):
>>> ... # so actual work
This makes it quick an easy to define common setup actions.
"""
def setup(
second_session_func: Callable[[Session], _Return]
) -> Callable[[Session], _Return]:
@functools.wraps(second_session_func)
def wrapper(session: Session) -> Any:
first_session_func(session)
return second_session_func(session)
return wrapper
return setup
@do_first
def apply_standard_pip_upgrades(session: Session) -> None:
session.install("--upgrade", "pip")
@do_first
def install_latest_npm_in_ci(session: Session) -> None:
if os.environ.get("CI") in TRUE_VALUES:
session.log("Running in CI environment - installing latest NPM")
session.run("npm", "install", "-g", "npm@latest", external=True)
@nox.session(reuse_venv=True)
@apply_standard_pip_upgrades
def format(session: Session) -> None:
"""Auto format Python and Javascript code"""
# format Python
install_requirements_file(session, "check-style")
session.run("black", ".")
session.run("isort", ".")
# format client Javascript
session.chdir(SRC / "client")
session.run("npm", "run", "format", external=True)
# format docs Javascript
session.chdir(ROOT / "docs" / "source" / "_custom_js")
session.run("npm", "run", "format", external=True)
@nox.session(reuse_venv=True)
@apply_standard_pip_upgrades
def example(session: Session) -> None:
"""Run an example"""
session.install("matplotlib")
install_idom_dev(session)
session.run(
"python",
"scripts/one_example.py",
*session.posargs,
env=get_idom_script_env(),
)
@nox.session(reuse_venv=True)
@install_latest_npm_in_ci
@apply_standard_pip_upgrades
def docs(session: Session) -> None:
"""Build and display documentation in the browser (automatically reloads on change)"""
install_requirements_file(session, "build-docs")
install_idom_dev(session, extras="all")
session.run(
"python",
"scripts/live_docs.py",
"--open-browser",
# watch python source too
"--watch=src/idom",
# for some reason this matches absolute paths
"--ignore=**/_auto/*",
"--ignore=**/_static/custom.js",
"--ignore=**/node_modules/*",
"--ignore=**/package-lock.json",
"-a",
"-E",
"-b",
"html",
"docs/source",
"docs/build",
env=get_idom_script_env(),
)
@nox.session
def docs_in_docker(session: Session) -> None:
"""Build a docker image for the documentation and run it to mimic production"""
session.run(
"docker",
"build",
".",
"--file",
"docs/Dockerfile",
"--tag",
"idom-docs:latest",
external=True,
)
session.run(
"docker",
"run",
"-it",
"-p",
"5000:5000",
"-e",
"DEBUG=1",
"--rm",
"idom-docs:latest",
external=True,
)
@nox.session
def test(session: Session) -> None:
"""Run the complete test suite"""
session.notify("test_python", posargs=session.posargs)
session.notify("test_docs")
session.notify("test_javascript")
@nox.session
def test_python(session: Session) -> None:
"""Run all Python checks"""
session.notify("test_python_suite", posargs=session.posargs)
session.notify("test_python_types")
session.notify("test_python_style")
session.notify("test_python_build")
@nox.session
def test_javascript(session: Session) -> None:
"""Run all Javascript checks"""
session.notify("test_javascript_suite")
session.notify("test_javascript_build")
session.notify("test_javascript_style")
@nox.session
@install_latest_npm_in_ci
@apply_standard_pip_upgrades
def test_python_suite(session: Session) -> None:
"""Run the Python-based test suite"""
session.env["IDOM_DEBUG_MODE"] = "1"
install_requirements_file(session, "test-env")
posargs = session.posargs
if "--no-cov" in session.posargs:
session.log("Coverage won't be checked")
session.install(".[all]")
else:
posargs += ["--cov=src/idom", "--cov-report", "term"]
install_idom_dev(session, extras="all")
session.run("pytest", *posargs)
@nox.session
@apply_standard_pip_upgrades
def test_python_types(session: Session) -> None:
"""Perform a static type analysis of the Python codebase"""
install_requirements_file(session, "check-types")
install_requirements_file(session, "pkg-deps")
install_requirements_file(session, "pkg-extras")
session.run("mypy", "--strict", "src/idom")
@nox.session
@apply_standard_pip_upgrades
def test_python_style(session: Session) -> None:
"""Check that Python style guidelines are being followed"""
install_requirements_file(session, "check-style")
session.run("flake8", "src/idom", "tests", "docs")
black_default_exclude = r"\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_build|buck-out|build|dist"
session.run(
"black",
".",
"--check",
"--exclude",
rf"/({black_default_exclude}|venv|node_modules)/",
)
session.run("isort", ".", "--check-only")
@nox.session
@apply_standard_pip_upgrades
def test_python_build(session: Session) -> None:
"""Test whether the Python package can be build for distribution"""
install_requirements_file(session, "build-pkg")
session.run("python", "-m", "build", "--sdist", "--wheel", "--outdir", "dist", ".")
@nox.session
@install_latest_npm_in_ci
@apply_standard_pip_upgrades
def test_docs(session: Session) -> None:
"""Verify that the docs build and that doctests pass"""
install_requirements_file(session, "build-docs")
install_idom_dev(session, extras="all")
session.run(
"sphinx-build",
"-a", # re-write all output files
"-T", # show full tracebacks
"-W", # turn warnings into errors
"--keep-going", # complete the build, but still report warnings as errors
"-b",
"html",
"docs/source",
"docs/build",
)
session.run("sphinx-build", "-b", "doctest", "docs/source", "docs/build")
# ensure docker image build works too
session.run("docker", "build", ".", "--file", "docs/Dockerfile", external=True)
@do_first
@install_latest_npm_in_ci
def setup_client_env(session: Session) -> None:
session.chdir(SRC / "client")
session.run("npm", "install", external=True)
@nox.session
@setup_client_env
def test_javascript_suite(session: Session) -> None:
"""Run the Javascript-based test suite and ensure it bundles succesfully"""
session.run("npm", "run", "test", external=True)
@nox.session
@setup_client_env
def test_javascript_build(session: Session) -> None:
"""Run the Javascript-based test suite and ensure it bundles succesfully"""
session.run("npm", "run", "test", external=True)
@nox.session
@setup_client_env
def test_javascript_style(session: Session) -> None:
"""Check that Javascript style guidelines are being followed"""
session.run("npm", "run", "check-format", external=True)
@nox.session
def build_js(session: Session) -> None:
"""Build javascript client code"""
session.chdir(SRC / "client")
session.run("npm", "run", "build", external=True)
@nox.session
def tag(session: Session) -> None:
"""Create a new git tag"""
try:
session.run(
"git",
"diff",
"--cached",
"--exit-code",
silent=True,
external=True,
)
except Exception:
session.error("Cannot create a tag - tROOT are uncommited changes")
version = get_version()
install_requirements_file(session, "make-release")
session.run("pysemver", "check", version)
changelog_file = ROOT / "docs" / "source" / "developing-idom" / "changelog.rst"
for line in changelog_file.read_text().splitlines():
if line == version:
break
else:
session.error(
f"No changelog entry for {version} in {changelog_file} - "
f"make sure you have a title section called {version}."
)
session.run("git", "tag", version, external=True)
if "push" in session.posargs:
session.run("git", "push", "--tags", external=True)
@nox.session
def update_version(session: Session) -> None:
"""Update the version of all Python and Javascript packages in this repo"""
if len(session.posargs) > 1:
session.error("To many arguments")
try:
new_version = session.posargs[0]
except IndexError:
session.error("No version tag given")
install_requirements_file(session, "make-release")
# check that version is valid semver
session.run("pysemver", "check", new_version)
old_version = get_version()
session.log(f"Old version: {old_version}")
session.log(f"New version: {new_version}")
set_version(new_version)
session.run("python", "scripts/update_versions.py")
# trigger npm install to update package-lock.json
session.install("-e", ".")
@nox.session(reuse_venv=True)
def latest_pull_requests(session: Session) -> None:
"""A basic script for outputing changelog info"""
session.install("requests", "python-dateutil")
session.run("python", "scripts/latest_pull_requests.py", *session.posargs)
@nox.session(reuse_venv=True)
def latest_closed_issues(session: Session) -> None:
"""A basic script for outputing changelog info"""
session.install("requests", "python-dateutil")
session.run("python", "scripts/latest_closed_issues.py", *session.posargs)
def install_requirements_file(session: Session, name: str) -> None:
file_path = ROOT / "requirements" / (name + ".txt")
assert file_path.exists(), f"requirements file {file_path} does not exist"
session.install("-r", str(file_path))
def install_idom_dev(session: Session, extras: str = "stable") -> None:
if "--no-install" not in session.posargs:
session.install("-e", f".[{extras}]")
else:
session.posargs.remove("--no-install")
def get_version() -> str:
return (ROOT / "VERSION").read_text().strip()
def set_version(new: str) -> None:
(ROOT / "VERSION").write_text(new.strip() + "\n")
def get_idom_script_env() -> dict[str, str]:
return {
"PYTHONPATH": os.getcwd(),
"IDOM_DEBUG_MODE": os.environ.get("IDOM_DEBUG_MODE", "1"),
"IDOM_CHECK_VDOM_SPEC": os.environ.get("IDOM_CHECK_VDOM_SPEC", "0"),
}