-
Notifications
You must be signed in to change notification settings - Fork 22
/
dodo.py
246 lines (208 loc) · 6.84 KB
/
dodo.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
# SPDX-License-Identifier: MIT
# Copyright (c) 2019 Martijn Pieters
# Licensed under the MIT license as detailed in LICENSE.txt
# doit tasks for aiolimiter
import os
from pathlib import Path
from doit.action import CmdAction
from doit.exceptions import TaskFailed
from doit.tools import Interactive, run_once
DOIT_CONFIG = {"default_tasks": ["all"], "minversion": "0.31.0"}
POETRY_ACTIVE = os.environ.get("POETRY_ACTIVE", False)
# https://github.com/MicrosoftDocs/vsts-docs/issues/4051
ON_CI = os.environ.get("BUILD_REASON", False) or os.environ.get("CI", False)
# Paths
def _path_sortkey(p):
"""Sort paths lexicographically, but put directories last
This ensures easy clean-ups while still getting a nicer output
"""
return (p.is_dir(), *p.parts)
def is_towncrier_fragment(path):
parts = path.name.split(".")
return (
len(parts) > 1
and parts[0].isdigit()
and parts[0].isascii()
and parts[1] in {"feature", "bugfix", "doc", "removal", "misc"}
)
# source files
THIS = Path(__file__).resolve()
HERE = THIS.parent
PYPROJECT = HERE / "pyproject.toml"
SRC_PATH = HERE / "src"
SRC_FILES = sorted(SRC_PATH.rglob("*.py"), key=_path_sortkey)
TESTS_PATH = HERE / "tests"
TESTS_FILES = sorted(TESTS_PATH.rglob("*.py"), key=_path_sortkey)
DOC_PATH = HERE / "docs"
RST_FILES = sorted(DOC_PATH.rglob("*.rst"), key=_path_sortkey)
CHANGELOG_PATH = HERE / "changelog.d"
CHANGELOG_TEMPLATE = CHANGELOG_PATH / "towncrier_template.md"
CHANGELOG_FRAGMENTS = sorted(filter(is_towncrier_fragment, CHANGELOG_PATH.iterdir()))
ALL_PY_FILES = sorted(
[THIS, *SRC_FILES, *TESTS_FILES, *DOC_PATH.glob("*.py")], key=_path_sortkey
)
# outputs
DOC_BUILD_PATH = DOC_PATH / "_build"
DOC_HTML_PATH = DOC_BUILD_PATH / "html"
DOC_SPELLING_PATH = DOC_BUILD_PATH / "spelling"
DOC_HTML_FILES = sorted([*DOC_HTML_PATH.rglob("*"), DOC_HTML_PATH], key=_path_sortkey)
DOC_SPELLING_FILES = [DOC_SPELLING_PATH / "output.txt", DOC_SPELLING_PATH]
DIST_PATH = HERE / "dist"
DIST_FILES = sorted(
[*DIST_PATH.glob("*.tar.gz"), *DIST_PATH.glob("*.whl")], key=_path_sortkey
)
CHANGELOG = HERE / "CHANGELOG.md"
def with_poetry(*actions):
if not POETRY_ACTIVE:
return actions
# handle both shell and exec actions
return [
(
f"poetry run {action}"
if isinstance(action, str)
else ["poetry", "run", *action]
)
for action in actions
]
def task_poetry_install():
# in case we have doit installed outside of poetry
# and there is no lock file, run poetry first.
return {
"basename": "_install_poetry",
"actions": [["poetry", "install", "--with", "docs"]],
"targets": ["poetry.lock"],
"uptodate": [run_once],
}
def task_devsetup():
"""Set up the development environment"""
yield {
"name": "install_precommit",
"setup": ["_install_poetry"],
"actions": with_poetry(["pre-commit", "install"]),
"targets": [".git/hooks/pre-commit"],
"file_dep": [".pre-commit-config.yaml"],
}
def task_lint():
"""Lint the code with isort, flake8 and mypy"""
yield {
"name": "isort_check",
"setup": ["devsetup"],
"actions": with_poetry("isort --check-only %(changed)s"),
"file_dep": ALL_PY_FILES,
}
yield {
"name": "flake8",
"setup": ["devsetup"],
"actions": with_poetry("flake8 %(changed)s"),
"file_dep": ALL_PY_FILES,
}
yield {
"name": "mypy",
"setup": ["devsetup"],
"actions": with_poetry(["mypy", SRC_PATH]),
"file_dep": SRC_FILES,
}
def task_format():
"""Format files with isort and black"""
yield {
"name": "isort",
"setup": ["devsetup"],
"actions": with_poetry("isort %(changed)s"),
"file_dep": ALL_PY_FILES,
}
yield {
"name": "black",
"setup": ["devsetup"],
"actions": with_poetry("black %(changed)s"),
"file_dep": ALL_PY_FILES,
}
def task_test():
"""Run the tests"""
test_cmd = with_poetry("pytest")[0]
return {
"setup": ["devsetup"],
"actions": [Interactive(test_cmd)],
"file_dep": [*SRC_FILES, *TESTS_FILES],
"task_dep": ["lint"],
}
def task_tox():
"""Run tox, testing against all supported Python releases"""
test_cmd = with_poetry("tox")[0]
return {
"setup": ["devsetup"],
"actions": [Interactive(test_cmd)],
"file_dep": [*SRC_FILES, *TESTS_FILES],
"task_dep": ["lint"],
}
def task_docs_checks():
"""Check documentation spelling and towncrier handling"""
make_cmd = with_poetry(["make", "spelling"])[0]
yield {
"name": "docs_spelling",
"setup": ["devsetup"],
"actions": [CmdAction(make_cmd, cwd=DOC_PATH, shell=False)],
"file_dep": [
*SRC_FILES,
*RST_FILES,
DOC_PATH / "conf.py",
DOC_PATH / "Makefile",
DOC_PATH / "spelling_wordlist.txt",
],
"targets": DOC_SPELLING_FILES,
"clean": True,
}
def check_towncrier_fragments(changed):
for path in map(Path, changed):
if not (is_towncrier_fragment(path) or path == CHANGELOG_TEMPLATE):
return TaskFailed(
f"{CHANGELOG_PATH.name}/{path.name} is not a valid towncrier "
f"fragment name."
)
yield {
"name": "towncrier_fragments",
"actions": [check_towncrier_fragments],
"file_dep": sorted(CHANGELOG_PATH.iterdir()),
}
yield {
"name": "towncrier",
"task_dep": ["docs_checks:towncrier_fragments"],
"setup": ["devsetup"],
"actions": with_poetry(["towncrier", "--draft"]),
"file_dep": [PYPROJECT, CHANGELOG_TEMPLATE, *CHANGELOG_FRAGMENTS],
}
def task_docs():
"""Build the documentation"""
make_cmd = with_poetry(["make", "html"])[0]
return {
"setup": ["devsetup"],
"task_dep": ["docs_checks"],
"actions": [CmdAction(make_cmd, cwd=DOC_PATH, shell=False)],
"file_dep": [
*SRC_FILES,
*RST_FILES,
DOC_PATH / "conf.py",
DOC_PATH / "Makefile",
],
"targets": DOC_HTML_FILES,
"clean": True,
}
def task_build():
"""Build the distribution packages"""
yield {
"name": "poetry",
"setup": ["devsetup"],
"actions": ["poetry build"],
"task_dep": ["test", "docs"],
"file_dep": [PYPROJECT, *ALL_PY_FILES],
"targets": [DIST_PATH, *DIST_FILES],
"clean": True,
}
yield {
"name": "check",
"setup": ["devsetup", "build:poetry"],
"actions": ["twine check dist/*"],
"file_dep": [*DIST_FILES],
}
def task_all():
"""Run all checks, then build the docs and release"""
return {"actions": [], "task_dep": ["tox", "docs", "build"]}