Exemple #1
0
class FileSystemMonitor:
    def __init__(self, syncer=None, minimal=True):
        self.syncer = syncer
        self.minimal = minimal
        self.event_handler = MyFileSystemEventHandler(syncer=self.syncer)
        self.observer = Observer(timeout=20)
        self.paths = []
        if minimal:
            self.paths_minimal()

    def _log_info(self, msg):
        print(" - %s" % msg)

    def paths_minimal(self):
        self.paths = []
        for item in [
                "jumpscaleX_core/JumpscaleCore",
                "jumpscaleX_core/cmds",
                "jumpscaleX_core/install",
                # "jumpscaleX_libs",
                # "jumpscaleX_threebot",
                # "jumpscaleX_builders",
                # "jumpscaleX_libs_extra",
                # "jumpscaleX_weblibs",
        ]:
            self.paths.append(
                "{DIR_CODE}/github/threefoldtech/%s:/sandbox/code/github/threefoldtech/%s"
                % (item, item))

    def _get_paths(self):
        if self.paths != []:
            return self.syncer._get_paths(paths=self.paths)
        else:
            return self.syncer._get_paths()

    def start(self):

        for item in self._get_paths():
            source, dest = item
            self._log_info("monitor:%s" % source)
            self.observer.schedule(self.event_handler, source, recursive=True)

        self.observer.start()

        self._log_info("WE ARE MONITORING ")

        try:
            while True:
                gevent.time.sleep(1)
                # print(1)
        except KeyboardInterrupt:
            self.observer.stop()
        self.observer.join()

    def __str__(self):
        return "FileSystemMonitor"
Exemple #2
0
def test_can_watch_for_changes_with_gevent():
    # Given a gevent-based thread that counts stuff and sleeps every second
    count = 0

    def counter():
        nonlocal count
        while True:
            count += 1
            time.sleep(1)

    counter_thread = Thread(target=counter, daemon=True)
    counter_thread.start()

    # And an event handler that gets notified when this file changes
    events = []

    class Handler(FileSystemEventHandler):
        def on_any_event(self, event):
            events.append(event)

    # And an observer that dispatches to that handler
    try:
        observer = Observer()
        observer.schedule(Handler(), rel(".."), recursive=True)
        observer.start()

        # When I touch this file
        subprocess.run(["touch", __file__])

        # And wait a second
        time.sleep(1)

        # Then the counter should have incremented
        assert count >= 2

        # And the event should have been observed
        assert events == [FileModifiedEvent(__file__)]
    finally:
        observer.stop()
        observer.join()
Exemple #3
0
from threading import Thread

logging.basicConfig(level=logging.DEBUG)

running = True


def printer():
    global running
    logger = logging.getLogger("printer")
    while running:
        logger.info("Ping!")
        time.sleep(1)


try:
    pinger = Thread(target=printer)
    pinger.start()

    observer = Observer()
    observer.schedule(LoggingEventHandler(), ".", recursive=True)
    observer.start()

    while True:
        time.sleep(1)
except KeyboardInterrupt:
    running = False
    observer.stop()

observer.join()