-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabfile.py
executable file
·160 lines (125 loc) · 5.61 KB
/
labfile.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
#!/usr/bin/env python3
import shutil
import sys
from pathlib import Path
from typing import List
import coverage as coverage_
import mypy.api
import pytest
from flake8.api import legacy as flake8
from sphinx.application import Sphinx
import alkymi as alk # NOQA: This has to happen after coverage start
# Glob all source and test files and make them available as recipe outputs
source_files = alk.recipes.glob_files("source_files", Path("alkymi"), "*.py", recursive=True)
example_files = alk.recipes.glob_files("example_files", Path("examples"), "*.py", recursive=True)
test_files = alk.recipes.glob_files("test_files", Path("tests"), "test_*.py", recursive=True)
# Also run linting and type checking on this file itself
labfile = alk.recipes.file("get_labfile", Path("labfile.py"))
@alk.recipe(transient=True)
def test(test_files: List[Path]) -> None:
"""
Run all alkymi unit tests
:param test_files: The pytest files to execute
"""
# Run the tests on a separate thread to avoid asyncio event-loop issues (simulates normal pytest execution)
result = alk.utils.run_on_thread(lambda: pytest.main([str(f) for f in test_files]))()
if result != pytest.ExitCode.OK:
exit(1)
@alk.recipe(transient=True)
def coverage(test_files: List[Path]) -> None:
"""
Run all alkymi unit tests while capturing test coverage data
:param test_files: The pytest files to execute to generate test coverage data
"""
# Because alkymi was already imported to run this script, we need to delete it before running coverage. Otherwise,
# coverage won't see the lines hit by import statements, resulting in a false coverage value. The test files will
# automatically reimport everything needed from alkymi once executed by pytest
for module in list(sys.modules.keys()):
if "alkymi" in module:
del sys.modules[module]
# Start coverage and run tests - note that debugging will not work from this point on
cov = coverage_.coverage(source=["alkymi"], check_preimported=True)
cov.start()
test(test_files)
cov.stop()
cov.xml_report()
cov.report(show_missing=True)
@alk.recipe(transient=True)
def lint(source_files: List[Path], example_files: List[Path], test_files: List[Path], labfile: Path) -> None:
"""
Lint all alkymi source, example and test files using flake8
:param source_files: The alkymi source files to type check
:param example_files: The alkymi examples files to type check
:param test_files: The alkymi unit test files to type check
:param labfile: This file itself to type check
"""
style_guide = flake8.get_style_guide(max_line_length=120, max_complexity=10, ignore=["E303"])
all_files = source_files + example_files + test_files + [labfile]
report = style_guide.check_files([str(file) for file in all_files])
if report.get_statistics("E"):
exit(1)
print("Flake8 found no style violations in {} files".format(len(all_files)), file=sys.stderr)
@alk.recipe(transient=True)
def type_check(source_files: List[Path], example_files: List[Path], test_files: List[Path], labfile: Path) -> None:
"""
Type check all alkymi source, example and test files using mypy
:param source_files: The alkymi source files to type check
:param example_files: The alkymi examples files to type check
:param test_files: The alkymi unit test files to type check
:param labfile: This file itself to type check
"""
all_files = source_files + example_files + test_files + [labfile]
args = [str(file) for file in all_files]
stdout, stderr, return_code = mypy.api.run(args)
assert not stderr, stderr
# Print the actual results of type checking
print("Type checking report:")
print(stdout)
# If no violations were found, mypy will return 0 as the return code
if return_code != 0:
exit(return_code)
@alk.recipe(transient=True)
def docs() -> None:
"""
Build the documentation in docs/source using Sphinx and stores it in docs/build/index.html
"""
doc_dir = Path("docs")
source_dir = doc_dir / "source"
build_dir = doc_dir / "build"
# Explicitly set 'status' and 'warning' here to make sure progress is forwarded to Rich for formatting
sphinx = Sphinx(str(source_dir), confdir=str(source_dir), outdir=str(build_dir),
doctreedir=str(build_dir / "doctrees"), buildername="html", status=sys.stdout, warning=sys.stderr)
sphinx.build()
@alk.recipe(transient=True)
def build() -> Path:
"""
Builds the distributions (source + wheel) for alkymi
:return: The directory holding the outputs
"""
# Clean output dir first
dist_dir = Path("dist")
if dist_dir.exists():
shutil.rmtree(dist_dir)
dist_dir.mkdir(exist_ok=False)
alk.utils.call(["python3", "setup.py", "sdist", "bdist_wheel"])
return dist_dir
@alk.recipe(transient=True)
def release_test(build: Path) -> None:
"""
Uploads the built alkymi distributions to the pypi test server
:param build: The build directory containing alkymi distributions to upload
"""
alk.utils.call(["python3", "-m", "twine", "upload", "--repository", "testpypi", "{}/*".format(build)])
@alk.recipe(transient=True)
def release(build: Path) -> None:
"""
Uploads the built alkymi distributions to the pypi production server
:param build: The build directory containing alkymi distributions to upload
"""
alk.utils.call(["python3", "-m", "twine", "upload", "{}/*".format(build)])
def main():
lab = alk.Lab("alkymi")
lab.add_recipes(test, coverage, lint, type_check, docs, build, release_test, release)
lab.open()
if __name__ == "__main__":
main()