-
Notifications
You must be signed in to change notification settings - Fork 43
/
noxfile.py
333 lines (263 loc) · 8.58 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
# Copyright iris-grib contributors
#
# This file is part of iris-grib and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""
Perform test automation with nox.
For further details, see https://nox.thea.codes/en/stable/#
"""
from contextlib import contextmanager
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
#: Name of the package to test.
PACKAGE = str("iris_grib")
#: Cirrus-CI environment variable hook.
PY_VER = os.environ.get("PY_VER", ["3.10", "3.11", "3.12"])
IRIS_SOURCE = os.environ.get("IRIS_SOURCE", ["source", "conda-forge"])
#: Default cartopy cache directory.
CARTOPY_CACHE_DIR = os.environ.get("HOME") / Path(".local/share/cartopy")
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(
"python",
"-c",
"import cartopy; cartopy.io.shapereader.natural_earth()",
)
def _write_iris_config(session: nox.sessions.Session) -> None:
"""
Add test data dir and libudunits2.so to iris config.
test data dir is set from session pos args. i.e. can be
configured by passing in on the command line:
nox --session tests -- --test-data-dir $TEST_DATA_DIR/test_data
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
try:
test_data_dir = session.posargs[session.posargs.index("--test-data-dir") + 1]
except Exception:
test_data_dir = ""
iris_config_file = os.path.join(
session.virtualenv.location,
"lib",
f"python{session.python}",
"site-packages",
"iris",
"etc",
"site.cfg",
)
iris_config = f"""
[Resources]
test_data_dir = {test_data_dir}
[System]
udunits2_path = {
os.path.join(session.virtualenv.location,'lib', 'libudunits2.so')
}
"""
print("Iris config\n-----------")
print(iris_config)
with open(iris_config_file, "w") as f:
f.write(iris_config)
def _session_lockfile(session: nox.sessions.Session) -> Path:
"""
Return the path of the session lockfile for the relevant python string
e.g ``py38``.
"""
lockfile_name = f"py{session.python.replace('.', '')}-linux-64.lock"
return Path("requirements/locks") / lockfile_name
def _file_content(file_path: Path) -> str:
with file_path.open("r") as file:
return file.read()
def _session_cachefile(session: nox.sessions.Session) -> Path:
"""Return the path of the session lockfile cache."""
tmp_dir = Path(session.create_tmp())
cache = tmp_dir / _session_lockfile(session).name
return cache
def _venv_populated(session: nox.sessions.Session) -> bool:
"""
Return 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:
"""
Return True if the installed session is different to that specified in the
lockfile.
"""
result = False
if _venv_populated(session):
expected = _file_content(_session_lockfile(session))
actual = _file_content(_session_cachefile(session))
result = actual != expected
return result
def _install_and_cache_venv(session: nox.sessions.Session) -> None:
"""
Install and 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)
session.conda_install(f"--file={lockfile}")
with open(lockfile, "rb") as fi:
hexdigest = hashlib.sha256(fi.read()).hexdigest()
with _session_cachefile(session).open("w") as cachefile:
cachefile.write(hexdigest)
@contextmanager
def prepare_venv(
session: nox.sessions.Session, iris_source: str = "conda-forge"
) -> None:
"""
Create and cache the nox session conda environment, and additionally
provide conda environment package details and info.
Note that, iris-grib is installed into the environment using pip.
Parameters
----------
session: object
A `nox.sessions.Session` object.
iris_source: str
Determines where Iris was sourced from. Either 'conda-forge' (the
default), or 'source' which refers to the Iris main branch.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
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: {venv_dir}")
_install_and_cache_venv(session)
elif _venv_changed(session):
# Destroy the environment and rebuild it.
logger.debug(f"Lockfile changed. Recreating conda env: {venv_dir}")
_reuse_original = session.virtualenv.reuse_existing
session.virtualenv.reuse_existing = False
session.virtualenv.create()
_install_and_cache_venv(session)
session.virtualenv.reuse_existing = _reuse_original
logger.debug(f"Environment up to date: {venv_dir}")
if iris_source == "source":
# get latest iris
iris_dir = f"{session.create_tmp()}/iris"
if os.path.exists(iris_dir):
# cached. update by pulling from origin/master
session.run(
"git",
"-C",
iris_dir,
"pull",
"origin",
"main",
external=True, # use git from host environment
)
else:
session.run(
"git",
"clone",
"https://github.com/scitools/iris.git",
iris_dir,
external=True,
)
session.install(iris_dir, "--no-deps")
_cache_cartopy(session)
_write_iris_config(session)
# Install the iris-grib source in develop mode.
session.install("--no-deps", "--editable", ".")
# 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("conda", "info")
session.run("conda", "list", f"--prefix={venv_dir}")
session.run(
"conda",
"list",
f"--prefix={venv_dir}",
"--explicit",
)
@nox.session(python=PY_VER, venv_backend="conda")
@nox.parametrize("iris_source", IRIS_SOURCE)
def tests(session: nox.sessions.Session, iris_source: str):
"""
Perform iris-grib tests against release and development versions of iris.
Parameters
----------
session: object
A `nox.sessions.Session` object.
iris_source: str
Either 'conda-forge' if using Iris from conda-forge, or 'source' if
installing Iris from the Iris' main branch.
"""
prepare_venv(session, iris_source)
session.run("python", "-m", "eccodes", "selfcheck")
run_args = [
"pytest",
"--pyargs",
"iris_grib",
]
if "-c" in session.posargs or "--coverage" in session.posargs:
run_args.extend(["--cov=iris_grib", "--cov-report=xml"])
session.run(*run_args)
@nox.session(python=PY_VER, venv_backend="conda")
def doctest(session: nox.sessions.Session):
"""
Perform iris-grib doc-tests.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.cd("docs")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"doctest",
external=True,
)
@nox.session(python=PY_VER, venv_backend="conda")
def linkcheck(session: nox.sessions.Session):
"""
Perform iris-grib doc link check.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
prepare_venv(session)
session.cd("docs")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"linkcheck",
external=True,
)