-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
154 lines (133 loc) · 4.17 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
"""Module of Invoke tasks regarding CODE QUALITY to be invoked from the command line. Try
invoke --list
from the command line for a list of all available commands.
"""
import os
import shutil
from invoke import task
POSIX = os.name == "posix"
# we test these one at a time, b/c we get a ImportPathMismatchError with doctest and pytest from modules called
# the same thing in different packages (such as a wrapper.py docs and in boa)
TEST_DIRECTORIES = [
"boa",
# "docs", we have no doctests in docs yet, so this running returns none, which fails, which isn't what we want
"tests",
]
@task
def black(command, checkonly=False):
"""Runs black (autoformatter) on all .py files recursively
if checkonly=True, only checks if would change files
"""
print(
"""
Running Black the Python code formatter
=======================================
"""
)
cmd = "black --check --diff ." if checkonly else "black ."
command.run(cmd, echo=True, pty=POSIX)
@task
def isort(command, checkonly=False):
"""Runs isort (import sorter) on all .py files recursively
if checkonly=True, only checks if would change files
"""
print(
"""
Running isort the Python code import sorter
===========================================
"""
)
cmd = "isort --check-only --diff ." if checkonly else "isort ."
command.run(cmd, echo=True, pty=POSIX)
@task
def lint(
command,
):
"""Runs flake8 plugin flakeheaven (linter) on all .py files recursively"""
print(
"""
Running flakeheaven, a Python code linter
===================================
"""
)
command.run("flakeheaven lint", echo=True, pty=POSIX)
@task
def style(command, checkonly=False):
"""Runs black, isort, and flake8
if checkonly=True, only checks if would change files
"""
black(command, checkonly=checkonly)
isort(command, checkonly=checkonly)
lint(command)
# Only prints if doesn't exit from the above not failing out
print(
"""
All Style Checks Passed Successfully
====================================
"""
)
@task
def test_dir(command, options="", dir_="."):
"""Runs pytest to identify failing tests and doctests"""
print(
"""
Running pytest the test framework
=================================
"""
)
command.run(f"python -m pytest -v {options} {dir_}", echo=True, pty=POSIX)
@task(aliases=["tests"])
def test(command, options=""):
"""Runs pytest to identify failing tests and doctests"""
print(
"""
Running pytest the test framework
=================================
"""
)
for dir_ in TEST_DIRECTORIES:
test_dir(command, options=options, dir_=dir_)
# command.run(f"python -m pytest {options} {' '.join(dir_ for dir_ in TEST_DIRECTORIES)}", echo=True, pty=POSIX)
print(
"""
All Testing Directories Passed Successfully
===========================================
"""
)
@task
def docs(command, warn_is_error=False, options=""):
"""Runs Sphinx to build the docs locally for testing"""
print(
"""
Running Sphinx to test the docs building
========================================
"""
)
o = "-W " if warn_is_error else ""
if "-W" in options:
options = options.replace("-W", "")
options = options + " " + o
shutil.rmtree("docs/_build", ignore_errors=True)
shutil.rmtree("docs/api", ignore_errors=True)
shutil.rmtree("docs/code_reference/api", ignore_errors=True)
shutil.rmtree("docs/jupyter_execute", ignore_errors=True)
shutil.rmtree("docs/examples/default_config.yaml", ignore_errors=True)
command.run("python -m boa.config --output-path docs/examples/default_config.yaml", echo=True, pty=POSIX)
command.run(f"sphinx-build {options} -b html docs docs/_build", echo=True, pty=POSIX)
@task(pre=[black, isort, lint, test, docs])
def all(
command,
):
"""Runs black, isort, flake8, and pytest
Arguments:
command {[type]} -- [description]
"""
# If we get to this point all tests listed in 'pre' have passed
# unless we have run the task with the --warn flag
if not command.config.run.warn:
print(
"""
All Checks Passed Successfully
==========================================
"""
)