forked from fabien-marty/stlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
191 lines (149 loc) · 4.4 KB
/
tasks.py
File metadata and controls
191 lines (149 loc) · 4.4 KB
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
from __future__ import annotations
import difflib
import os
import sys
from typing import Any
import jinja2
from dunamai import Style, Version
from invoke import task
from yaml import Loader, load
PACKAGE = "stlog"
def _clean_core(c):
c.run(
"rm -Rf .*_cache build ; find . -type d -name __pycache__ -exec rm -Rf {} \\; 2>/dev/null"
)
def _clean_coverage(c):
c.run("rm -Rf htmlcov")
def _clean_apidoc(c):
c.run("rm -Rf apihtml")
def _clean_doc(c):
c.run("rm -Rf html")
@task
def clean(c):
"""Clean the repository"""
_clean_core(c)
_clean_coverage(c)
_clean_apidoc(c)
_clean_doc(c)
@task
def doc(c):
"""Make user documentation"""
_clean_doc(c)
c.run("mkdocs build --site-dir html")
@task(
help={
"port": "TCP port to use (default: 9090)",
"bind": "IP address to bind (0.0.0.0 for all, default: 127.0.0.0.1)",
}
)
def serve_doc(c, port=9090, bind="127.0.0.1"):
"""Serve the user documentation (dev mode)"""
_clean_doc(c)
c.run(f"mkdocs serve --livereload --dirtyreload --dev-addr={bind}:{port}")
@task(help={"fix": "try to automatically fix the code (default)"})
def lint_ruff(c, fix=True):
"""Lint the code with ruff"""
if fix:
c.run("ruff . --fix")
else:
c.run("ruff .")
@task(help={"fix": "try to automatically fix the code (default)"})
def lint_black(c, fix=True):
"""Lint the code with black"""
if fix:
c.run("black .")
else:
c.run("black --check .")
@task
def lint_mypy(c):
"""Lint the code with mypy"""
c.run("mypy --check-untyped-defs .")
@task(help={"fix": "try to automatically fix the code (default)"})
def lint(c, fix=True):
"""Lint the code with all linters"""
lint_ruff(c, fix=fix)
lint_black(c, fix=fix)
lint_mypy(c)
readme(c, lint=not fix)
@task(help={"coverage": "compute converage"})
def test(c, coverage=False):
"""Execute unit tests"""
if coverage:
_clean_coverage(c)
c.run(
f"pytest --no-cov-on-fail --cov={PACKAGE} --cov-report=term --cov-report=html --cov-report=xml tests/"
)
else:
c.run("pytest .")
@task
def apidoc(c):
"""Make API doc"""
_clean_apidoc(c)
c.run(f"pdoc3 --html --output-dir=apihtml {PACKAGE}")
@task
def bump_version(c, force_version: str | None = None):
if force_version is None:
version = Version.from_git().serialize(style=Style.SemVer)
else:
version = force_version
with open("stlog/__init__.py") as f:
c = f.read()
lines = []
for line in c.splitlines():
if line.startswith("VERSION = "):
lines.append(f'VERSION = "{version}"')
else:
lines.append(line)
with open("stlog/__init__.py", "w") as g:
g.write("\n".join(lines))
print(f"Setting version={version}")
os.system(f"poetry version {version}")
@task
def readme(c, lint=False):
if lint:
print("linting readme...")
else:
print("making readme...")
os.environ["STLOG_UNIT_TESTS_MODE"] = "1"
def get_variables() -> dict[str, Any]:
with open("mkdocs.yml") as f:
data = load(f, Loader=Loader)
data["extra"]["pathprefix"] = "docs/"
return data["extra"]
env = jinja2.Environment(
loader=jinja2.FileSystemLoader("."),
extensions=["jinja2_shell_extension.ShellExtension"],
)
template = env.get_template("README.md.j2")
variables = get_variables()
res = template.render(**variables)
res = (
"""
<!-- WARNING: generated from README.md.j2, do not modify this file manually but modify README.md.j2 instead
and execute 'poetry run invoke readme' to regenerate this README.md file -->
"""
+ res
)
if lint:
with open("README.md") as f:
to_compare = f.read()
if to_compare != res:
print("README.md must be rebuilt")
print()
sys.stdout.writelines(
difflib.unified_diff(
to_compare.splitlines(),
res.splitlines(),
fromfile="README.md",
tofile="new README.md",
)
)
print()
print("use 'poetry run invoke readme' to do that")
sys.exit(1)
else:
with open("README.md", "w") as f:
f.write(res)
@task(apidoc, doc, readme)
def docs(c):
pass