-
Notifications
You must be signed in to change notification settings - Fork 16
/
noxfile.py
275 lines (205 loc) · 8.78 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
import json
import os
import pathlib
import shutil
import tarfile
import urllib.request
import nox
DIR = pathlib.Path(__file__).parent.resolve()
VENV_DIR = pathlib.Path('./.venv').resolve()
with pathlib.Path('./tests/vak.tests.config.json').open('rb') as fp:
VAK_TESTS_CONFIG = json.load(fp)
nox.options.sessions = ['test', 'coverage']
@nox.session
def build(session: nox.Session) -> None:
"""
Build an SDist and wheel with ``flit``.
"""
dist_dir = DIR.joinpath("dist")
if dist_dir.exists():
shutil.rmtree(dist_dir)
session.install(".[dev]")
session.run("flit", "build")
@nox.session(python="3.10.7")
def dev(session: nox.Session) -> None:
"""
Sets up a python development environment for the project.
This session will:
- Create a python virtualenv for the session
- Install the `virtualenv` cli tool into this environment
- Use `virtualenv` to create a global project virtual environment
- Invoke the python interpreter from the global project environment to install
the project and all it's development dependencies.
"""
session.install("virtualenv")
# VENV_DIR here is a pathlib.Path location of the project virtualenv
# e.g. .venv
session.run("virtualenv", os.fsdecode(VENV_DIR), silent=True)
python = os.fsdecode(VENV_DIR.joinpath("bin/python"))
# Use the venv's interpreter to install the project along with
# all it's dev dependencies, this ensures it's installed in the right way
session.run(python, "-m", "pip", "install", "-e", ".[dev,test,doc]", external=True)
@nox.session(python="3.10")
def lint(session):
"""
Run the linter.
"""
session.install("isort", "black", "flake8")
# run isort first since black disagrees with it
session.run("isort", "./src")
session.run("black", "./src", "--line-length=79")
session.run("flake8", "./src", "--max-line-length", "120")
TEST_PYTHONS = [
"3.10",
"3.11",
"3.12",
]
@nox.session(python=TEST_PYTHONS)
def test(session) -> None:
"""
Run the unit and regular tests.
"""
session.install(".[test]")
if session.posargs:
session.run("pytest", *session.posargs)
else:
session.run("pytest", "-x", "--slow-last")
@nox.session
def coverage(session) -> None:
"""
Run the unit and regular tests, and save coverage report
"""
session.install(".[test]")
session.run(
"pytest", "--slow-last", "--cov=./", "--cov-report=xml", *session.posargs
)
@nox.session
def doc(session: nox.Session) -> None:
"""
Build the docs.
To run ``sphinx-autobuild``, do:
.. code-block::console
nox -s doc -- autobuild
Otherwise the docs will be built once using
"""
session.install(".[doc]")
if session.posargs:
if "autobuild" in session.posargs:
print("Building docs at http://127.0.0.1:8000 with sphinx-autobuild -- use Ctrl-C to quit")
session.run("sphinx-autobuild", "doc", "doc/_build/html")
else:
print("Unsupported argument to docs")
else:
session.run("sphinx-build", "-nW", "--keep-going", "-b", "html", "doc/", "doc/_build/html")
# ---- sessions below this all have to do with data for tests ----------------------------------------------------
def clean_dir(dir_path):
"""Helper function that "cleans" a directory by removing all files
(that are not hidden) without removing the directory itself."""
dir_path = pathlib.Path(dir_path)
dir_contents = dir_path.glob('*')
for content in dir_contents:
if content.is_dir():
shutil.rmtree(content)
else:
if content.name.startswith('.'):
# e.g., .gitkeep file we don't want to delete
continue
content.unlink()
DATA_FOR_TESTS_DIR = './tests/data_for_tests/'
SOURCE_TEST_DATA_DIR = f"{DATA_FOR_TESTS_DIR}source/"
SOURCE_TEST_DATA_DIRS = [
dir_ for dir_
in sorted(pathlib.Path(SOURCE_TEST_DATA_DIR).glob('*/'))
if dir_.is_dir()
]
@nox.session(name='test-data-clean-source')
def test_data_clean_source(session) -> None:
"""Clean (remove) 'source' test data, used by TEST_DATA_GENERATE_SCRIPT."""
clean_dir(SOURCE_TEST_DATA_DIR)
def copy_url(url: str, path: str) -> None:
"""Copy data from a url to a local file."""
urllib.request.urlretrieve(url, path)
SOURCE_TEST_DATA_URL = 'https://osf.io/2ehbp/download'
SOURCE_TEST_DATA_TAR = f'{SOURCE_TEST_DATA_DIR}source_test_data-version-1.x.tar.gz'
@nox.session(name='test-data-tar-source')
def test_data_tar_source(session) -> None:
"""Make a .tar.gz file of just the 'source' test data used to run tests."""
session.log(f"Making tarfile with source data: {SOURCE_TEST_DATA_TAR}")
make_tarfile(SOURCE_TEST_DATA_TAR, SOURCE_TEST_DATA_DIRS)
@nox.session(name='test-data-download-source')
def test_data_download_source(session) -> None:
"""Download and extract a .tar.gz file of 'source' test data, used by TEST_DATA_GENERATE_SCRIPT."""
session.log(f'Downloading: {SOURCE_TEST_DATA_URL}')
copy_url(url=SOURCE_TEST_DATA_URL, path=SOURCE_TEST_DATA_TAR)
session.log(f'Extracting downloaded tar: {SOURCE_TEST_DATA_TAR}')
with tarfile.open(SOURCE_TEST_DATA_TAR, "r:gz") as tf:
tf.extractall(path='.')
TEST_DATA_GENERATE_SCRIPT = './tests/scripts/generate_data_for_tests.py'
@nox.session(name='test-data-generate', python="3.10")
def test_data_generate(session) -> None:
"""Produced 'generated' test data, by running TEST_DATA_GENERATE_SCRIPT on 'source' test data."""
session.install(".[test]")
session.run("python", TEST_DATA_GENERATE_SCRIPT)
GENERATED_TEST_DATA_DIR = f'{DATA_FOR_TESTS_DIR}generated/'
@nox.session(name='test-data-clean-generated')
def test_data_clean_generated(session) -> None:
"""Clean (remove) 'generated' test data."""
clean_dir(GENERATED_TEST_DATA_DIR)
def make_tarfile(name: str, to_add: list):
"""Helper function that makes a tarfile"""
with tarfile.open(name, "w:gz") as tf:
for add_name in to_add:
tf.add(name=add_name)
CONFIGS_DIR = f'{GENERATED_TEST_DATA_DIR}configs'
PREP_DIR = f'{GENERATED_TEST_DATA_DIR}prep/'
RESULTS_DIR = f'{GENERATED_TEST_DATA_DIR}results/'
PREP_CI: list = []
for model_name in VAK_TESTS_CONFIG['models']:
PREP_CI.extend(
sorted(
pathlib.Path(PREP_DIR).glob(f'*/*/{model_name}')
)
)
RESULTS_CI: list = []
for model_name in VAK_TESTS_CONFIG['models']:
PREP_CI.extend(
sorted(
pathlib.Path(RESULTS_DIR).glob(f'*/*/{model_name}')
)
)
GENERATED_TEST_DATA_CI_TAR = f'{GENERATED_TEST_DATA_DIR}generated_test_data-version-1.x.ci.tar.gz'
GENERATED_TEST_DATA_CI_DIRS = [CONFIGS_DIR] + PREP_CI + RESULTS_CI
GENERATED_TEST_DATA_ALL_TAR = f'{GENERATED_TEST_DATA_DIR}generated_test_data-version-1.x.tar.gz'
GENERATED_TEST_DATA_ALL_DIRS = [CONFIGS_DIR, PREP_DIR, RESULTS_DIR]
@nox.session(name='test-data-tar-generated-all')
def test_data_tar_generated_all(session) -> None:
"""Make a .tar.gz file of all 'generated' test data."""
session.log(f"Making tarfile with all generated data: {GENERATED_TEST_DATA_ALL_TAR}")
make_tarfile(GENERATED_TEST_DATA_ALL_TAR, GENERATED_TEST_DATA_ALL_DIRS)
@nox.session(name='test-data-tar-generated-ci')
def test_data_tar_generated_ci(session) -> None:
"""Make a .tar.gz file of just the 'generated' test data used to run tests on CI."""
session.log(f"Making tarfile with generated data for CI: {GENERATED_TEST_DATA_CI_TAR}")
make_tarfile(GENERATED_TEST_DATA_CI_TAR, GENERATED_TEST_DATA_CI_DIRS)
GENERATED_TEST_DATA_ALL_URL = 'https://osf.io/xfp6n/download'
@nox.session(name='test-data-download-generated-all')
def test_data_download_generated_all(session) -> None:
"""Download and extract a .tar.gz file of all 'generated' test data"""
session.install("pandas")
session.log(f'Downloading: {GENERATED_TEST_DATA_ALL_URL}')
copy_url(url=GENERATED_TEST_DATA_ALL_URL, path=GENERATED_TEST_DATA_ALL_TAR)
session.log(f'Extracting downloaded tar: {GENERATED_TEST_DATA_ALL_TAR}')
with tarfile.open(GENERATED_TEST_DATA_ALL_TAR, "r:gz") as tf:
tf.extractall(path='.')
session.log('Fixing paths in .csv files')
session.install("pandas")
GENERATED_TEST_DATA_CI_URL = 'https://osf.io/un2zs/download'
@nox.session(name='test-data-download-generated-ci')
def test_data_download_generated_ci(session) -> None:
"""Download and extract a .tar.gz file of just the 'generated' test data used to run tests on CI"""
session.install("pandas")
session.log(f'Downloading: {GENERATED_TEST_DATA_CI_URL}')
copy_url(url=GENERATED_TEST_DATA_CI_URL, path=GENERATED_TEST_DATA_CI_TAR)
session.log(f'Extracting downloaded tar: {GENERATED_TEST_DATA_CI_TAR}')
with tarfile.open(GENERATED_TEST_DATA_CI_TAR, "r:gz") as tf:
tf.extractall(path='.')