-
Notifications
You must be signed in to change notification settings - Fork 58
/
noxfile.py
163 lines (127 loc) · 4.82 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
import pathlib
import shutil
import nox
ROOT = pathlib.Path(__file__).parent
@nox.session
def test(session: nox.Session) -> None:
"""Run the tests."""
session.install("matplotlib", "pandas")
session.install("pytest", "pytest-xdist")
with session.chdir("lessons/python/ivy-diffusion"):
session.install("-e", ".")
args = [
"-n",
"auto",
"-vvv",
] + session.posargs
with session.chdir("lessons/best-practices"):
for fname in ["examples.py", "test_examples.py"]:
session.log(f"test -f {fname}")
if not pathlib.Path(fname).is_file():
session.error(f"{fname}: file does not exist")
session.run("pytest", *args)
@nox.session(name="test-notebooks", venv_backend="mamba", python="3.11")
def test_notebooks(session: nox.Session) -> None:
"""Run the notebooks."""
args = [
"pytest",
"lessons",
"--ignore-glob=*py",
"--nbmake",
"--nbmake-kernel=python3",
"--nbmake-timeout=3000",
"-n",
"auto",
"-vvv",
] + session.posargs
session.conda_install("nbmake", "pytest", "pytest-xdist")
session.conda_install("--file", "lessons/requirements.in")
session.install("git+https://github.com/csdms/bmi-example-python.git")
session.run(*args)
@nox.session(name="insert-toc")
def insert_toc(session: nox.Session) -> None:
session.install("git+https://github.com/mcflugen/heartfelt-hooks.git")
notebooks = (
"lessons/landlab/landlab/bedrock_landslides_on_dems.ipynb",
"lessons/landlab/landlab/create_a_component-solution.ipynb",
"lessons/landlab/landlab/intro-to-grids-solution.ipynb",
"lessons/landlab/landlab/intro_part_for_component_clinic.ipynb",
"lessons/landlab/landlab/landlab-fault-scarp-for-espin-solution.ipynb",
"lessons/landlab/landlab/overland_flow.ipynb",
"lessons/landlab/landlab/practice-your-skills-solution.ipynb",
"lessons/landlab/landlab/tidal_flow_calculator.ipynb",
)
for notebook in notebooks:
session.run("insert-toc", "--in-place", "--allow-missing-toc", "-vvv", notebook)
@nox.session(name="hide-solutions")
def hide_solutions(session: nox.Session) -> None:
session.install("git+https://github.com/mcflugen/heartfelt-hooks.git")
insert_toc(session)
solution_notebooks = (
(
"lessons/landlab/landlab/practice-your-skills-solution.ipynb",
"lessons/landlab/landlab/practice-your-skills.ipynb",
),
(
"lessons/landlab/landlab/intro-to-grids-solution.ipynb",
"lessons/landlab/landlab/intro-to-grids.ipynb",
),
(
"lessons/landlab/landlab/landlab-fault-scarp-for-espin-solution.ipynb",
"lessons/landlab/landlab/landlab-fault-scarp-for-espin.ipynb",
),
(
"lessons/landlab/landlab/create_a_component-solution.ipynb",
"lessons/landlab/landlab/create_a_component.ipynb",
),
)
for solution, lesson in solution_notebooks:
shutil.copy(solution, solution + ".bak")
out = session.run(
"hide-solution-cells",
"--silent",
"--tags-to-hide=solution",
solution,
external=True,
silent=True,
)
session.log(f"overwriting {lesson}")
with open(lesson, "w") as fp:
fp.write(out)
@nox.session
def lint(session: nox.Session) -> None:
"""Look for lint."""
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files")
@nox.session(python=False)
def clean(session):
"""Remove all .venv's, build files and caches in the directory."""
root_folders = [".pytest_cache", ".venv", ".nox"] if not session.posargs else []
with session.chdir(ROOT):
for folder in root_folders:
session.log(f"rm -r {folder}")
shutil.rmtree(folder, ignore_errors=True)
for folder in _args_to_folders(session.posargs):
with session.chdir(folder):
for pattern in ["*.py[co]", "__pycache__"]:
session.log(f"rm {pattern}")
_clean_rglob(pattern)
clean_checkpoints(session)
@nox.session(python=False, name="clean-checkpoints")
def clean_checkpoints(session):
"""Remove jupyter notebook checkpoint files."""
for folder in _args_to_folders(session.posargs):
with session.chdir(folder):
_clean_rglob("*-checkpoint.ipynb")
_clean_rglob(".ipynb_checkpoints")
def _args_to_folders(args):
return [ROOT] if not args else [pathlib.Path(f) for f in args]
def _clean_rglob(pattern):
nox_dir = pathlib.Path(".nox")
for p in pathlib.Path(".").rglob(pattern):
if nox_dir in p.parents:
continue
if p.is_dir():
p.rmdir()
else:
p.unlink()