forked from SciTools/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
312 lines (257 loc) · 7.33 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
"""
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
#: Default to reusing any pre-existing nox environments.
nox.options.reuse_existing_virtualenvs = True
#: Name of the package to test.
PACKAGE = str("lib" / Path("iris"))
#: Cirrus-CI environment variable hook.
PY_VER = os.environ.get("PY_VER", "3.7")
#: Default cartopy cache directory.
CARTOPY_CACHE_DIR = os.environ.get("HOME") / Path(".local/share/cartopy")
def venv_cached(session):
"""
Determine whether the nox session environment has been cached.
Parameters
----------
session: object
A `nox.sessions.Session` object.
Returns
-------
bool
Whether the session has been cached.
"""
result = False
yml = Path(f"requirements/ci/py{PY_VER.replace('.', '')}.yml")
tmp_dir = Path(session.create_tmp())
cache = tmp_dir / yml.name
if cache.is_file():
with open(yml, "rb") as fi:
expected = hashlib.sha256(fi.read()).hexdigest()
with open(cache, "r") as fi:
actual = fi.read()
result = actual == expected
return result
def cache_venv(session):
"""
Cache the nox session environment.
This consists of saving a hexdigest (sha256) of the associated
conda requirements YAML file.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
yml = Path(f"requirements/ci/py{PY_VER.replace('.', '')}.yml")
with open(yml, "rb") as fi:
hexdigest = hashlib.sha256(fi.read()).hexdigest()
tmp_dir = Path(session.create_tmp())
cache = tmp_dir / yml.name
with open(cache, "w") as fo:
fo.write(hexdigest)
def cache_cartopy(session):
"""
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()",
)
@nox.session
def flake8(session):
"""
Perform flake8 linting of iris.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
# Pip install the session requirements.
session.install("flake8")
# Execute the flake8 linter on the package.
session.run("flake8", PACKAGE)
# Execute the flake8 linter on this file.
session.run("flake8", __file__)
@nox.session
def black(session):
"""
Perform black format checking of iris.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
# Pip install the session requirements.
session.install("black==20.8b1")
# Execute the black format checker on the package.
session.run("black", "--check", PACKAGE)
# Execute the black format checker on this file.
session.run("black", "--check", __file__)
@nox.session(python=[PY_VER], venv_backend="conda")
def tests(session):
"""
Perform iris system, integration and unit tests.
Parameters
----------
session: object
A `nox.sessions.Session` object.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
if not venv_cached(session):
# Determine the conda requirements yaml file.
fname = f"requirements/ci/py{PY_VER.replace('.', '')}.yml"
# Back-door approach to force nox to use "conda env update".
command = (
"conda",
"env",
"update",
f"--prefix={session.virtualenv.location}",
f"--file={fname}",
"--prune",
)
session._run(*command, silent=True, external="error")
cache_venv(session)
cache_cartopy(session)
session.run("python", "setup.py", "develop")
session.run(
"python",
"-m",
"iris.tests.runner",
"--default-tests",
"--system-tests",
)
@nox.session(python=[PY_VER], venv_backend="conda")
def gallery(session):
"""
Perform iris gallery doc-tests.
Parameters
----------
session: object
A `nox.sessions.Session` object.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
if not venv_cached(session):
# Determine the conda requirements yaml file.
fname = f"requirements/ci/py{PY_VER.replace('.', '')}.yml"
# Back-door approach to force nox to use "conda env update".
command = (
"conda",
"env",
"update",
f"--prefix={session.virtualenv.location}",
f"--file={fname}",
"--prune",
)
session._run(*command, silent=True, external="error")
cache_venv(session)
cache_cartopy(session)
session.run("python", "setup.py", "develop")
session.run(
"python",
"-m",
"iris.tests.runner",
"--gallery-tests",
)
@nox.session(python=[PY_VER], venv_backend="conda")
def doctest(session):
"""
Perform iris doc-tests.
Parameters
----------
session: object
A `nox.sessions.Session` object.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
if not venv_cached(session):
# Determine the conda requirements yaml file.
fname = f"requirements/ci/py{PY_VER.replace('.', '')}.yml"
# Back-door approach to force nox to use "conda env update".
command = (
"conda",
"env",
"update",
f"--prefix={session.virtualenv.location}",
f"--file={fname}",
"--prune",
)
session._run(*command, silent=True, external="error")
cache_venv(session)
cache_cartopy(session)
session.run("python", "setup.py", "develop")
session.cd("docs/iris")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"doctest",
external=True,
)
@nox.session(python=[PY_VER], venv_backend="conda")
def linkcheck(session):
"""
Perform iris doc link check.
Parameters
----------
session: object
A `nox.sessions.Session` object.
Notes
-----
See
- https://github.com/theacodes/nox/issues/346
- https://github.com/theacodes/nox/issues/260
"""
if not venv_cached(session):
# Determine the conda requirements yaml file.
fname = f"requirements/ci/py{PY_VER.replace('.', '')}.yml"
# Back-door approach to force nox to use "conda env update".
command = (
"conda",
"env",
"update",
f"--prefix={session.virtualenv.location}",
f"--file={fname}",
"--prune",
)
session._run(*command, silent=True, external="error")
cache_venv(session)
cache_cartopy(session)
session.run("python", "setup.py", "develop")
session.cd("docs/iris")
session.run(
"make",
"clean",
"html",
external=True,
)
session.run(
"make",
"linkcheck",
external=True,
)