This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
noxfile.py
240 lines (184 loc) · 6.57 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
"""
Perform test automation with nox.
For further details, see https://nox.thea.codes/en/stable/#
"""
from hashlib import sha256
import os
from pathlib import Path
from shutil import rmtree
from urllib.request import urlretrieve
from zipfile import ZipFile
import nox
#: Default to reusing any pre-existing nox environments.
nox.options.reuse_existing_virtualenvs = True
#: Name of the package to test.
PACKAGE = Path("iris_ugrid").absolute()
#: Cirrus-CI environment variable hook.
PY_VER = os.environ.get("PY_VER", "3.7")
# Git commit of iris that iris-ugrid depends on.
with Path("requirements").joinpath("manual", "iris_commit.txt").open() as fi:
IRIS_COMMIT = fi.read().strip()
def venv_cached(session, cache_info_path, env_spec_path, iris_commit):
"""
Determine whether the nox session environment has been cached.
Parameters
----------
session: object
A `nox.sessions.Session` object.
cache_info_path: Path
A Path object pointing to the expected directory that would contain
cache info.
env_spec_path: pathlib.Path
A Path object pointing to the conda env spec YAML for Iris-ugrid.
iris_commit : str
The string for the Iris commit Iris-ugrid is dependent on.
Returns
-------
bool
Whether the session has been cached.
"""
result = False
cache_env_spec = cache_info_path / env_spec_path.name
cache_iris_commit = cache_info_path / "iris-commit"
caches_found = all(
[file.is_file() for file in (cache_env_spec, cache_iris_commit)]
)
if caches_found:
with env_spec_path.open("rb") as fi:
expected = sha256(fi.read()).hexdigest()
with cache_env_spec.open("r") as fi:
actual = fi.read()
ok_env_spec = actual == expected
expected = iris_commit
with cache_iris_commit.open("r") as fi:
actual = fi.read()
ok_iris_commit = actual == expected
result = ok_env_spec and ok_iris_commit
return result
def cache_venv(session, cache_info_path, env_spec_path, iris_commit):
"""
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.
cache_info_path: pathlib.Path
A Path object denoting the directory that cache info should be written
to.
env_spec_path: pathlib.Path
A Path object pointing to the conda env spec YAML for Iris-ugrid.
iris_commit: str
The string for the Iris commit Iris-ugrid is dependent on.
"""
if not cache_info_path.is_dir():
cache_info_path.mkdir()
with env_spec_path.open("rb") as fi:
hexdigest = sha256(fi.read()).hexdigest()
cache_env_spec = cache_info_path / env_spec_path.name
with cache_env_spec.open("w+") as fo:
fo.write(hexdigest)
cache_iris_commit = cache_info_path / "iris-commit"
with cache_iris_commit.open("w+") as fo:
fo.write(iris_commit)
@nox.session
def flake8(session):
"""
Perform flake8 linting of iris-ugrid.
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", str(PACKAGE))
# Execute the flake8 linter on this file.
session.run("flake8", __file__)
@nox.session
def black(session):
"""
Perform black format checking of iris-ugrid.
Parameters
----------
session: object
A `nox.sessions.Session` object.
"""
# Pip install the session requirements.
session.install("black==19.10b0")
# Execute the black format checker on the package.
session.run("black", "--check", str(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-ugrid 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
"""
INSTALL_DIR = Path().cwd().absolute()
env_spec_self = (
INSTALL_DIR
/ "requirements"
/ "ci"
/ f"py{PY_VER.replace('.', '')}.yml"
)
IRIS_DIR = Path(session.virtualenv.location) / "iris"
cache_info_path = Path(session.virtualenv.location) / "nox_cache_info"
if not venv_cached(session, cache_info_path, env_spec_self, IRIS_COMMIT):
def conda_env_update(env_spec_path):
# Back-door approach to force nox to use "conda env update".
command = (
f"conda env update --prefix={session.virtualenv.location} "
f"--file={env_spec_path}"
)
command = command.split(" ")
session._run(*command, silent=True, external="error")
# Download Iris.
github_archive_url = (
f"https://github.com/SciTools/iris/archive/{IRIS_COMMIT}.zip"
)
iris_zip = Path(urlretrieve(github_archive_url, "iris.zip")[0])
with ZipFile(iris_zip, "r") as zip_open:
zip_open.extractall()
if IRIS_DIR.is_dir():
rmtree(IRIS_DIR)
Path(f"iris-{IRIS_COMMIT}").rename(IRIS_DIR)
iris_zip.unlink()
# Install Iris dependencies.
env_spec_iris = (
IRIS_DIR
/ "requirements"
/ "ci"
/ f"py{PY_VER.replace('.', '')}.yml"
)
conda_env_update(env_spec_iris)
# Configure Iris.
site_cfg_content = [
"[Resources]",
f"test_data_dir = {os.environ['IRIS_TEST_DATA_DIR']}/test_data",
f"doc_dir = {IRIS_DIR / 'docs' / 'iris'}",
"[System]",
f"udunits2_path = {session.virtualenv.location}/lib/libudunits2.so",
]
site_cfg_path = IRIS_DIR / "lib" / "iris" / "etc" / "site.cfg"
with site_cfg_path.open("w+") as site_cfg:
site_cfg.writelines(line + "\n" for line in site_cfg_content)
# Install Iris.
os.chdir(IRIS_DIR)
session.run(*"python setup.py install".split(" "), silent=True)
#######################################################################
# Install dependencies.
conda_env_update(env_spec_self)
cache_venv(session, cache_info_path, env_spec_self, IRIS_COMMIT)
session.run("pytest", "-v", str(PACKAGE))