-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
430 lines (362 loc) · 11.5 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
"""
Tasks for maintaining the project.
Execute 'invoke --list' for guidance on using Invoke
"""
# Core Library modules
import logging.config
import shutil
import webbrowser
from pathlib import Path
# Third party modules
import yaml
from invoke import call, task
from jinja2 import Template
ROOT_DIR = Path(__file__).parent
BUILD_FROM = "".join(['"', str(ROOT_DIR / "."), '"'])
DIST_SOURCE = "".join(['"', str(ROOT_DIR / "dist/*"), '"'])
PYPIRC = "".join(['"', str(ROOT_DIR / ".pypirc"), '"'])
DOC_DIR_STR = "".join(['"', str(ROOT_DIR / "docs"), '"'])
DOCS_BUILD_DIR_STR = "".join(['"', str(ROOT_DIR / "docs" / "_build"), '"'])
DOCS_INDEX = "".join(['"', str(ROOT_DIR / "docs" / "_build" / "index.html"), '"'])
LOG_DIR = ROOT_DIR.joinpath("logs")
TEST_DIR = ROOT_DIR.joinpath("tests")
SRC_DIR = ROOT_DIR.joinpath("src")
PKG_DIR = SRC_DIR.joinpath("piptools_sync")
PYTHON_FILES_ALL = list(ROOT_DIR.rglob("*.py"))
PYTHON_FILES_ALL.remove(ROOT_DIR / "tasks.py")
PYTHON_FILES_ALL_STR = ""
for file in PYTHON_FILES_ALL:
PYTHON_FILES_ALL_STR = "".join([PYTHON_FILES_ALL_STR, '"', str(file), '" '])
PYTHON_FILES_SRC = list(SRC_DIR.rglob("*.py"))
PYTHON_FILES_SRC_STR = ""
for file in PYTHON_FILES_SRC:
PYTHON_FILES_SRC_STR = "".join([PYTHON_FILES_SRC_STR, '"', str(file), '" '])
if LOG_DIR / "tasks.log":
Path.unlink(LOG_DIR / "tasks.log", missing_ok=True)
LOGGING_CONFIG_TEMPLATE = """
version: 1
disable_existing_loggers: False
formatters:
basic:
style: "{"
format: "{levelname:s}:{name:s}:{message:s}"
timestamp:
style: "{"
format: "{asctime} - {levelname} - {name} - {message}"
datefmt: "%Y-%m-%d+%H:%M:%S"
handlers:
console:
class: logging.StreamHandler
level: INFO
stream: ext://sys.stdout
formatter: basic
file:
class: logging.FileHandler
level: DEBUG
filename: {{ LOG_FILE }}
encoding: utf-8
formatter: timestamp
loggers:
main:
handlers: [console, file]
level: DEBUG
propagate: False
"""
LOGGING_CONFIG = Template(LOGGING_CONFIG_TEMPLATE).render(
LOG_FILE=str(LOG_DIR / "tasks.log")
)
logging.config.dictConfig(yaml.safe_load(LOGGING_CONFIG))
logger = logging.getLogger("main")
logger.debug("Total python files: %s", len(PYTHON_FILES_ALL))
for file in PYTHON_FILES_ALL:
logger.debug("%s", file)
logger.debug("src python files: %s", len(PYTHON_FILES_SRC))
for file in PYTHON_FILES_SRC:
logger.debug("%s", file)
def _delete_director(items_to_delete):
"""Utility function to delete files or directories."""
for item in items_to_delete:
if item.is_dir():
logger.debug("Deleting Directory: %s", item)
shutil.rmtree(item, ignore_errors=True)
elif item.is_file():
logger.debug("Deleting File: %s", item)
Path.unlink(item, missing_ok=True)
else:
raise ValueError(f"{item} is not a directory or a file")
def _finder(directory, item, exclusions):
"""Utility function to generate a Path list of files based on globs."""
item_list = list(directory.rglob(item))
logger.debug("for %s : Found: %s", item, item_list)
for exc in exclusions:
logger.debug("removing exclusion: %s", exc)
if exc in item_list:
item_list.remove(exc)
if item_list:
logger.debug("Items to process: %s", item_list)
_delete_director(item_list)
def _clean_mypy():
"""Clean up mypy cache and results."""
patterns = [
".mypy_cache",
"mypy-report",
]
excludes = []
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_build():
"""Clean up build artifacts."""
# Specify glob patterns to delete
patterns = [
"build/",
"dist/",
".eggs/",
"*egg-info",
"*.egg",
]
# specify pathlib objects to exclude from deletion (can be directories of files)
excludes = [
SRC_DIR / "piptools_sync.egg-info/",
]
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_test():
"""Clean up test artifacts."""
patterns = [
"assets",
"coverage",
"mypy",
".pytest_cache",
"htmlcov",
".coverage",
".tox",
"coverage.xml",
"coverage.html",
"pytest.html",
"coverage.html",
]
excludes = [ROOT_DIR / "assets", ROOT_DIR / "docs" / "assets"]
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_python():
"""Clean up python file artifacts."""
patterns = ["*.pyc", "*.pyo", "__pycache__"]
excludes = []
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_docs():
"""Clean the document build."""
patterns = ["_build", "jupyter_execute", "*.css"]
excludes = []
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_logs():
"""Clean the log files."""
patterns = ["*.log"]
excludes = [
LOG_DIR / "tasks.log",
]
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_bandit():
"""Clean the bandit report files."""
patterns = ["bandit.html"]
excludes = []
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
def _clean_flake8():
"""Clean the bandit report files."""
patterns = [
"flake-report/",
"*report.html",
"*source.html",
]
excludes = []
for pattern in patterns:
_finder(ROOT_DIR, pattern, excludes)
@task
def clean(c):
"""Removes all test, build, log and lint artifacts from the environment."""
_clean_bandit()
_clean_mypy()
_clean_build()
_clean_python()
_clean_test()
_clean_docs()
_clean_logs()
_clean_flake8()
@task(
name="lint-isort",
aliases=[
"isort",
"is",
],
help={
"check": "Checks if source is formatted without applying changes",
"all-files": "Selects all files to be scanned. Default is 'src' only",
},
)
def lint_isort(c, check=False, all_files=False):
"""Run isort against selected python files."""
isort_options = ["--check-only", "--diff"] if check else []
if all_files:
c.run(f"isort {' '.join(isort_options)} {PYTHON_FILES_ALL_STR}")
else:
c.run(f"isort {' '.join(isort_options)} {PYTHON_FILES_SRC_STR}")
@task(
name="lint-black",
aliases=[
"black",
"bl",
],
help={
"check": "Checks if source is formatted without applying changes",
"all-files": "Selects all files to be scanned. Default is 'src' only",
},
optional=["all_files"],
)
def lint_black(c, check=False, all_files=False):
"""Runs black formatter against selected python files."""
black_options = ["--diff", "--check"] if check else []
if all_files:
c.run(f"black {' '.join(black_options)} {PYTHON_FILES_ALL_STR}")
else:
c.run(f"black {' '.join(black_options)} {PYTHON_FILES_SRC_STR}")
@task(
name="lint-flake8",
aliases=[
"flake8",
"fl",
],
help={
"all-files": "Selects all files to be scanned. Default is 'src' only",
"open_browser": "Open the mypy report in the web browser",
},
optional=["all_files"],
)
def lint_flake8(c, open_browser=False, all_files=False):
"""Run flake8 against selected files."""
_clean_flake8()
if all_files:
c.run(f"flake8 --format=html --htmldir=flake-report {PYTHON_FILES_ALL_STR}")
else:
c.run(f"flake8 --format=html --htmldir=flake-report {PYTHON_FILES_SRC_STR}")
if open_browser:
report_path = "".join(['"', str(ROOT_DIR / "flake-report" / "index.html"), '"'])
webbrowser.open(report_path)
@task(pre=[lint_isort, lint_black, lint_flake8])
def lint(c):
"""Run all lint tasks on 'src' files only."""
@task(
pre=[
call(lint_isort, all_files=True),
call(lint_black, all_files=True),
call(lint_flake8, all_files=True),
]
)
def lint_all(c):
"""Run all lint tasks on all files."""
@task(
help={
"open_browser": "Open the mypy report in the web browser",
"all-files": "Selects all files to be scanned. Default is 'src' only",
},
)
def mypy(c, open_browser=False, all_files=False):
"""Run mypy against selected python files."""
_clean_mypy()
if all_files:
c.run(f"mypy {PYTHON_FILES_ALL_STR}")
else:
c.run(f"mypy {PYTHON_FILES_SRC_STR}")
if open_browser:
report_path = "".join(['"', str(ROOT_DIR / "mypy-report" / "index.html"), '"'])
webbrowser.open(report_path)
@task
def safety(c):
"""Runs safety to check for insecure requirements."""
c.run("safety check --full-report")
@task(
name="bandit",
help={
"open_browser": "Open the bandit report in the web browser",
"all-files": "Selects all files to be scanned. Default is 'src' only",
},
)
def bandit(c, open_browser=False, all_files=False):
"""Runs bandit against selected python files."""
_clean_bandit()
bandit_options = ["--format html", "--output bandit.html", "--skip B101,B603"]
if all_files:
c.run(f"bandit {' '.join(bandit_options)} {PYTHON_FILES_ALL_STR}")
else:
c.run(f"bandit {' '.join(bandit_options)} {PYTHON_FILES_SRC_STR}")
if open_browser:
report_path = "".join(['"', str(ROOT_DIR / "bandit.html"), '"'])
webbrowser.open(report_path)
@task(pre=[safety, bandit])
def secure(c):
"""Runs all security tools."""
@task(
help={
"open_browser": "Open the test report in the web browser",
},
)
def tests(c, open_browser=False):
"""Run tests using pytest."""
_clean_test()
print(TEST_DIR)
c.run(
f'pytest "{str(TEST_DIR)}" --cov=piptools_sync --cov-report=html'
f" --html=pytest-report.html -ra"
)
if open_browser:
pytest_path = "".join(['"', str(ROOT_DIR / "pytest-report.html"), '"'])
cov_path = "".join(['"', str(ROOT_DIR / "htmlcov" / "index.html"), '"'])
webbrowser.open(pytest_path)
webbrowser.open(cov_path)
@task(
help={
"open_browser": "Open the docs in the web browser",
},
)
def docs(c, open_browser=False):
"""Build documentation."""
_clean_docs()
build_docs = f"sphinx-build -b html {DOC_DIR_STR} {DOCS_BUILD_DIR_STR}"
c.run(build_docs)
if open_browser:
webbrowser.open(DOCS_INDEX)
@task
def build(c):
"""Creates a new sdist & wheel build using the PyPA tool."""
_clean_build()
c.run(f"python -m build --sdist --wheel {BUILD_FROM}")
@task(pre=[build])
def pypi_test(c):
"""Uploads a build to the PyPI-test python repository."""
c.run(f"python -m twine upload --config-file {PYPIRC} -r testpypi {DIST_SOURCE}")
@task(pre=[build])
def pypi(c):
"""Uploads a build to the PyPI python repository."""
c.run(f"python -m twine upload --config-file {PYPIRC} {DIST_SOURCE}")
@task(pre=[pypi, pypi_test])
def publish(c):
"""Uploads a build to the PyPI-test and PyPI python repositories."""
@task
def psr(c):
"""Runs semantic-release publish."""
_clean_build()
c.run("semantic-release publish")
@task
def update(c):
"""Updates the development environment"""
c.run("pre-commit clean")
c.run("pre-commit gc")
c.run("pre-commit autoupdate")
c.run("pip-compile -q -r -U --allow-unsafe requirements/base.in")
c.run("pip-compile -q -r -U --allow-unsafe requirements/development.in")
c.run("pip-compile -q -r -U --allow-unsafe requirements/production.in")
c.run("pip-compile -q -r -U --allow-unsafe requirements/test.in")
c.run("pip-sync")