forked from Johnzero/OE7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoreload
73 lines (61 loc) · 2.09 KB
/
autoreload
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
# AutoReload Process For Windows
# wangbuke <[email protected]>
# taken from https://github.com/stevekrenzel/autoreload
#
# To use autoreload:
# Run ./autoreload.py <path> <ext1,ext2,extn> <cmd>
# e.g: $ python autoreload "./openerp" ".py,.xml" "python openerp-server -c openerp-server.conf"
#
##############################################################################
import sys
import subprocess
import time
import os
PATH = '.'
WAIT = 3
def start_process(cmd):
process = subprocess.Popen(command, shell=True)
return process
def stop_process(process):
process.kill()
process.wait()
def file_filter(extensions):
def my_filter(name):
return any([name.endswith(ext) for ext in extensions])
return my_filter
def file_times(path, extension):
extensions = extension.split(',')
for root, dirs, files in os.walk(path):
for file in filter(file_filter(extensions=extensions), files):
yield os.stat(os.path.join(root, file)).st_mtime
def print_stdout(process):
stdout = process.stdout
if stdout != None:
print stdout
if __name__ == "__main__":
command = ' '.join(sys.argv[1:])
path = sys.argv[1]
extension = sys.argv[2]
command = sys.argv[3]
process = start_process(command)
last_mtime = max(file_times(PATH,extension))
while True:
try:
print_stdout(process)
max_mtime = max(file_times(PATH,extension))
if max_mtime > last_mtime:
last_mtime = max_mtime
print "\n"
print 'Restarting process...'
print "\n"
stop_process(process)
process = start_process(command)
time.sleep(WAIT)
except (KeyboardInterrupt, SystemExit):
print "Caught KeyboardInterrupt, terminating process"
stop_process(process)
break
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: