-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnoxfile.py
More file actions
201 lines (155 loc) · 5.86 KB
/
noxfile.py
File metadata and controls
201 lines (155 loc) · 5.86 KB
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
from __future__ import annotations
import os
import pathlib
import shutil
import sys
from functools import partial
import nox
# Control factors for finding pieces of the module
MODULE_NAME = "commentedconfigparser"
TESTS_PATH = "tests"
COVERAGE_FAIL_UNDER = 100
VENV_PATH = "./.venv"
LINT_PATH = "./src"
REQUIREMENTS_PATH = "./requirements"
LINTING_COMMANDS = (
(
"isort",
"--verbose",
"--force-single-line-imports",
"--profile",
"black",
"--add-import",
"from __future__ import annotations",
LINT_PATH,
TESTS_PATH,
),
("black", "--verbose", LINT_PATH, TESTS_PATH),
("flake8", "--show-source", "--verbose", LINT_PATH, TESTS_PATH),
("mypy", "--no-incremental", "--package", MODULE_NAME),
("mypy", "--no-incremental", TESTS_PATH),
)
# What we allowed to clean (delete)
CLEANABLE_TARGETS = [
"./dist",
"./build",
"./.nox",
"./.coverage",
"./.coverage.*",
"./coverage.json",
"./**/.mypy_cache",
"./**/.pytest_cache",
"./**/__pycache__",
"./**/*.pyc",
"./**/*.pyo",
]
# Define the default sessions run when `nox` is called on the CLI
nox.options.default_venv_backend = "virtualenv"
nox.options.sessions = ["lint", "test"]
@nox.session()
def dev(session: nox.Session) -> None:
"""Setup a development environment by creating the venv and installs dependencies."""
# Use the active environement if it exists, otherwise create a new one
venv_path = os.environ.get("VIRTUAL_ENV", VENV_PATH)
if sys.platform == "win32":
venv_path = f"{venv_path}/Scripts"
activate_command = f"{venv_path}/activate"
else:
venv_path = f"{venv_path}/bin"
activate_command = f"source {venv_path}/activate"
if not os.path.exists(VENV_PATH):
session.run("python", "-m", "venv", VENV_PATH, "--upgrade-deps")
python = partial(session.run, f"{venv_path}/python", "-m")
contraint = ("--constraint", f"{REQUIREMENTS_PATH}/constraints.txt")
python("pip", "install", "--editable", ".[dev,test]", *contraint, external=True)
if not os.environ.get("VIRTUAL_ENV"):
session.log(f"\n\nRun '{activate_command}' to enter the virtual environment.\n")
@nox.session(name="test")
def run_tests_with_coverage(session: nox.Session) -> None:
"""Run pytest with coverage, outputs console report and json."""
print_standard_logs(session)
contraint = ("--constraint", f"{REQUIREMENTS_PATH}/constraints.txt")
session.install(".[test]", *contraint)
coverage = partial(session.run, "python", "-m", "coverage")
coverage("erase")
if "partial-coverage" in session.posargs:
coverage("run", "--parallel-mode", "--module", "pytest", TESTS_PATH)
else:
coverage("run", "--module", "pytest", TESTS_PATH)
coverage("report", "--show-missing", f"--fail-under={COVERAGE_FAIL_UNDER}")
coverage("json")
@nox.session()
def coverage_combine(session: nox.Session) -> None:
"""CI: Combine parallel-mode coverage files and produce reports."""
print_standard_logs(session)
contraint = ("--constraint", f"{REQUIREMENTS_PATH}/constraints.txt")
session.install("-r", f"{REQUIREMENTS_PATH}/requirements-test.txt", *contraint)
coverage = partial(session.run, "python", "-m", "coverage")
coverage("combine")
coverage("report", "--show-missing", f"--fail-under={COVERAGE_FAIL_UNDER}")
coverage("json")
@nox.session(name="lint")
def run_linters_and_formatters(session: nox.Session) -> None:
"""Run code formatters, linters, and type checking against all files."""
print_standard_logs(session)
contraint = ("--constraint", f"{REQUIREMENTS_PATH}/constraints.txt")
session.install(".[dev,test]", *contraint)
python = partial(session.run, "python", "-m")
for linter_command in LINTING_COMMANDS:
python(*linter_command)
@nox.session()
def build(session: nox.Session) -> None:
"""Build distribution files."""
print_standard_logs(session)
session.install("build")
session.run("python", "-m", "build")
@nox.session(name="update-deps")
def update_deps(session: nox.Session) -> None:
"""Process requirement*.txt files, updating only additions/removals."""
print_standard_logs(session)
session.install("pip-tools")
session.run(
"pip-compile",
"--strip-extras",
"--no-annotate",
"--no-emit-index-url",
"--output-file",
f"{REQUIREMENTS_PATH}/constraints.txt",
*get_requirement_files(),
)
@nox.session(name="upgrade-deps")
def upgrade_deps(session: nox.Session) -> None:
"""Process requirement*.txt files and upgrade all libraries as possible."""
print_standard_logs(session)
session.install("pip-tools")
session.run(
"pip-compile",
"--strip-extras",
"--no-annotate",
"--no-emit-index-url",
"--upgrade",
"--output-file",
f"{REQUIREMENTS_PATH}/constraints.txt",
*get_requirement_files(),
)
@nox.session(python=False)
def clean(_: nox.Session) -> None:
"""Clean cache, .pyc, .pyo, and test/build artifact files from project."""
count = 0
for searchpath in CLEANABLE_TARGETS:
for filepath in pathlib.Path(".").glob(searchpath):
if filepath.is_dir():
shutil.rmtree(filepath)
else:
filepath.unlink()
count += 1
print(f"{count} files cleaned.")
def print_standard_logs(session: nox.Session) -> None:
"""Reusable output for monitoring environment factors."""
version = session.run("python", "--version", silent=True)
session.log(f"Running from: {session.bin}")
session.log(f"Running with: {version}")
def get_requirement_files() -> list[pathlib.Path]:
"""Get a list of requirement files matching "requirements*.txt"."""
glob = pathlib.Path(REQUIREMENTS_PATH).glob("requirements*.txt")
return [path for path in glob]