-
Notifications
You must be signed in to change notification settings - Fork 3
/
autogen.py
executable file
·130 lines (95 loc) · 3.69 KB
/
autogen.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
#!/usr/bin/python3
"""ProtoFaust
==========
DSP prototyping in Faust for VCV Rack
Copyright (c) 2019-2020 Martin Zuther (http://www.mzuther.de/) and
contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Thank you for using free software!
"""
import pyinotify
import datetime
import os
import subprocess
class OnWriteHandler(pyinotify.ProcessEvent):
def __init__(self, payload_command):
print()
print('==> initial run')
print()
# initialise payload command
self.payload_command = payload_command
# run payload once on startup
self.run_payload()
def print_current_file(self, filename):
current_time = datetime.datetime.now()
formatted_time = current_time.strftime('%H:%M:%S')
output = '{0:s} ==> {1:s}'.format(formatted_time, filename)
print(output)
print()
# is called on completed writes
def process_IN_CLOSE_WRITE(self, event):
# ignore temporary files
if event.name.endswith('#'):
return
# ignore compiled Python files
elif event.name.endswith('.pyc'):
return
# get name of changed file
if event.name:
filename = os.path.join(event.path, event.name)
else:
filename = event.path
# print name of changed file
print('==> ' + filename)
print()
# run payload
self.run_payload()
def run_payload(self):
# run payload
proc = subprocess.Popen(self.payload_command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
# display output of "stdout" and "stderr" (if any)
for pipe_output in proc.communicate():
if pipe_output:
print(pipe_output.strip())
print()
# define directories to be ignored
def exclude_directories(path):
if path.startswith('src/faust/main-svg'):
return True
else:
return False
if __name__ == '__main__':
# directory that should be monitored
directory_to_watch = 'src/faust/'
# command to be run on payload. "unbuffer" pretends a TTY, thus
# keeping escape sequences
payload_command = 'make faust-clean && make faust'
# create an instance of "pyinotify"
watchmanager = pyinotify.WatchManager()
# add a watch (will trigger on completed writes)
watchmanager.add_watch(directory_to_watch,
pyinotify.IN_CLOSE_WRITE,
rec=True,
auto_add=True,
exclude_filter=exclude_directories)
# create an instance of our file processor
watchhandler = OnWriteHandler(payload_command)
# connect to our file processor to "pyinotify"
watchnotifier = pyinotify.Notifier(watchmanager,
default_proc_fun=watchhandler)
# keep monitoring
watchnotifier.loop()