This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnoxfile.py
293 lines (250 loc) · 9.13 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
# Copyright (c) 2019 The Erizo Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
#
# This code is part of the Fatiando a Terra project (https://www.fatiando.org)
#
"""
Configuration for setting up virtual environments to check code style, run
tests, build documentation, and create package distributions for PyPI.
Instructions
------------
* List all available sessions: 'nox -l'
* Run all sessions: 'nox' (without arguments)
* Run only a particular session: 'nox -s SESSION' (e.g., 'nox -s test-3.8')
* Only create virtual environments and install packages: 'nox --install-only'
(use this to prepare for working offline)
* Run a session but skip the install step: 'nox -s SESSION -- skip-install'
* Run a session and list installed packages:
'nox -s SESSION -- list-packages'
* Run only selected code style checkers: 'nox -s check -- CHECKER' (could be
'black', 'flake8', or 'pylint')
* Open the documentation in a browser after building: 'nox -s docs -- show'
* Create a sandbox conda environment with all dependencies and the package
installed: 'nox -s sandbox'. You can then activate it with 'conda activate
.nox/sandbox' (from the project folder) and run things like 'python',
'ipython', and 'jupyter'. Useful for experimentation.
"""
from pathlib import Path
import shutil
import webbrowser
import nox
# CONFIGURE NOX
# Always reuse environments. Use --no-reuse-existing-virtualenvs to disable.
nox.options.reuse_existing_virtualenvs = True
# Custom command-line arguments that we're implementing
NOX_ARGS = ["skip-install", "list-packages", "show"]
PACKAGE = "erizo"
PYTHON_VERSIONS = ["3.8", "3.7", "3.6"]
DOCS = Path("doc")
REQUIREMENTS = {
"run": "requirements.txt",
"test": str(Path("env") / "requirements-test.txt"),
"docs": str(Path("env") / "requirements-docs.txt"),
"style": str(Path("env") / "requirements-style.txt"),
"build": str(Path("env") / "requirements-build.txt"),
"sandbox": str(Path("env") / "requirements-sandbox.txt"),
}
STYLE_ARGS = {
"pylint": [PACKAGE, "setup.py"],
"flake8": [PACKAGE, "setup.py", str(DOCS / "conf.py")],
"black": [
"--check",
PACKAGE,
"setup.py",
str(DOCS / "conf.py"),
"examples",
"tutorials",
"noxfile.py",
],
}
# Use absolute paths for pytest otherwise it struggles to pick up coverage info
PYTEST_ARGS = [
f"--cov-config={Path('.coveragerc').resolve()}",
"--cov-report=term-missing",
f"--cov={PACKAGE}",
"--doctest-modules",
'--doctest-glob="*.rst"',
"-v",
"--pyargs",
]
RST_FILES = [str(p.resolve()) for p in Path("doc").glob("**/*.rst")]
@nox.session()
def format(session):
"Run 'black' to format the code"
install_requirements(session, ["style"])
session.run("black", *STYLE_ARGS["black"][1:])
@nox.session()
def check(session):
"Check code style"
install_requirements(session, ["style"])
list_packages(session)
args = list(set(session.posargs).difference(NOX_ARGS))
if args:
checks = args
else:
checks = ["black", "flake8", "pylint"]
for check in checks:
session.run(check, *STYLE_ARGS[check])
@nox.session()
def build(session):
"Build source and wheel distributions"
install_requirements(session, ["build"])
packages = build_project(session, install=False)
check_packages(session, packages)
list_packages(session)
@nox.session(python=PYTHON_VERSIONS)
def test(session):
"Run the tests and measure coverage (using pip)"
install_requirements(session, ["build", "run", "test"])
build_project(session, install=True)
list_packages(session)
run_pytest(session)
@nox.session(venv_backend="conda", python=PYTHON_VERSIONS)
def test_conda(session):
"Run the tests and measure coverage (using conda)"
install_requirements(session, ["build", "run", "test"], package_manager="conda")
build_project(session, install=True)
list_packages(session, package_manager="conda")
run_pytest(session)
@nox.session(venv_backend="conda", python="3.8")
def docs(session):
"""
Build the documentation web pages
Uses conda instead of pip because some dependencies don't install well with
pip (cartopy, in particular).
"""
install_requirements(session, ["build", "run", "docs"], package_manager="conda")
build_project(session, install=True)
list_packages(session, package_manager="conda")
# Generate the API reference
session.run(
"sphinx-autogen",
"-i",
"-t",
str(DOCS / "_templates"),
"-o",
str(DOCS / "api" / "generated"),
str(DOCS / "api" / "index.rst"),
)
# Build the HTML pages
html = DOCS / "_build" / "html"
session.run(
"sphinx-build",
"-d",
str(DOCS / "_build" / "doctrees"),
"doc",
str(html),
)
if session.posargs and "show" in session.posargs:
session.log("Opening built docs in the default web browser.")
webbrowser.open(f"file://{(html / 'index.html').resolve()}")
@nox.session(venv_backend="conda", python="3.8")
def sandbox(session):
"Create a sandbox conda environment to use outside of nox"
install_requirements(
session, ["build", "style", "run", "docs", "sandbox"], package_manager="conda"
)
build_project(session, install=True)
list_packages(session, package_manager="conda")
@nox.session
def clean(session):
"""
Remove all files generated by the build process
"""
files = [
Path("build"),
Path("dist"),
Path("MANIFEST"),
Path(".coverage"),
Path(".pytest_cache"),
Path("__pycache__"),
DOCS / "_build",
DOCS / "api" / "generated",
DOCS / "gallery",
DOCS / "tutorials",
]
files.extend(Path(PACKAGE).glob("**/*.pyc"))
files.extend(Path(".").glob(".coverage.*"))
files.extend(Path(PACKAGE).glob("**/__pycache__"))
files.extend(Path(".").glob("*.egg-info"))
files.extend(Path(".").glob("**/.ipynb_checkpoints"))
for f in files:
if f.exists():
session.log(f"removing: {f}")
if f.is_dir():
shutil.rmtree(f)
else:
f.unlink()
# UTILITY FUNCTIONS
###############################################################################
def install_requirements(session, requirements, package_manager="pip"):
"""
Install dependencies from the requirements files specified in REQUIREMENTS.
*requirements* should be a list of keywords defined in the REQUIREMENTS
dictionary. Set *package_manager* to "conda" if using the conda backend for
virtual environments.
"""
if package_manager not in {"pip", "conda"}:
raise ValueError(f"Invalid package manager '{package_manager}'")
if session.posargs and "skip-install" in session.posargs:
session.log("Skipping install steps.")
return
arg_name = {"pip": "-r", "conda": "--file"}
args = []
for requirement in requirements:
args.extend([arg_name[package_manager], REQUIREMENTS[requirement]])
if package_manager == "pip":
session.install(*args)
elif package_manager == "conda":
session.conda_install(
"--channel=conda-forge",
"--channel=defaults",
*args,
)
def list_packages(session, package_manager="pip"):
"""
List installed packages in the virtual environment.
If the argument 'list-packages' is passed to the nox session, will list the
installed packages in the virtual environment (using the package manager
specified).
Example: 'nox -s test-3.7 -- list-packages'
"""
if package_manager not in {"pip", "conda"}:
raise ValueError(f"Invalid package manager '{package_manager}'")
if session.posargs and "list-packages" in session.posargs:
session.log(f"Packages installed ({package_manager}):")
if package_manager == "pip":
session.run("pip", "freeze")
elif package_manager == "conda":
session.run("conda", "list")
def run_pytest(session):
"""
Run tests with pytest in a separate folder.
"""
tmpdir = session.create_tmp()
session.cd(tmpdir)
session.run("pytest", *PYTEST_ARGS, PACKAGE, *RST_FILES)
session.run("coverage", "xml", "-o", ".coverage.xml")
# Copy the coverage information back so it can be reported
for f in Path(".").glob(".coverage*"):
shutil.copy(f, Path(__file__).parent)
def build_project(session, install=False):
"""
Build source and wheel packages for the project and returns their path.
If 'install==True', will also install the package.
"""
session.log("Build source and wheel distributions:")
if Path("dist").exists():
shutil.rmtree("dist")
session.run("python", "setup.py", "sdist", "bdist_wheel", silent=True)
packages = list(Path("dist").glob("*"))
if install and packages:
session.install("--force-reinstall", "--no-deps", str(packages[0]))
return packages
def check_packages(session, packages):
"""
Use twine to check the built packages (source and wheel).
"""
for package in packages:
session.run("twine", "check", str(package))