-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
147 lines (120 loc) · 3.49 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
from invoke import Context, task
@task
def run(_):
# type: (Context) -> None
from yaex import append, delete, go_to_first_line, move, search, yaex
with open("README.md") as f:
readme_data = f.read()
readme_code = yaex(
append(readme_data),
delete(),
search(r"```python").in_reverse(),
delete().from_range(go_to_first_line(), move(0)),
)
exec(readme_code) # nosec
@task(aliases=("fmt",))
def format(c, all_files=False):
# type: (Context, bool) -> None
precommit_options = []
if all_files:
precommit_options.append("--all-files")
hooks = [
"trailing-whitespace",
"end-of-file-fixer",
"pyupgrade",
"add-trailing-comma",
"yesqa",
"isort",
"black",
]
for hook in hooks:
cmd = " ".join(["pre-commit", "run", *precommit_options, hook])
c.run(cmd)
@task
def lint(c, all_files=False):
# type: (Context, bool) -> None
precommit_options = []
if all_files:
precommit_options.append("--all-files")
hooks = [
"check-ast",
"debug-statements",
"name-tests-test",
"check-merge-conflict",
"check-added-large-files",
"detect-private-key",
"flake8",
"mypy",
"vulture",
"bandit",
]
for hook in hooks:
cmd = " ".join(["pre-commit", "run", *precommit_options, hook])
c.run(cmd)
@task
def tests(c, watch=False, quiet=False):
# type: (Context, bool, bool) -> None
pytest_options: list[str] = []
if quiet:
pytest_options.append("-q")
if watch:
_watch_tests(c, pytest_options)
else:
_run_pytest(c, pytest_options)
def _watch_tests(c: Context, pytest_options: list[str]) -> None:
import time
from watchdog.events import ( # type: ignore
EVENT_TYPE_CREATED,
EVENT_TYPE_DELETED,
EVENT_TYPE_MODIFIED,
EVENT_TYPE_MOVED,
RegexMatchingEventHandler,
)
from watchdog.observers import Observer # type: ignore
class MyEventHandler(RegexMatchingEventHandler): # type: ignore
def __init__(self, *args, **kwargs) -> None: # type: ignore
super().__init__(*args, **kwargs)
self.has_modified = True
def on_any_event(self, event): # type: ignore
super().on_any_event(event)
events_to_watch = [
EVENT_TYPE_CREATED,
EVENT_TYPE_DELETED,
EVENT_TYPE_MODIFIED,
EVENT_TYPE_MOVED,
]
if event.event_type in events_to_watch:
self.has_modified = True
def tick(self): # type: ignore
if self.has_modified:
_run_pytest(c, pytest_options)
self.has_modified = False
event_handler = MyEventHandler(
regexes=[r".*\.py$"],
ignore_directories=True,
)
observer = Observer()
observer.schedule(event_handler, ".", recursive=True)
observer.start()
try:
while True:
event_handler.tick() # type: ignore
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def _run_pytest(c: Context, pytest_options: list[str]) -> None:
cmd = " ".join(
[
"coverage",
"run",
"-m",
"pytest",
*pytest_options,
],
)
c.run(cmd, warn=True)
@task
def coverage(c):
# type: (Context) -> None
c.run("coverage report")