-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
148 lines (121 loc) · 4.09 KB
/
noxfile.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
import nox
import sys
import os
import shutil
import re
import json
from pathlib import Path
sys.path.append(os.path.dirname(__file__))
def split_cmd(cmd: str):
return cmd.strip().split(" ")
test_deps = [
'.',
'coverage'
]
class Tasks:
test = lambda posargs='': 'python -m unittest discover'
test_integration = lambda: "python ./tests/run_tests.py"
coverage = lambda posargs: f'coverage {posargs}'
build_wheel = 'python -m setup bdist_wheel'
send = r'twine upload dist/*.whl -u __token__ -p'
docs = 'pdoc kalash -o .'
quality = 'flake8'
@nox.session()
def test(session: nox.Session):
"""Run unit tests under coverage (use `nocov` arg to run without coverage)"""
session.install(*test_deps)
if session.posargs:
if 'nocov' in session.posargs:
# run tests without coverage:
session.run('python', '-m', *split_cmd(Tasks.test()), *session.posargs)
return
# run tests with coverage:
session.run(*split_cmd(Tasks.coverage('erase')))
session.run(
*split_cmd(Tasks.coverage('run -m unittest discover')),
*session.posargs
)
@nox.session()
def cov_report(session: nox.Session):
"""Combine coverage.py files and generate an XML report"""
session.install(
'coverage'
)
session.run(*split_cmd(Tasks.coverage('xml')), '-i')
def _copy_and_sanitize_paths_for_pdoc3(doc_source_path, target_path):
if not os.path.exists(target_path):
shutil.copytree(doc_source_path, target_path)
else:
shutil.rmtree(target_path)
shutil.copytree(doc_source_path, target_path)
for root, dirs, files in os.walk(target_path):
for file in files:
if file.endswith('.md'):
file_full_path = str(Path(root) / file)
with open(file_full_path, 'r') as f:
text = f.read()
# match [blah](blah.md#blah-blah-blah)
pattern = r'(\[.+?\]\()(.+?)(\.md)(\#.+?\))'
replacement_text = re.sub(pattern, r'\g<1>\g<4>', text)
with open(file_full_path, 'w') as f:
f.write(replacement_text)
@nox.session()
def quality(session: nox.Session):
session.install('flake8')
session.run(Tasks.quality)
@nox.session()
def json_schema(session: nox.Session):
session.install('.')
from kalash.config import Trigger
with open('kalash/spec.schema.json', 'w') as f:
json.dump(Trigger.json_schema(), f, indent=4)
@nox.session()
def docs(session: nox.Session):
"""Build HTML documentation."""
built_docs_dir = 'kalash/built_docs'
pdoc_dir = 'kalash/pdoc'
res_dir = 'kalash/res'
target_res = Path(built_docs_dir) / 'html' / 'res'
if not os.path.exists(built_docs_dir):
os.makedirs(built_docs_dir)
_copy_and_sanitize_paths_for_pdoc3('kalash/doc', pdoc_dir)
session.install('pdoc', '.')
cwd = os.getcwd()
session.chdir(built_docs_dir)
session.run(*split_cmd(Tasks.docs))
session.chdir(cwd)
if os.path.exists(target_res):
shutil.rmtree(target_res)
shutil.copytree(res_dir, target_res)
@nox.session()
def whl(session: nox.Session):
"""Build wheel (remember to build docs first)"""
session.install(
'wheel',
'setuptools',
'.'
)
session.run(*split_cmd(Tasks.build_wheel))
@nox.session()
def send(session: nox.Session):
"""Send wheels from the `dist` folder to Technica Nexus"""
session.install(
'twine'
)
token = os.environ.get("TWINE_TOKEN")
if not token:
raise ValueError("PyPi upload token not set!")
session.run(*split_cmd(Tasks.send), token)
@nox.session()
def clean(session: nox.Session):
"""Remove potentially lingering files"""
to_delete = [
".coverage",
"kalash_reports"
]
for p in to_delete:
if os.path.exists(p):
if os.path.isdir(p):
shutil.rmtree(p)
else:
os.remove(p)