Newer
Older
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
#!/usr/bin/env python3
#
# Copyright (C) 2021 Associated Universities, Inc. Washington DC, USA.
#
# This file is part of NRAO Workspaces.
#
# Workspaces 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.
#
# Workspaces 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 Workspaces. If not, see <https://www.gnu.org/licenses/>.
import re
import subprocess
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class PexHandler(FileSystemEventHandler):
event_cache = {}
def on_modified(self, event):
seconds = int(time.time())
key = (seconds, event.src_path)
# check for identical events
if key in self.event_cache:
return
# don't handle events if they happen quickly after another event
if self.event_cache.__len__() != 0:
previous_entry = list(self.event_cache)[-1]
elapsed_time = seconds - previous_entry[0]
print(f"elapsed_time between events: {elapsed_time}")
if elapsed_time < 10:
print(f"Too little time between events, Skipping...")
return
self.event_cache[key] = True
# Only build pex if changes are made to files that end in ".py"
if event.src_path.endswith(".py"):
pexable = re.search("/pexable/(.*?)/", event.src_path).group(1)
print(f"******************** Building pex - {pexable} ********************")
# run local-build-pexables.sh
sp = subprocess.run(
["./bin/local-build-pexables.sh", f"{pexable}"],
stdout=subprocess.PIPE,
universal_newlines=True,
)
print(f"{sp.stdout}")
print(f"*********************** {pexable} finished ***********************")
print("Modification event handled.")
if __name__ == "__main__":
path = sys.argv[1] if len(sys.argv) > 1 else "./apps/cli/executables/pexable/"
event_handler = PexHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()