-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
373 lines (318 loc) · 11.4 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""Automation module to run from documentation builds to tests."""
import contextlib
from functools import partial
from pathlib import Path
from shutil import rmtree
from typing import Callable, Dict, Iterator, List
import nox
from nox.command import CommandFailed
from nox.sessions import Session
from settings import (
FIGURES_DIR_PATH,
FILTER_DIR_PATH,
REPORT_DIR_PATH,
REPORT_NAME,
REQUIREMENTS_DIR_PATH,
ROOT,
)
# Configure nox
nox.options.sessions = ["test-docs", "test-src", "lint"]
nox.options.reuse_existing_virtualenvs = True
nox.options.default_venv_backend = "venv"
# Globals
form_requirements = ["-r", str(REQUIREMENTS_DIR_PATH / "format.txt")]
test_requirements = ["-r", str(REQUIREMENTS_DIR_PATH / "tests.txt")]
docs_requirements = ["-r", str(REQUIREMENTS_DIR_PATH / "docs.txt")]
prod_requirements = ["-r", str(REQUIREMENTS_DIR_PATH / "prod.txt")]
mlp_frameworks_requirements = [
"-r",
str(REQUIREMENTS_DIR_PATH / "mlp_frameworks.txt"),
]
hp_optimization_requirements = [
"-r",
str(REQUIREMENTS_DIR_PATH / "hp_optimization.txt"),
]
python_files = [
"noxfile.py",
str(FILTER_DIR_PATH / "filters.py"),
"src",
"tests",
]
# -----------------------------------------------------------------------------
# Cleaning
# -----------------------------------------------------------------------------
def show_help(session: Session, help_dict: Dict[str, str]) -> None:
"""Process the extra arguments for a session.
:param session: current session.
:param help: arguments help for the curent session.
"""
if "help" in session.posargs:
session.log("=" * 40)
session.log("Function posargs:")
for argument, description in help_dict.items():
session.log(f"\t- {argument}: {description}")
session.log("=" * 40)
session.skip()
def remove_files(
path_list: List[Path], func_logger: Callable = print,
) -> None:
"""Remove the list of paths provided.
:param path_list: :class:`Path` list to remove.
:param func_logger: print function to use.
"""
def remove_file(path: Path, func_logger: Callable = print):
"""Remove provided path.
:param path: :class:`Path` instance to remove.
:param func_logger: print function to use.
"""
try:
func_logger(f"Trying to remove '{path}'...")
if path.is_file():
path.unlink()
else:
rmtree(path)
except FileNotFoundError as error:
func_logger(
f"Couldn't remove '{error.filename}'. File/directory not found"
)
else:
func_logger("File/directory removed succesfully")
list(map(partial(remove_file, func_logger=func_logger), path_list,))
@nox.session(name="clean-py")
def clean_py(session: Session) -> None:
"""Celean python cache files.
If ``verbose`` is provided in :attr:`Session.posargs`, each file removal
will be logged. Nothing will print otherwise.
"""
show_help(session, {"verbose": "Show each deleted file."})
lggr = session.log if "verbose" in session.posargs else lambda *args: None
session.log("Cleaning global unwanted files...")
remove_files(
[
*list(ROOT.glob("**/*.py[cod]")),
*list(ROOT.glob("**/__pycache__/")),
*list(ROOT.glob("**/.pytest_cache/")),
*list(ROOT.glob("**/.mypy_cache/")),
],
lggr,
)
@nox.session(name="clean-docs")
def clean_docs(session: Session) -> None:
"""Clean doc construction.
If ``verbose`` is provided in :attr:`Session.posargs`, each file removal
will be logged. Nothing will print otherwise.
"""
show_help(session, {"verbose": "Show each deleted file."})
lggr = session.log if "verbose" in session.posargs else lambda *args: None
session.log("Cleaning latex and pweave files...")
remove_files(
[
*list(ROOT.glob("**/_minted-*/")),
*list(REPORT_DIR_PATH.glob("_*")),
*list(REPORT_DIR_PATH.glob("**/*.pkl")),
*list(REPORT_DIR_PATH.glob("secciones/*.tex")),
*list(FIGURES_DIR_PATH.glob("*.[!tex]*")),
REPORT_DIR_PATH / "presentation.nav",
REPORT_DIR_PATH / "presentation.snm",
REPORT_DIR_PATH / "figures_pweave",
REPORT_DIR_PATH / f"{REPORT_NAME}.bbl",
REPORT_DIR_PATH / f"{REPORT_NAME}.blg",
REPORT_DIR_PATH / f"{REPORT_NAME}.txt",
REPORT_DIR_PATH / f"{REPORT_NAME}.aux",
REPORT_DIR_PATH / f"{REPORT_NAME}.log",
REPORT_DIR_PATH / f"{REPORT_NAME}.out",
REPORT_DIR_PATH / f"{REPORT_NAME}.lof",
REPORT_DIR_PATH / f"{REPORT_NAME}.lot",
REPORT_DIR_PATH / f"{REPORT_NAME}.toc",
REPORT_DIR_PATH / f"{REPORT_NAME}.fls",
REPORT_DIR_PATH / f"{REPORT_NAME}.fdb_latexmk",
REPORT_DIR_PATH / f"{REPORT_NAME}.pdf",
REPORT_DIR_PATH / f"{REPORT_NAME}/",
],
lggr,
)
# -----------------------------------------------------------------------------
# Docs
# -----------------------------------------------------------------------------
@contextlib.contextmanager
def chdir(session: Session, dir_path: Path) -> Iterator[Path]:
"""Temporarily chdir when entering CM and chdir back on exit."""
orig_dir = Path.cwd()
session.chdir(str(dir_path))
try:
yield dir_path
finally:
session.chdir(str(orig_dir))
@nox.session(name="build-latex")
def build_latex(session: Session, clean: bool = False) -> None:
"""Create tex files from Pweave sources."""
session.log("Building latex files and figures through pweave ...")
session.install(*docs_requirements, *prod_requirements)
fig_dir = str(REPORT_DIR_PATH / "figures_pweave")
if clean:
clean_docs(session)
with chdir(session, ROOT):
for pweave_file in REPORT_DIR_PATH.glob("**/*.texw"):
session.run(
"pweave",
"--format=texminted",
"--documentation-mode",
f"--figure-directory={fig_dir}",
f"--output={str(pweave_file.with_suffix('.tex'))}",
str(pweave_file),
silent=True,
)
session.log("Creating plain documentation from latex ...")
plain_report_path = str(REPORT_DIR_PATH / f"{REPORT_NAME}.txt")
filters_path = FILTER_DIR_PATH / "filters.py"
pandoc_args = [
str(REPORT_DIR_PATH / f"{REPORT_NAME}.tex"),
f"--output={plain_report_path}",
"--from=latex",
"--to=plain",
]
if filters_path.exists():
pandoc_args.append(f"--filter={str(filters_path)}")
with chdir(session, REPORT_DIR_PATH):
session.run("pandoc", *pandoc_args, silent=True, external=True)
@nox.session(name="build-pdf")
def build_pdf(session: Session) -> None:
"""Create pdf file."""
show_help(
session,
{
"skip-latex": "Skip Pweave to LaTeX generation.",
"clean": (
"Clean LaTeX and Pweave cache files before creating tex files."
),
},
)
if "skip-latex" not in session.posargs:
clean = "clean" in session.posargs
build_latex(session, clean)
session.log("Building figures...")
with chdir(session, FIGURES_DIR_PATH):
for latex_file in FIGURES_DIR_PATH.glob("*.tex"):
session.run(
"pdflatex",
"-interaction=nonstopmode",
"-shell-escape",
str(latex_file),
silent=True,
external=True,
)
session.log("Building pdf through pdflatex ...")
pdflatex_cmd = [
"pdflatex",
"-interaction=nonstopmode",
"-shell-escape",
f"{REPORT_NAME}.tex",
]
with chdir(session, REPORT_DIR_PATH):
session.run(*pdflatex_cmd, silent=True, external=True)
# Ignore error if there are no citations
try:
session.run(
"bibtex", f"{REPORT_NAME}.aux", silent=True, external=True
)
except CommandFailed:
pass
session.run(*pdflatex_cmd, silent=True, external=True)
session.run(*pdflatex_cmd, silent=True, external=True)
@nox.session(name="build-presentation")
def build_presentation(session: Session):
"""Build the presentation document."""
with chdir(session, REPORT_DIR_PATH):
session.run(
"pdflatex",
"-interaction=nonstopmode",
"-shell-escape",
"presentation.tex",
)
# -----------------------------------------------------------------------------
# Tests
# -----------------------------------------------------------------------------
@nox.session(name="test-docs")
def test_docs(session: Session) -> None:
"""Run the documentation related tests."""
show_help(session, {"skip-latex": "Skip the latex step."})
if "skip-latex" in session.posargs:
session.log("Skipping LaTeX and PDF building for this session.")
else:
build_pdf(session)
session.install(*test_requirements, *docs_requirements)
session.run("pytest", "-vvv", "-rs", str(ROOT / "tests" / "docs"))
@nox.session(name="test-src", python="3.7")
def test_src(session: Session) -> None:
"""Run the source code related tests."""
test_path = (
session.posargs[0] if session.posargs else str(ROOT / "tests" / "src")
)
session.log("Running tests!")
session.install(
*test_requirements,
*mlp_frameworks_requirements,
*hp_optimization_requirements,
env={"TMPDIR": "/var/tmp"},
)
session.run(
"pytest",
test_path,
f"--cov={str(ROOT / 'src')}",
"--cov-report=term-missing",
"-vvv",
silent=False,
)
# -----------------------------------------------------------------------------
# Format
# -----------------------------------------------------------------------------
@nox.session(name="format")
def apply_format(session: Session) -> None:
"""Apply formating rules to the selected files."""
session.install(*form_requirements)
session.run("black", "-l", "79", *python_files, silent=False)
session.run(
"isort", "-rc", *python_files, silent=False,
)
# -----------------------------------------------------------------------------
# Lint
# -----------------------------------------------------------------------------
@nox.session()
def lint(session: Session) -> None:
"""Lint the selected files."""
session.install(
*test_requirements,
*mlp_frameworks_requirements,
*hp_optimization_requirements,
*docs_requirements,
"nox==2020.5.24",
env={"TMPDIR": "/var/tmp"},
)
with chdir(session, ROOT):
session.run("mypy", *python_files, silent=False)
session.run("flake8", *python_files, silent=False)
session.run(
"pycodestyle",
"--ignore=E203,W503,E231",
*python_files,
silent=False,
)
session.run("pydocstyle", *python_files, silent=False)
session.run(
"black",
"-l",
"79",
"--check",
"--diff",
*python_files,
silent=False,
)
session.run(
"isort",
"-rc",
"--check-only",
"--diff",
*python_files,
silent=False,
)
session.run("pylint", *python_files, silent=False)