/
toxdog.py
274 lines (230 loc) · 9.71 KB
/
toxdog.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
""" Automatically run tox jobs for real-time feedback on changes. """
# Copyright 2017 Seth Michael Larson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = 'Seth Michael Larson'
__email__ = 'sethmichaellarson@protonmail.com'
__license__ = 'Apache-2.0'
__version__ = '1.0.0'
import argparse
import multiprocessing
import os
import sys
try:
import subprocess32 as subprocess
except ImportError:
import subprocess
import time
import threading
from colorama import init, Fore, Style
from tox.config import parseconfig
from watchdog.observers.polling import PollingObserver as Observer
from watchdog.events import (RegexMatchingEventHandler, FileSystemEvent, EVENT_TYPE_DELETED)
init()
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
DEVNULL = open(os.devnull, 'a+')
class ToxProcess(object):
def __init__(self, env, path):
self.env = env
self.proc = subprocess.Popen('tox -e %s' % env,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=path,
env=os.environ)
self.output = b''
self._buffer = b''
@property
def exit_status(self):
return self.proc.returncode
def poll(self):
start_value = self.proc.returncode
try:
self.proc.poll()
except Exception:
return False
return self.proc.returncode is not start_value
def terminate(self):
if self.proc:
self.proc.terminate()
self.proc = None
class ToxdogThread(threading.Thread):
def __init__(self, queue, path, envs, omit_envs):
super(ToxdogThread, self).__init__()
self.queue = queue
self.path = path
self.envs = envs
self.omit_envs = omit_envs
self.running = True
self.tox_procs = {}
self.tox_waiting_envs = []
self.lock = threading.Lock()
self.reason = ''
self.max_concurrent = min(4, multiprocessing.cpu_count())
def run(self):
while self.running:
try:
event = self.queue.get_nowait()
if event is None:
self.start_processes(Fore.LIGHTCYAN_EX + 'INITIAL' + Fore.LIGHTWHITE_EX)
else:
assert isinstance(event, FileSystemEvent)
path = os.path.relpath(event.src_path, self.path)
if event.event_type == EVENT_TYPE_DELETED:
action = 'DELETE'
else:
action = 'MODIFY'
action = Fore.LIGHTCYAN_EX + action + Fore.LIGHTWHITE_EX
self.start_processes('%s %s' % (action, path))
except Empty:
self.poll_processes()
time.sleep(1.0)
self.kill_processes()
def start_processes(self, reason):
self.reason = reason
try:
tox_config = parseconfig(['-c', self.path])
except Exception:
return
self.kill_processes()
self.tox_waiting_envs = [x for x in sorted(tox_config.envconfigs)
if (len(self.envs) == 0 or x in self.envs) and
x not in self.omit_envs]
for env in self.tox_waiting_envs:
self.tox_procs[env] = None
self.start_next_process()
self.update_status()
def start_next_process(self):
if len(self.tox_waiting_envs) > 0:
tox_env = self.tox_waiting_envs[0]
self.tox_waiting_envs = self.tox_waiting_envs[1:]
self.tox_procs[tox_env] = ToxProcess(tox_env, self.path)
if self._running_processes() < self.max_concurrent:
self.start_next_process()
else:
self.update_status()
def poll_processes(self):
for env, proc in sorted(self.tox_procs.items()):
if proc is not None and proc.exit_status is None:
proc.poll()
if (proc.exit_status is not None and
self._running_processes() < self.max_concurrent):
self.start_next_process()
self.update_status()
def kill_processes(self):
for _, proc in self.tox_procs.items():
if proc is not None:
proc.terminate()
self.tox_procs = {}
def _running_processes(self):
running = 0
for _, proc in self.tox_procs.items():
if proc is not None and proc.exit_status is None:
running += 1
return running
def update_status(self):
with self.lock:
sys.stdout.write("\r\033[K")
sys.stdout.flush()
for env, proc in sorted(self.tox_procs.items()):
if proc is None:
sys.stdout.write(Fore.LIGHTBLACK_EX + env + Style.RESET_ALL + ' ')
elif proc.exit_status is None:
sys.stdout.write(Fore.LIGHTYELLOW_EX + env + Style.RESET_ALL + ' ')
elif proc.exit_status == 0:
sys.stdout.write(Fore.LIGHTGREEN_EX + env + Style.RESET_ALL + ' ')
else:
sys.stdout.write(Fore.LIGHTRED_EX + env + Style.RESET_ALL + ' ')
sys.stdout.write(Fore.LIGHTWHITE_EX + self.reason + Style.RESET_ALL)
sys.stdout.flush()
class ToxdogEventHandler(RegexMatchingEventHandler):
def __init__(self, path, envs, omit_envs, max_concurrent):
super(ToxdogEventHandler, self).__init__(regexes=['.*\.py$',
'.*\.rst$',
'.*\.md$',
'.*/tox\.ini$'],
ignore_directories=True,
ignore_regexes=['.*/__pycache__/.*',
'\.ropeproject/.*',
'.*\$py\.class$',
'.*/\.Python/.*',
'.*/env/.*',
'.*/builds/.*',
'.*/develop-eggs/.*',
'.*/dist/.*',
'.*/downloads/.*',
'.*/eggs/.*',
'.*/\.eggs/.*',
'.*/lib/.*',
'.*/lib64/.*',
'.*/\.egg-info/.*',
'.*/\.tox/.*'])
self.queue = Queue()
self.last_event = 0
self.thread = ToxdogThread(self.queue, path, envs, omit_envs)
self.thread.max_concurrent = max_concurrent
def start(self):
self.thread.start()
self.queue.put_nowait(None)
def stop(self):
self.thread.running = False
self.thread.join()
def handle_event(self, event):
current_time = time.time()
if current_time - self.last_event < 1.0:
return
self.queue.put(event)
def on_deleted(self, event):
self.handle_event(event)
def on_modified(self, event):
self.handle_event(event)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c',
nargs='?',
default=os.getcwd(),
help=('Location of the tox.ini file. Defaults to '
'current working directory.'))
parser.add_argument('-n',
nargs='?',
default=min(8, multiprocessing.cpu_count()),
help=('Number of concurrent jobs allowed. Default '
'is number of CPUs or 8, whichever is smaller.'))
parser.add_argument('-e',
nargs='*',
help=('Environments to allow from the tox.ini file. '
'Default is all environments.'))
parser.add_argument('-o',
nargs='*',
help=('Environments to omit from the tox.ini file. '
'Default is omitting no environments.'))
args = vars(parser.parse_args(sys.argv[1:]))
observer = Observer()
handler = ToxdogEventHandler(args['c'], args['e'] or [], args['o'] or [], int(args['n']))
handler.start()
observer.schedule(handler, args['c'], True)
observer.start()
try:
while True:
time.sleep(1.0)
except KeyboardInterrupt:
observer.stop()
handler.stop()
sys.stdout.write("\r\033[K")
sys.stdout.flush()
observer.join()
if __name__ == '__main__':
main()