-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtasks.py
265 lines (200 loc) · 7.4 KB
/
tasks.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
"""
MIT License
Copyright (c) 2022 Ramon Hagenaars
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 os
import shutil
import sys
import venv as venv_
from glob import glob
from pathlib import Path
import invoke.tasks as invoke_tasks
from invoke import task
_ROOT = "nptyping"
_PY_VERSION_STR = (
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
)
_DEFAULT_VENV = f".venv{_PY_VERSION_STR}"
if sys.version_info.minor >= 11:
# Patch invoke to replace a deprecated inspect function.
# FIXME: https://github.com/pyinvoke/invoke/pull/877
invoke_tasks.inspect.getargspec = invoke_tasks.inspect.getfullargspec
if os.name == "nt":
_PY_SUFFIX = "\\Scripts\\python.exe"
_PIP_SUFFIX = "\\Scripts\\pip.exe"
else:
_PY_SUFFIX = "/bin/python"
_PIP_SUFFIX = "/bin/pip"
def get_venv(py=None):
return f".venv{py}" if py else _DEFAULT_VENV
def get_constraints(py=None):
if py is not None:
# Skip the patch version.
py = ".".join(py.split(".")[:2])
return f"constraints-{py}.txt" if py else "constraints.txt"
def get_py(py=None):
return f"{get_venv(py)}{_PY_SUFFIX}"
def get_pip(py=None):
return f"{get_venv(py)}{_PIP_SUFFIX}"
def get_versions(py=None):
if py:
py_versions = [py]
else:
py_versions = sorted(
venv_path.split(".venv")[1]
for venv_path in glob(str(Path(__file__).parent / ".venv*"))
)
py_versions.sort(key=lambda version: int(version.replace(".", "")))
return py_versions
def print_header(version, function):
print()
print(f"[ {version} - {function.__name__} ]")
# BUILD TOOLS
@task
def run(context, command, py=None):
"""Run the given command using the venv."""
context.run(f"{get_py(py)} {command}")
@task
def destroy(context, py=None):
"""Destroy the generated virtual environment."""
venv_to_destroy = get_venv(py)
print(f"Destroying {venv_to_destroy}")
shutil.rmtree(venv_to_destroy, ignore_errors=True)
@task
def clean(context, py=None):
"""Clean up all generated stuff."""
print("Swiping clean the project")
try:
os.remove(".coverage")
except FileNotFoundError:
... # No problem at all.
shutil.rmtree(f"{_ROOT}.egg-info", ignore_errors=True)
shutil.rmtree("dist", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
shutil.rmtree(".mypy_cache", ignore_errors=True)
shutil.rmtree(".pytest_cache", ignore_errors=True)
shutil.rmtree("__pycache__", ignore_errors=True)
@task
def venv(context, py=None):
"""Create a new virtual environment and install all build dependencies in
it."""
print(f"Creating virtual environment: {_DEFAULT_VENV}")
venv_.create(_DEFAULT_VENV, with_pip=True)
print("Upgrading pip")
context.run(f"{get_py(py)} -m pip install --upgrade pip")
context.run(f"{get_pip(py)} install -r ./dependencies/build-requirements.txt")
@task
def lock(context, py=None):
"""Lock the project dependencies in a constraints file."""
for version in get_versions(py):
print_header(version, lock)
context.run(
f"{get_py(version)} -m piptools compile ./dependencies/* --output-file {get_constraints(version)} --quiet"
)
@task
def install(context, py=None):
"""Install all dependencies (dev)."""
for version in get_versions(py):
print_header(version, install)
print(f"Upgrading pip")
context.run(f"{get_py(version)} -m pip install --upgrade pip")
print(f"Installing dependencies into: {version}")
print(
f"{get_pip(version)} install .[dev] --constraint {get_constraints(version)}"
)
context.run(
f"{get_pip(version)} install .[dev] --constraint {get_constraints(version)}"
)
@task(clean, venv, lock, install)
def init(context, py=None):
"""Initialize a new dev setup."""
@task
def wheel(context, py=None):
"""Build a wheel."""
print(f"Installing dependencies into: {_DEFAULT_VENV}")
context.run(f"{get_py(py)} setup.py sdist")
context.run(f"{get_pip(py)} wheel . --wheel-dir dist --no-deps")
# QA TOOLS
@task
def test(context, py=None):
"""Run the tests."""
for version in get_versions(py):
print_header(version, test)
context.run(f"{get_py(version)} -m unittest discover tests")
@task
def doctest(context, py=None, verbose=False):
"""Run the doctests."""
# Check the README.
context.run(f"{get_py(py)} -m doctest README.md")
context.run(f"{get_py(py)} -m doctest USERDOCS.md")
# And check all the modules.
for filename in glob(f"{_ROOT}/**/*.py", recursive=True):
if verbose:
print(f"doctesting {filename}")
context.run(f"{get_py(py)} -m doctest {filename}")
@task
def coverage(context, py=None):
"""Run the tests with coverage."""
for version in get_versions(py):
print_header(version, coverage)
context.run(f"{get_py(version)} -m coverage run -m unittest discover tests")
context.run(f"{get_py(py)} -m coverage combine")
context.run(f"{get_py(py)} -m coverage report")
@task
def pylint(context, py=None):
"""Run pylint for various PEP-8 checks."""
context.run(f"{get_py(py)} -m pylint --rcfile=setup.cfg {_ROOT}")
@task
def mypy(context, py=None):
"""Run mypy for static type checking."""
context.run(f"{get_py(py)} -m mypy {_ROOT}")
@task(doctest, pylint, mypy, coverage)
@task
def qa(context, py=None):
"""Run the linting tools."""
# FORMATTERS
@task
def black(context, check=False, py=None):
"""Run Black for formatting."""
cmd = f"{get_py(py)} -m black {_ROOT} setup.py tasks.py tests"
if check:
cmd += " --check"
context.run(cmd)
@task
def isort(context, check=False, py=None):
"""Run isort for optimizing imports."""
cmd = f"{get_py(py)} -m isort {_ROOT} setup.py tasks.py tests"
if check:
cmd += " --check"
context.run(cmd)
@task
def autoflake(context, check=False, py=None):
"""Run autoflake to remove unused imports and variables."""
cmd = (
f"{get_py(py)} -m autoflake {_ROOT} setup.py tasks.py tests --recursive --in-place"
f" --remove-unused-variables --remove-all-unused-imports --expand-star-imports"
)
if check:
cmd += " --check"
context.run(cmd)
@task
def format(context, check=False, py=None):
"""Run the formatters."""
autoflake(context, check=check, py=py)
isort(context, check=check, py=py)
black(context, check=check, py=py)