-
Notifications
You must be signed in to change notification settings - Fork 283
/
noxfile.py
317 lines (250 loc) · 8.51 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
"""Perform test automation with nox.
For further details, see https://nox.thea.codes/en/stable/#
"""
import hashlib
import os
from pathlib import Path
import nox
from nox.logger import logger
#: Default to reusing any pre-existing nox environments.
nox.options.reuse_existing_virtualenvs = True
#: Python versions we can run sessions under
_PY_VERSIONS_ALL = ["3.10", "3.11", "3.12"]
_PY_VERSION_LATEST = _PY_VERSIONS_ALL[-1]
#: One specific python version for docs builds
_PY_VERSION_DOCSBUILD = _PY_VERSION_LATEST
#: Cirrus-CI environment variable hook.
PY_VER = os.environ.get("PY_VER", _PY_VERSIONS_ALL)
#: Default cartopy cache directory.
CARTOPY_CACHE_DIR = os.environ.get("HOME") / Path(".local/share/cartopy")
# https://github.com/numpy/numpy/pull/19478
# https://github.com/matplotlib/matplotlib/pull/22099
#: Common session environment variables.
ENV = dict(NPY_DISABLE_CPU_FEATURES="AVX512F,AVX512CD,AVX512_SKX")
def session_lockfile(session: nox.sessions.Session) -> Path:
"""Return the path of the session lockfile."""
return Path(f"requirements/locks/py{session.python.replace('.', '')}-linux-64.lock")
def session_cachefile(session: nox.sessions.Session) -> Path:
"""Return the path of the session lockfile cache."""
lockfile = session_lockfile(session)
tmp_dir = Path(session.create_tmp())
cache = tmp_dir / lockfile.name
return cache
def venv_populated(session: nox.sessions.Session) -> bool:
"""List of packages in the lockfile installed.
Returns True if the conda venv has been created.
"""
return session_cachefile(session).is_file()
def venv_changed(session: nox.sessions.Session) -> bool:
"""Return True if the installed session is different.
Compares to that specified in the lockfile.
"""
changed = False
cache = session_cachefile(session)
lockfile = session_lockfile(session)
if cache.is_file():
with open(lockfile, "rb") as fi:
expected = hashlib.sha256(fi.read()).hexdigest()
with open(cache, "r") as fi:
actual = fi.read()
changed = actual != expected
return changed
def cache_venv(session: nox.sessions.Session) -> None:
"""Cache the nox session environment.
This consists of saving a hexdigest (sha256) of the associated
conda lock file.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
lockfile = session_lockfile(session)
cache = session_cachefile(session)
with open(lockfile, "rb") as fi:
hexdigest = hashlib.sha256(fi.read()).hexdigest()
with open(cache, "w") as fout:
fout.write(hexdigest)
def cache_cartopy(session: nox.sessions.Session) -> None:
"""Determine whether to cache the cartopy natural earth shapefiles.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
if not CARTOPY_CACHE_DIR.is_dir():
session.run_always(
"python",
"-c",
"import cartopy; cartopy.io.shapereader.natural_earth()",
)
def prepare_venv(session: nox.sessions.Session) -> None:
"""Create and cache the nox session conda environment.
Additionally provide conda environment package details and info.
Note that, iris is installed into the environment using pip.
Parameters
----------
session : object
A `nox.sessions.Session` object.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
lockfile = session_lockfile(session)
venv_dir = session.virtualenv.location_name
if not venv_populated(session):
# environment has been created but packages not yet installed
# populate the environment from the lockfile
logger.debug(f"Populating conda env at {venv_dir}")
session.conda_install("--file", str(lockfile))
cache_venv(session)
elif venv_changed(session):
# destroy the environment and rebuild it
logger.debug(f"Lockfile changed. Re-creating conda env at {venv_dir}")
_re_orig = session.virtualenv.reuse_existing
session.virtualenv.reuse_existing = False
session.virtualenv.create()
session.conda_install("--file", str(lockfile))
session.virtualenv.reuse_existing = _re_orig
cache_venv(session)
logger.debug(f"Environment {venv_dir} is up to date")
cache_cartopy(session)
# Determine whether verbose diagnostics have been requested
# from the command line.
verbose = "-v" in session.posargs or "--verbose" in session.posargs
if verbose:
session.run_always("conda", "info")
session.run_always("conda", "list", f"--prefix={venv_dir}")
session.run_always(
"conda",
"list",
f"--prefix={venv_dir}",
"--explicit",
)
@nox.session(python=PY_VER, venv_backend="conda")
def tests(session: nox.sessions.Session):
"""Perform iris system, integration and unit tests.
Coverage testing is enabled if the "--coverage" or "-c" flag is used.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.install("--no-deps", "--editable", ".")
session.env.update(ENV)
run_args = [
"pytest",
"-n",
"auto",
"lib/iris/tests",
]
if "-c" in session.posargs or "--coverage" in session.posargs:
run_args[-1:-1] = ["--cov=lib/iris", "--cov-report=xml"]
session.run(*run_args)
@nox.session(python=_PY_VERSION_DOCSBUILD, venv_backend="conda")
def doctest(session: nox.sessions.Session):
"""Perform iris doctests and gallery.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.install("--no-deps", "--editable", ".")
session.env.update(ENV)
session.cd("docs")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"doctest",
external=True,
)
@nox.session(python=_PY_VERSION_DOCSBUILD, venv_backend="conda")
def gallery(session: nox.sessions.Session):
"""Perform iris gallery doc-tests.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.install("--no-deps", "--editable", ".")
session.env.update(ENV)
session.run(
"pytest",
"-n",
"auto",
"docs/gallery_tests",
)
@nox.session(python=_PY_VERSION_DOCSBUILD, venv_backend="conda")
def linkcheck(session: nox.sessions.Session):
"""Perform iris doc link check.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.install("--no-deps", "--editable", ".")
session.cd("docs")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"linkcheck",
external=True,
)
@nox.session(python=PY_VER, venv_backend="conda")
def wheel(session: nox.sessions.Session):
"""Perform iris local wheel install and import test.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.cd("dist")
fname = list(Path(".").glob("scitools_iris-*.whl"))
if len(fname) == 0:
raise ValueError("Cannot find wheel to install.")
if len(fname) > 1:
emsg = f"Expected to find 1 wheel to install, found {len(fname)} instead."
raise ValueError(emsg)
session.install(fname[0].name)
session.run(
"python",
"-c",
"import iris; print(f'{iris.__version__=}')",
external=True,
)
@nox.session
def benchmarks(session: nox.sessions.Session):
"""Run the Iris benchmark runner. Run session with `-- --help` for help.
Parameters
----------
session : object
A `nox.sessions.Session` object.
"""
if len(session.posargs) == 0:
message = (
"This session MUST be run with at least one argument. The "
"arguments are passed down to the benchmark runner script. E.g:\n"
"nox -s benchmarks -- --help\n"
"nox -s benchmarks -- something --help\n"
"nox -s benchmarks -- something\n"
)
session.error(message)
session.install("asv", "nox")
bm_runner_path = Path(__file__).parent / "benchmarks" / "bm_runner.py"
session.run("python", bm_runner_path, *session.posargs)