-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
159 lines (121 loc) · 4.44 KB
/
build.py
File metadata and controls
159 lines (121 loc) · 4.44 KB
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
import os
import sys
import logging
from collections import namedtuple
from pynt import task
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import time
import pexpect
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
SOURCE_FOLDERS = ['src']
@task()
def flake():
print('flake8 check')
result = _execute_sh('flake8 src')
if result.exitstatus != 0:
print_result_text('Flake errors detected, see above', ShColor.FAIL)
else:
print_result_text('Flake check passed', ShColor.OKGREEN)
@task(flake)
def test(test_identifier=None):
"""Runs our unit tests"""
# UNIT_TESTING environment variable changes some decorators
# to be pass throughs
os.environ['UNIT_TESTING'] = 'unit testing'
# jumping through a few hoops to getting coloured text printed out
if test_identifier is None:
# Discover tests in all of the source folders
src_args = []
for folder in SOURCE_FOLDERS:
src_args.append('-s {0}'.format(folder))
test_str = ' '.join(src_args)
else:
print('Running with test identifier : ' + test_identifier)
test_str = test_identifier
result = _execute_sh('py.test {0} --junitxml=test_results/junit_results.xml'.format(test_str))
# Report to the outside world that the tests have failed
if result.exitstatus != 0:
exit(result.exitstatus)
def create_observer(handler, path):
observer = Observer()
observer.schedule(handler, path, recursive=True)
return observer
@task()
def watchtest(test_identifier=None):
'''Watching files for changes and runs tests'''
class WatchTestsEventHandler(PatternMatchingEventHandler):
patterns = ["*.py"]
def run_tests(self, event):
# delete the pyc file
try:
file_to_remove = './' + event.src_path + 'c'
os.remove(file_to_remove)
print('Deleted pyc file ' + file_to_remove)
except OSError:
# it's ok if the file does not exist
print('Failed to delete file ' + file_to_remove)
try:
_execute_sh("pynt 'test[{0}]'".format(test_identifier if test_identifier is not None else ''))
except:
root.exception('Error running tests')
def on_modified(self, event):
self.run_tests(event)
def on_created(self, event):
self.run_tests(event)
handler = WatchTestsEventHandler()
observers = []
for folder in SOURCE_FOLDERS:
observers.append(create_observer(handler, folder))
for ob in observers:
ob.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
for ob in observers:
ob.stop()
ob.join()
# Utility stuff ----------------------------
class ShColor:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ShellResult = namedtuple('ShellResult', 'output exitstatus signalstatus')
class ExecuteShellError(Exception):
pass
# Something simple to make sure python 2 and python 3 are both handled
class StdOutBytesToFile(object):
def write(self, str_or_bytes_to_write):
if isinstance(str_or_bytes_to_write, str):
return sys.stdout.write(str_or_bytes_to_write)
return sys.stdout.write(str_or_bytes_to_write.decode(sys.stdout.encoding))
def flush(self):
return sys.stdout.flush()
def _execute_sh(cmd, abort_on_error=False):
"""Execute a shell command"""
child = pexpect.spawn(cmd)
# redirect the stdout of child to parent
child.logfile = StdOutBytesToFile()
child.expect(pexpect.EOF, timeout=1200)
if child.isalive():
child.wait()
if abort_on_error:
if child.exitstatus != 0:
raise ExecuteShellError('Error executing command: {0}'.format(cmd))
return ShellResult(output=child.before,
exitstatus=child.exitstatus,
signalstatus=child.signalstatus)
def print_result_text(text, color):
print('{3}{0}============= {1} ============={2}'.format(color, text, ShColor.ENDC, ShColor.BOLD))