forked from SciTools/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
executable file
·327 lines (261 loc) · 8.6 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
"""
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.7", "3.8"]
_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")
def session_lockfile(session: nox.sessions.Session) -> Path:
"""Return the path of the session lockfile."""
return Path(
f"requirements/ci/nox.lock/py{session.python.replace('.', '')}-linux-64.lock"
)
def session_cachefile(session: nox.sessions.Session) -> Path:
"""Returns 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:
"""Returns True if the conda venv has been created
and the list of packages in the lockfile installed."""
return session_cachefile(session).is_file()
def venv_changed(session: nox.sessions.Session) -> bool:
"""Returns True if the installed session is different 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 fo:
fo.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, and 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
def precommit(session: nox.sessions.Session):
"""
Perform pre-commit hooks of iris codebase.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
import yaml
# Pip install the session requirements.
session.install("pre-commit")
# Load the pre-commit configuration YAML file.
with open(".pre-commit-config.yaml", "r") as fi:
config = yaml.load(fi, Loader=yaml.FullLoader)
# List of pre-commit hook ids that we don't want to run.
excluded = ["no-commit-to-branch"]
# Enumerate the ids of pre-commit hooks we do want to run.
ids = [
hook["id"]
for entry in config["repos"]
for hook in entry["hooks"]
if hook["id"] not in excluded
]
# Execute the pre-commit hooks.
[session.run("pre-commit", "run", "--all-files", id) for id in ids]
@nox.session(python=PY_VER, venv_backend="conda")
def tests(session: nox.sessions.Session):
"""
Perform iris system, integration and unit tests.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.install("--no-deps", "--editable", ".")
session.run(
"python",
"-m",
"iris.tests.runner",
"--default-tests",
"--system-tests",
)
@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.cd("docs")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"doctest",
external=True,
)
session.cd("..")
session.run(
"python",
"-m",
"iris.tests.runner",
"--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[-1], venv_backend="conda")
@nox.parametrize(
["ci_mode"],
[True, False],
ids=["ci compare", "full"],
)
def benchmarks(session: nox.sessions.Session, ci_mode: bool):
"""
Perform esmf-regrid performance benchmarks (using Airspeed Velocity).
Parameters
----------
session: object
A `nox.sessions.Session` object.
ci_mode: bool
Run a cut-down selection of benchmarks, comparing the current commit to
the last commit for performance regressions.
Notes
-----
ASV is set up to use ``nox --session=tests --install-only`` to prepare
the benchmarking environment. This session environment must use a Python
version that is also available for ``--session=tests``.
"""
session.install("asv", "nox")
session.cd("benchmarks")
# Skip over setup questions for a new machine.
session.run("asv", "machine", "--yes")
def asv_exec(*sub_args: str) -> None:
run_args = ["asv", *sub_args]
session.run(*run_args)
if ci_mode:
# If on a PR: compare to the base (target) branch.
# Else: compare to previous commit.
previous_commit = os.environ.get("PR_BASE_SHA", "HEAD^1")
try:
asv_exec("continuous", "--factor=1.2", previous_commit, "HEAD")
finally:
asv_exec("compare", previous_commit, "HEAD")
else:
# f5ceb808 = first commit supporting nox --install-only .
asv_exec("run", "f5ceb808..HEAD")