-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
239 lines (194 loc) · 8.29 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
"""Tasks for maintaining the project.
Execute 'invoke --list' for guidance on using Invoke
"""
from pathlib import Path
import platform
import shutil
import sys
from typing import Any, cast, Dict, List
import webbrowser
from invoke import Context, task
from invoke.exceptions import Failure
from invoke.runners import Result
if sys.version_info[:3] >= (3, 6, 0):
import tomli
ROOT_DIR = Path(__file__).parent
SETUP_FILE = ROOT_DIR.joinpath("setup.py")
TEST_DIR = ROOT_DIR.joinpath("tests")
SOURCE_DIR = ROOT_DIR.joinpath("radikoplaylist")
SETUP_PY = ROOT_DIR.joinpath("setup.py")
TASKS_PY = ROOT_DIR.joinpath("tasks.py")
COVERAGE_FILE = ROOT_DIR.joinpath(".coverage")
COVERAGE_DIR = ROOT_DIR.joinpath("htmlcov")
COVERAGE_REPORT = COVERAGE_DIR.joinpath("index.html")
PYTHON_DIRS = [str(d) for d in [SETUP_PY, TASKS_PY, SOURCE_DIR, TEST_DIR]]
def _delete_file(file: Path) -> None:
try:
file.unlink(missing_ok=True)
except TypeError:
# missing_ok argument added in 3.8
try:
file.unlink()
except FileNotFoundError:
pass
if sys.version_info[:3] >= (3, 6, 0):
@task(help={"check": "Checks if source is formatted without applying changes"})
def style(context: Context, check: bool = False) -> None:
"""Format code."""
for result in [
docformatter(context, check),
isort(context, check),
autoflake(context, check),
black(context, check),
]:
if result.failed:
raise Failure(result)
@task
def docformatter(context: Context, check: bool = False) -> Result:
"""Runs docformatter.
This function includes hard coding of line length.
see:
- Add pyproject.toml support for config (Issue #10) by weibullguy · Pull Request #77 · PyCQA/docformatter
https://github.com/PyCQA/docformatter/pull/77
"""
parsed_toml = tomli.loads(Path("pyproject.toml").read_text("UTF-8"))
config = parsed_toml["tool"]["docformatter"]
list_options = build_list_options_docformatter(config, check)
docformatter_options = " ".join(list_options)
return cast(
Result, context.run("docformatter {} {}".format(docformatter_options, " ".join(PYTHON_DIRS)), warn=True)
)
# Reason: This is dataclass. pylint: disable=too-few-public-methods
class DocformatterOption:
def __init__(self, list_str: List[str], enable: bool) -> None:
self.list_str = list_str
self.enable = enable
def build_list_options_docformatter(config: Dict[str, Any], check: bool) -> List[str]:
"""Builds list of docformatter options."""
docformatter_options = (
DocformatterOption(["--recursive"], "recursive" in config and config["recursive"]),
DocformatterOption(["--wrap-summaries", str(config["wrap-summaries"])], "wrap-summaries" in config),
DocformatterOption(["--wrap-descriptions", str(config["wrap-descriptions"])], "wrap-descriptions" in config),
DocformatterOption(["--check"], check),
DocformatterOption(["--in-place"], not check),
)
return [
item
for docformatter_option in docformatter_options
if docformatter_option.enable
for item in docformatter_option.list_str
]
def autoflake(context: Context, check: bool = False) -> Result:
"""Runs autoflake."""
autoflake_options = "--recursive {}".format("--check" if check else "--in-place")
return cast(Result, context.run("autoflake {} {}".format(autoflake_options, " ".join(PYTHON_DIRS)), warn=True))
def isort(context: Context, check: bool = False) -> Result:
"""Runs isort."""
isort_options = "--recursive {}".format("--check-only --diff" if check else "")
return cast(Result, context.run("isort {} {}".format(isort_options, " ".join(PYTHON_DIRS)), warn=True))
def black(context: Context, check: bool = False) -> Result:
"""Runs black."""
black_options = "{}".format("--check --diff" if check else "")
return cast(Result, context.run("black {} {}".format(black_options, " ".join(PYTHON_DIRS)), warn=True))
if sys.version_info[:3] >= (3, 6, 0):
@task
def lint_flake8(context: Context) -> None:
"""Lint code with flake8."""
context.run("flake8 {} {}".format("--radon-show-closures", " ".join(PYTHON_DIRS)))
@task
def lint_pylint(context: Context) -> None:
"""Lint code with pylint."""
context.run("pylint {}".format(" ".join(PYTHON_DIRS)))
@task
def lint_mypy(context: Context) -> None:
"""Lint code with mypy."""
context.run("mypy {}".format(" ".join(PYTHON_DIRS)))
@task
def lint_bandit(context: Context) -> None:
"""Lints code with bandit."""
space = " "
context.run("bandit --recursive {}".format(space.join([str(p) for p in [SOURCE_DIR, TASKS_PY]])), pty=True)
context.run("bandit --recursive --skip B101 {}".format(TEST_DIR), pty=True)
@task
def lint_dodgy(context: Context) -> None:
"""Lints code with dodgy."""
context.run("dodgy --ignore-paths csvinput", pty=True)
@task
def lint_pydocstyle(context: Context) -> None:
"""Lints code with pydocstyle."""
context.run("pydocstyle .", pty=True)
@task(lint_bandit, lint_dodgy, lint_flake8, lint_pydocstyle)
def lint(_context: Context) -> None:
"""Run all linting."""
@task(lint_mypy, lint_pylint)
def lint_deep(_context: Context) -> None:
"""Runs deep linting."""
@task
def radon_cc(context: Context) -> None:
"""Reports code complexity."""
context.run("radon cc {}".format(" ".join(PYTHON_DIRS)))
@task
def radon_mi(context: Context) -> None:
"""Reports maintainability index."""
context.run("radon mi {}".format(" ".join(PYTHON_DIRS)))
@task(radon_cc, radon_mi)
def radon(_context: Context) -> None:
"""Reports radon."""
@task
def xenon(context: Context) -> None:
"""Check code complexity."""
context.run(
("xenon" " --max-absolute A" "--max-modules A" "--max-average A" "{}").format(" ".join(PYTHON_DIRS))
)
@task
# Reason: pyinvoke in Python 3.5 can't use type hint:
# - Using python3 keyword-only arguments or annotated arguments causes a ValueError from inspect
# · Issue #357 · pyinvoke/invoke
# https://github.com/pyinvoke/invoke/issues/357
def test(context): # type: ignore[no-untyped-def]
"""Run tests."""
pty = platform.system() == "Linux"
context.run("pytest", pty=pty)
if sys.version_info[:3] >= (3, 6, 0):
@task(help={"publish": "Publish the result via coveralls", "xml": "Export report as xml format"})
def coverage(context: Context, publish: bool = False, xml: bool = False) -> None:
"""Create coverage report."""
context.run("coverage run --source {} -m pytest".format(SOURCE_DIR))
context.run("coverage report")
if publish:
# Publish the results via coveralls
context.run("coveralls")
return
# Build a local report
if xml:
context.run("coverage xml")
else:
context.run("coverage html")
webbrowser.open(COVERAGE_REPORT.as_uri())
@task
def clean_build(context: Context) -> None:
"""Clean up files from package building."""
context.run("rm -fr build/")
context.run("rm -fr dist/")
context.run("rm -fr .eggs/")
context.run("find . -name '*.egg-info' -exec rm -fr {} +")
context.run("find . -name '*.egg' -exec rm -f {} +")
@task
def clean_python(context: Context) -> None:
"""Clean up python file artifacts."""
context.run("find . -name '*.pyc' -exec rm -f {} +")
context.run("find . -name '*.pyo' -exec rm -f {} +")
context.run("find . -name '*~' -exec rm -f {} +")
context.run("find . -name '__pycache__' -exec rm -fr {} +")
@task
def clean_tests(_context: Context) -> None:
"""Clean up files from testing."""
_delete_file(COVERAGE_FILE)
shutil.rmtree(COVERAGE_DIR, ignore_errors=True)
@task(pre=[clean_build, clean_python, clean_tests])
def clean(_context: Context) -> None:
"""Runs all clean sub-tasks."""
@task(clean)
def dist(context: Context) -> None:
"""Build source and wheel packages."""
context.run("python -m build")