-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoxfile.py
285 lines (233 loc) · 8.04 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
# -*- coding: utf-8 -*-
import os
from os import path
from shutil import which
import nox
LIBS = (
"aiopen",
"asyncify",
"dgpylibs",
"dgpytest",
"fmts",
"funkify",
"h5",
"jsonbourne",
"lager",
"listless",
"requires",
"shellfish",
"xtyping",
)
def is_win() -> bool:
"""Determine if current operating system is windows
Returns:
True if on a windows machine; False otherwise
"""
return os.name == "nt"
nox.options.envdir = ".nox_win" if is_win() else ".nox"
IS_GITLAB_CI = "GITLAB_CI" in os.environ
IS_GITHUB_CI = "CI" in os.environ and os.environ["CI"] == "true"
REUSE_TEST_ENVS = True
PWD = path.abspath(path.dirname(__file__))
LIBS_DIR = path.join(PWD, "libs")
VENV_BACKEND = None if is_win() or IS_GITHUB_CI or not which("conda") else "conda"
LIB_DIRS = {
el: path.join(LIBS_DIR, el)
for el in os.listdir(LIBS_DIR)
if el[0] != "." and el in LIBS
}
SOURCE_DIRS = {el: path.join(LIBS_DIR, el, "src", el) for el in LIBS}
TESTS_DIRS = {el: path.join(LIBS_DIR, el, "tests") for el in LIB_DIRS}
# #############
# ### UTILS ###
# #############
def _get_session_python_site_packages_dir(session):
try:
site_packages_dir = session._runner._site_packages_dir
except AttributeError:
old_install_only_value = session._runner.global_config.install_only
try:
# Force install only to be false for the following chunk of code
# For additional information as to why see:
# /~https://github.com/theacodes/nox/pull/181
session._runner.global_config.install_only = False
site_packages_dir = session.run(
"python",
"-c"
"import sys; "
"from distutils.sysconfig import get_python_lib; "
"sys.stdout.write(get_python_lib())",
silent=True,
log=False,
)
session._runner._site_packages_dir = site_packages_dir
finally:
session._runner.global_config.install_only = old_install_only_value
return site_packages_dir
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def pipc(session):
session.install("pip-tools")
reqs_ini = (
"dev.in",
"docs.in",
)
for reqs_file in reqs_ini:
output_filepath = path.join(
PWD, "requirements", reqs_file.replace(".in", ".txt")
)
session.run(
"uv",
"pip",
"compile",
"-o",
output_filepath,
path.join(PWD, "requirements", reqs_file),
)
def _mypy(session):
session.install(
"-U",
"mypy",
"typing-extensions",
"pydantic",
"pydantic_core",
"anyio",
"pytest",
"nox",
)
session.install("orjson", "types-orjson", "fastapi", "click==8.1.3")
session.run("mypy", "--version")
session.run(
"mypy",
"--show-error-codes",
"--config-file",
"./pyproject.toml",
*list(SOURCE_DIRS.values()),
)
for lib in LIBS:
noxfile_path = path.join("libs", lib, "noxfile.py")
args = [
"mypy",
"--show-error-codes",
"--config-file",
"./pyproject.toml",
*list(SOURCE_DIRS.values()),
path.join("libs", lib, "tests"),
]
if path.exists(noxfile_path):
args.append(noxfile_path)
session.run(*args)
def _ruff(session: nox.Session) -> None:
session.install("ruff")
session.run("ruff", "--version")
session.run("ruff", "check", ".")
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def mypy(session):
_mypy(session)
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def lint(session):
_mypy(session)
ruffext = """
[tool.ruff]
extend = "../../pyproject.toml"
"""
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def dev(session):
from pprint import pprint
import toml
for libname, dirpath in LIB_DIRS.items():
echo(libname, dirpath)
pyproject_toml_fspath = path.join(dirpath, "pyproject.toml")
with open(pyproject_toml_fspath) as f:
pyproject_toml_str = f.read().rstrip("\n")
data = toml.loads(pyproject_toml_str)
pprint(data)
def _pkg_entry_point(pkg_name):
return "\n".join(
[
"# -*- coding: utf-8 -*-",
f'"""pkg entry ~ `python -m {pkg_name}`"""',
"import sys",
"",
f"from {pkg_name}._meta import __pkgroot__, __title__, __version__",
"",
"sys.stdout.write(",
' f"package: {__title__}\\nversion: {__version__}\\npkgroot: {__pkgroot__}\\n"',
")",
"",
]
)
def echo(*args, **kwargs):
print(*args, **kwargs) # noqa: T201
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def update_metadata(session):
import toml
libs2update = {k: v for k, v in LIB_DIRS.items() if k not in {"dgpylibs"}}
for libname, dirpath in libs2update.items():
echo(libname, dirpath)
with open(path.join(dirpath, "pyproject.toml")) as f:
pyproject_toml_str = f.read()
data = toml.loads(pyproject_toml_str)
echo("____________________________")
poetry_metadata = data["tool"]["poetry"]
assert "name" in poetry_metadata and poetry_metadata["name"] == libname
assert "version" in poetry_metadata
assert "description" in poetry_metadata and poetry_metadata["description"] != ""
metadata_file_lines = [
"# -*- coding: utf-8 -*-",
'"""Package metadata/info"""\n',
'__all__ = ("__title__", "__description__", "__pkgroot__", "__version__")',
'__title__ = "{}"'.format(poetry_metadata["name"]),
'__description__ = "{}"'.format(poetry_metadata["description"]),
'__pkgroot__ = __file__.replace("_meta.py", "").rstrip("/\\\\")',
'__version__ = "{}"'.format(poetry_metadata["version"]),
]
metadata_file_string = "\n".join(metadata_file_lines).strip("\n") + "\n"
deprecated_meta_file_lines = [
"# -*- coding: utf-8 -*-",
'"""Package metadata/info"""',
"import warnings",
"",
f"from {libname}.__about__ import __description__, __pkgroot__, __title__, __version__",
"",
"warnings.warn(",
f' "{libname}._meta is deprecated, use {libname}.__about__ instead",',
" DeprecationWarning,",
" stacklevel=2,",
")",
"",
'__all__ = ("__title__", "__description__", "__pkgroot__", "__version__")',
"",
]
# check that is valid python...
exec(metadata_file_string)
echo("~~~")
echo(metadata_file_string)
echo("~~~")
meta_filepath = path.join(dirpath, libname, "_meta.py")
metadata_filepath = path.join(dirpath, libname, "__about__.py")
with open(metadata_filepath, "w", encoding="utf-8", newline="\n") as f:
f.write(metadata_file_string)
with open(meta_filepath, "w", encoding="utf-8", newline="\n") as f:
f.write("\n".join(deprecated_meta_file_lines))
def _install_mkdocs_deps(session):
session.install(
"mkdocs", # docs!
"mkdocs-material", # material theme
"mkdocs-jupyter", # rendering jupyter notebooks
"mkdocstrings[python]", # render docstrings 2 markdown
"markdown-callouts", # callouts
"black", # code formatting for mkdocstrings-signatures
)
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def mkdocs_serve(session):
_install_mkdocs_deps(session)
session.run("mkdocs", "serve")
@nox.session(venv_backend=VENV_BACKEND, reuse_venv=True)
def mkdocs(session):
_install_mkdocs_deps(session)
session.run("mkdocs", "build")
@nox.session(reuse_venv=True)
def freeze(session):
for lib in LIBS:
session.install(lib)
_freeze = session.run("pip", "freeze", "--local", "-l")