-
Notifications
You must be signed in to change notification settings - Fork 111
/
tasks.py
139 lines (112 loc) · 3.54 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
#!/usr/bin/env python
import sys
import os
import logging
from invoke import task, Collection, UnexpectedExit, Failure
logger = logging.getLogger(__name__)
# Create the necessary collections (namespaces)
ns = Collection()
test = Collection("test")
ns.add_collection(test)
unit = Collection("unit")
ns.add_collection(unit)
build = Collection("build")
ns.add_collection(build)
# Build
@task
def build_package(c):
"""Build the package from the current directory contents for use with PyPi"""
c.run("python -m pip install --upgrade setuptools wheel")
c.run("python setup.py -q sdist bdist_wheel")
@task(pre=[build_package])
def install_package(c):
"""Install the package built from the current directory contents (not PyPi)"""
c.run("pip3 install -q dist/cloudtracker-*.tar.gz")
@task
def uninstall_package(c):
"""Uninstall the package"""
c.run('echo "y" | pip3 uninstall cloudtracker', pty=True)
c.run("rm -rf dist/*", pty=True)
@task(pre=[install_package])
def help_check(c):
"""Print the version to make sure the package installation didn't irrationally break"""
try:
c.run("./bin/cloudtracker --help", pty=True)
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
# TEST - format
@task
def fmt(c):
"""Auto format code with Python autopep8"""
try:
c.run("autopep8 cloudtracker/")
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
# TEST - LINT
@task
def run_linter(c):
"""Lint the code"""
try:
c.run("pylint cloudtracker/", warn=False)
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
# TEST - SECURITY
@task
def security_scan(c):
"""Runs `bandit` and `safety check`"""
try:
c.run("bandit -r cloudtracker/")
# c.run("safety check")
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
# UNIT TESTING
@task
def run_nosetests(c):
"""Unit testing: Runs unit tests using `nosetests`"""
c.run('echo "Running Unit tests"')
try:
c.run("nosetests -v --logging-level=CRITICAL")
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
@task
def run_pytest(c):
"""Unit testing: Runs unit tests with pytest and coverage"""
c.run('echo "Running Unit tests"')
try:
c.run("python -m coverage run -m pytest -v")
c.run("python -m coverage report -m")
except UnexpectedExit as u_e:
logger.critical(f"FAIL! UnexpectedExit: {u_e}")
sys.exit(1)
except Failure as f_e:
logger.critical(f"FAIL: Failure: {f_e}")
sys.exit(1)
build.add_task(build_package, "build")
build.add_task(install_package, "install")
build.add_task(uninstall_package, "uninstall")
unit.add_task(run_nosetests, "nose")
unit.add_task(run_pytest, "pytest")
test.add_task(run_linter, "lint")
test.add_task(fmt, "format")
test.add_task(security_scan, "security")
test.add_task(help_check, "help")