-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
301 lines (230 loc) · 9.14 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
"""Nox sessions.
Based on https://github.com/cjolowicz/hypermodern-python/blob/master/noxfile.py
Please refer to src/valiant/third-party/hypermodern-python/LICENSE for original
copyright and license details.
Copyright (c) 2020 The Valiant Authors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import tempfile
from typing import Callable, List, Optional
import nox
from nox.sessions import Session
package = "valiant"
nox.options.sessions = "safety", "lint", "mypy", "tests"
locations = "src", "tests", "noxfile.py"
source_location = ["src"]
supported_py_versions = ["3.8", "3.9"]
general_py_version = "3.9"
def install_with_constraints(
session: Session,
packages: List[str],
include_dev: bool = True,
callback: Optional[Callable] = None,
) -> None:
"""Install packages constrained by Poetry's lock file.
This function is a wrapper for nox.sessions.Session.install. It
invokes pip to install packages inside of the session's virtualenv.
Additionally, pip is passed a constraints file generated from
Poetry's lock file, to ensure that the packages are pinned to the
versions specified in poetry.lock. This allows you to manage the
packages as Poetry development dependencies.
Arguments:
session: The Session object.
packages: List of packages to install.
include_dev: Export the dev dependencies from poetry.
callback: A function to call with the session and requirements
"""
with tempfile.NamedTemporaryFile(suffix=".txt") as requirements:
run_args = [
"poetry",
"export",
"--without-hashes",
"--format=requirements.txt",
f"--output={requirements.name}",
]
if include_dev:
run_args.append("--dev")
session.run(
*run_args, external=True,
)
session.install(f"--constraint={requirements.name}", *packages)
if callback:
callback(session, requirements.name)
@nox.session(python=general_py_version)
def lint(session: Session) -> None:
"""Lint using flake8."""
args = session.posargs or locations
packages = [
"flake8",
"flake8-annotations",
"flake8-bandit",
"flake8-black",
"flake8-bugbear",
"flake8-docstrings",
"flake8-copyright",
"darglint",
]
install_with_constraints(session, packages=packages)
session.run("flake8", *args)
@nox.session(python=general_py_version)
def tidy(session: Session) -> None:
"""Run all tidy up actions."""
tidy_imports(session)
black(session)
@nox.session(python=general_py_version)
def black(session: Session) -> None:
"""Run black code formatter."""
args = session.posargs or locations
install_with_constraints(session, packages=["black"])
session.run("black", *args)
@nox.session(python=general_py_version)
def tidy_imports(session: Session) -> None:
"""Lint inplace using flake8-isort."""
args = session.posargs or locations
install_with_constraints(session, packages=["isort", "autoflake"])
session.run(
"autoflake",
"--remove-all-unused-imports",
"--in-place",
"--recursive",
"--ignore-init-module-imports",
"src",
)
session.run("isort", "--recursive", "--apply", *args)
def install_dependencies(session: Session, requirements_file: str) -> None:
"""Installs dependencies from a requirements file."""
session.install(f"--requirement={requirements_file}")
def safety_check(session: Session, requirements_file: str) -> None:
"""Run the safety application."""
session.run("safety", "check", f"--file={requirements_file}", "--full-report")
@nox.session(python=general_py_version)
def safety(session: Session) -> None:
"""Scan PROD dependencies for insecure packages."""
packages = ["safety"]
install_with_constraints(
session, include_dev=False, callback=safety_check, packages=packages
)
@nox.session(python=general_py_version)
def safety_dev(session: Session) -> None:
"""Scan PROD+DEV dependencies for insecure packages."""
packages = ["safety"]
install_with_constraints(
session, include_dev=True, callback=safety_check, packages=packages
)
@nox.session(python="3.8")
def mypy(session: Session) -> None:
"""Type-check using mypy."""
args = session.posargs or source_location
install_with_constraints(session, packages=["mypy", "marshmallow-dataclass"])
session.run("mypy", *args)
@nox.session(python="3.8")
def pytype(session: Session) -> None:
"""Type-check using pytype.
This will likely throw up errors - I'm still testing.
"""
args = session.posargs or ["--disable=import-error", *source_location]
install_with_constraints(session, packages=["pytype"])
session.run("pytype", *args)
@nox.session(python=supported_py_versions)
def tests(session: Session) -> None:
"""Run the test suite.
Get coverage by adding --cov:
nox -rs tests -- --cov
"""
args = session.posargs or []
packages = [
"coverage[toml]",
"pytest",
"pytest-cov",
"pytest-mock",
"pytest-datafiles",
"./",
]
install_with_constraints(
session, packages=packages, include_dev=False, callback=install_dependencies
)
session.run("pytest", *args)
@nox.session(python=supported_py_versions)
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
args = session.posargs or ["-m", "not e2e"]
packages = ["pytest", "pytest-mock", "pytest-datafiles", "typeguard", "./"]
install_with_constraints(
session, packages=packages, include_dev=False, callback=install_dependencies
)
session.run("pytest", f"--typeguard-packages={package}", *args)
@nox.session(python=supported_py_versions)
def xdoctest(session: Session) -> None:
"""Run examples with xdoctest."""
args = session.posargs or ["all"]
packages = ["xdoctest", "pygments", "./"]
install_with_constraints(
session, packages=packages, include_dev=False, callback=install_dependencies
)
session.run("python", "-m", "xdoctest", package, *args)
@nox.session(python=general_py_version)
def audit(session: Session) -> None:
"""Run a Valiant audit on Valiant."""
def generate_audit(session: Session, requirements_file: str) -> None:
"""Generate a Valiant audit."""
from pathlib import Path
Path("./build").mkdir(exist_ok=True)
session.run(
"valiant", "audit", "-o", "json", f"{requirements_file}",
)
packages = ["./"]
install_with_constraints(
session, packages=packages, include_dev=False, callback=generate_audit
)
def generate_bom(session: Session, requirements_file: str) -> None:
"""Generate a Software Bill of Materials."""
from pathlib import Path
Path("./build").mkdir(exist_ok=True)
session.run(
"cyclonedx-py", "-j", "-i", f"{requirements_file}", "-o", "build/bom.json"
)
@nox.session(python=general_py_version)
def bom(session: Session) -> None:
"""Generate a Software Bill of Materials."""
packages = ["cyclonedx-bom"]
install_with_constraints(
session, packages=packages, include_dev=False, callback=generate_bom
)
@nox.session(python=general_py_version)
def coverage_report(session: Session) -> None:
"""Prepare the coverage report.
Will produce xml by default.
json: nox -rs coverage -- json
html: nox -rs coverage -- html
"""
args = session.posargs or ["xml"]
install_with_constraints(session, ["coverage[toml]"])
session.run("coverage", *args, "--fail-under=90")
@nox.session(python=general_py_version)
def docs(session: Session) -> None:
"""Build the documentation with mkdocs."""
session.install("mkdocs~=1.1")
session.run("mkdocs", "build", "--clean")
@nox.session(python=general_py_version)
def docs_serve(session: Session) -> None:
"""Start server to view docs."""
session.install("mkdocs~=1.1")
session.run("mkdocs", "serve")
@nox.session(python=general_py_version)
def docs_publish(session: Session) -> None:
"""Publish the documentation with mkdocs."""
session.install("mkdocs~=1.1")
session.run("mkdocs", "gh-deploy", "--clean")