forked from kai687/sphinxawesome-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
178 lines (136 loc) · 5.58 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
"""Run commands for this repository."""
from __future__ import annotations
import tempfile
import nox
nox.options.stop_on_first_error = True
nox.options.sessions = ["docs", "lint", "fmt", "mypy", "tests"]
python_versions = ["3.12", "3.11", "3.10", "3.9", "3.8"]
session_install = nox.Session.install
class PoetryNoxSession(nox.Session):
"""Class for monkey-patching the Session object."""
def export(self: PoetryNoxSession, group: str, file_name: str) -> None:
"""Export a group's dependencies from poetry.
Args:
group: The name of the dependency group from :file:`pyproject.toml`.
file_name: The file name for exporting the dependencies.
"""
self.run(
"poetry",
"export",
"--without-hashes",
"--only",
group,
"--output",
file_name,
external=True,
)
def install(self: PoetryNoxSession, group: str, *args: str) -> None: # type: ignore
"""Install a group's dependencies into the nox virtual environment.
To make Nox use the version constraints as defined in :file:`pyproject.toml` ,
we have to export the dependencies into a temporary :file:`requirements.txt`.
Args:
group: The dependency group to export.
*args: The packages to install, passed on to :meth:`nox.Session.install`.
"""
with tempfile.NamedTemporaryFile() as requirements:
self.export(group, requirements.name)
session_install(self, "-r", requirements.name, *args)
# Monkey-patch galore
nox.Session.install = PoetryNoxSession.install # type: ignore
nox.Session.export = PoetryNoxSession.export # type: ignore
@nox.session(python=python_versions)
def tests(session: nox.Session) -> None:
"""Run unit tests."""
args = session.posargs or ["--cov"]
deps = ["coverage[toml]", "pytest", "pytest-cov", "sphinx-design"]
session.install("dev", ".", *deps)
session.run("pytest", *args)
@nox.session(python=python_versions)
def docs(session: nox.Session, live: bool = False, verbose: bool = False) -> None:
"""Build the docs.
Args:
session: The nox session instance.
live: If ``True``, use :cmd:`sphinx-autobuild` to build the docs with a live-reloading server.
If ``False``, use the regular :cmd:`sphinx-build`.
verbose: If ``True``, run sphinx in verbose mode (``-vvv``).
"""
args = ["-b", "dirhtml", "-aWTE", "docs", "docs/public"]
deps = ["sphinx", "bs4", "sphinx-sitemap", "sphinx-design", "sphinx-docsearch"]
sphinx_build = "sphinx-build"
if "--live" in session.posargs:
live = True
session.posargs.remove("--live")
if "--verbose" in session.posargs:
verbose = True
session.posargs.remove("--verbose")
if live:
deps.append("sphinx-autobuild")
sphinx_build = "sphinx-autobuild"
args += ["-A", "mode=development", "--watch", "src/sphinxawesome_theme"]
if verbose:
args += ["-vvv"]
if session.posargs:
args += session.posargs
session.install("docs", ".", *deps)
session.run(sphinx_build, *args)
@nox.session
def live_docs(session: nox.Session) -> None:
"""Build the docs with :cmd:sphinx-autobuild`."""
verbose = bool("--verbose" in session.posargs)
docs(session, True, verbose)
@nox.session
def linkcheck(session: nox.Session) -> None:
"""Check links."""
args = session.posargs or ["-b", "linkcheck", "-aWTE", "docs", "docs/public/_links"]
deps = ["sphinx", "bs4", "sphinx-sitemap", "sphinx-design", "sphinx-docsearch"]
session.install("docs", ".", *deps)
session.run("sphinx-build", *args)
@nox.session
def xml(session: nox.Session) -> None:
"""Build XML version of the docs."""
args = ["-b", "xml", "-aWTE", "docs", "docs/public/xml"]
deps = ["sphinx", "bs4", "sphinx-sitemap", "sphinx-design", "sphinx-docsearch"]
session.install("docs", ".", *deps)
session.run("sphinx-build", *args)
@nox.session(venv_backend=None)
def export(session: nox.Session) -> None:
"""Export a :file`requirements.txt` file for Netlify (Python 3.8).
On Netlify, we install Poetry, Pip, and Pipx with the same versions
as specified in :file:`requirements.txt`. Then, we use poetry
to install the regular dependencies, just like on a local machine.
On GitHub actions, we use the same file, although it runs on Python 3.11.
"""
session.export("netlify", "requirements.txt") # type: ignore[attr-defined]
session.run(
"poetry",
"export",
"--without-hashes",
"--with",
"docs",
"--output",
"docs/readthedocs.txt",
external=True,
)
@nox.session(python=python_versions)
def lint(session: nox.Session) -> None:
"""Lint python files."""
deps = ["ruff"]
session.install("lint", ".", *deps)
session.run("ruff", ".")
@nox.session
def fmt(session: nox.Session) -> None:
"""Format python files."""
deps = ["ruff", "black"]
session.install("lint", ".", *deps)
session.run("ruff", "check", ".", "--select", "I", "--fix")
session.run("black", ".")
@nox.session(python=["3.8", "3.12"])
def mypy(session: nox.Session) -> None:
"""Type-check python files with mypy.
Usually, issues occur either for all versions or the earliest
supported version, so running for these two is usually enough.
"""
# We need to install these additional libraries or Mypy will complain
deps = ["mypy", "pytest", "sphinx", "types-docutils", "bs4", "nox"]
session.install("dev", *deps)
session.run("mypy")