-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
150 lines (125 loc) · 3.89 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
# mypy: allow-untyped-defs
from itertools import chain
from pathlib import Path
from shutil import rmtree
from invoke import UnexpectedExit, task
TOP_DIR = Path(__file__).parent
DOC_DIR = TOP_DIR / "docs"
SRC_DIR = TOP_DIR / "src"
SRC_ENV = {"PYTHONPATH": str(SRC_DIR)}
def source_arg(pattern):
"""Converts a source pattern to a command line argument."""
if pattern is None:
paths = chain(
SRC_DIR.glob("**/*.py"),
(TOP_DIR / "tests").glob("**/*.py"),
[Path(__file__)],
)
else:
paths = Path.cwd().glob(pattern)
for path in paths:
yield str(path)
def remove_dir(path):
"""Recursively removes a directory."""
if path.exists():
rmtree(path)
@task
def clean(c):
"""Clean up our output."""
print("Cleaning up...")
remove_dir(DOC_DIR)
doctest_report = TOP_DIR / "doctest.html"
if doctest_report.is_file():
doctest_report.unlink()
@task
def lint(c, src=None, html=None):
"""Check sources with PyLint."""
print("Checking sources with PyLint...")
report_dir = TOP_DIR
cmd = ["pylint"]
sources = set(source_arg(src))
sources.remove(__file__)
cmd += sources
if html is not None:
json_file = report_dir / "pylint.json"
cmd += [
"--load-plugins=pylint_json2html",
"--output-format=jsonextended",
f">{json_file}",
]
with c.cd(str(TOP_DIR)):
c.run(" ".join(cmd), env=SRC_ENV, warn=True, pty=True)
if html is not None:
c.run(f"pylint-json2html -f jsonextended -o {html} {json_file}")
@task
def types(c, src=None, clean=False, report=False):
"""Check sources with mypy."""
if clean:
print("Clearing mypy cache...")
remove_dir(TOP_DIR / ".mypy_cache")
print("Checking sources with mypy...")
report_dir = None
cmd = ["mypy"]
if report:
if report_dir is None:
mypy_report = TOP_DIR / "mypy-report"
else:
mypy_report = report_dir / "mypy-coverage"
remove_dir(mypy_report)
cmd.append(f"--html-report {mypy_report}")
sources = set(source_arg(src))
sources.remove(__file__)
cmd += sources
out_path = None if report_dir is None else report_dir / "mypy-log.txt"
out_stream = None if out_path is None else open(out_path, "w", encoding="utf-8")
try:
with c.cd(str(TOP_DIR)):
try:
c.run(" ".join(cmd), env=SRC_ENV, out_stream=out_stream, pty=True)
except UnexpectedExit as ex:
if ex.result.exited < 0:
print(ex)
finally:
if out_stream is not None:
out_stream.close()
@task
def readme(c):
"""Render README.md to HTML."""
print("Rendering README...")
DOC_DIR.mkdir(exist_ok=True)
c.run(f"markdown_py -f {DOC_DIR / 'README.html'} {TOP_DIR / 'README.md'}")
@task
def apidocs(c):
"""Generate documentation as HTML files."""
apiDir = DOC_DIR / "api"
remove_dir(apiDir)
apiDir.mkdir(parents=True)
cmd = [
"pydoctor",
"--make-html",
f"--html-output={apiDir}",
"--project-name=APE",
"--project-url=https://boxingbeetle.com/tools/ape/",
"--intersphinx=https://docs.python.org/3/objects.inv",
f"{SRC_DIR}/apetest",
]
c.run(" ".join(cmd))
@task(post=[apidocs, readme])
def docs(c):
"""Generate documentation as HTML files."""
@task(pre=[apidocs])
def doctest(c):
"""Check our documentation using APE."""
c.run("apetest --check launch docs/api/apetest doctest.html")
@task
def unittest(c, junit_xml=None):
"""Run unit tests."""
args = ["pytest"]
if junit_xml is not None:
args.append(f"--junit-xml={junit_xml}")
args.append("tests")
with c.cd(str(TOP_DIR)):
c.run(" ".join(args), env=SRC_ENV, pty=True)
@task(post=[doctest, unittest, lint])
def test(c):
"""Run all tests."""