Skip to content
Snippets Groups Projects
watcher.py 2.63 KiB
Newer Older
#!/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()