コード例 #1
0
    def testLoop(self, restore_cwd, tmpdir):
        files = ["data/w1.txt", "data/w2.txt", "data/w3.txt"]
        stop_file = "data/stop"
        tmpdir.mkdir("data")
        for fname in files + [stop_file]:
            tmpdir.join(fname).open("a").close()
        os.chdir(tmpdir.strpath)

        fw = FileModifyWatcher((files[0], files[1], stop_file))
        events = []
        should_stop = []
        started = []

        def handle_event(event):
            events.append(event.pathname)
            if event.pathname.endswith("stop"):
                should_stop.append(True)

        fw.handle_event = handle_event

        def loop_callback(notifier):
            started.append(True)
            # force loop to stop
            if should_stop:
                raise KeyboardInterrupt

        loop_thread = threading.Thread(target=fw.loop, args=(loop_callback,))
        loop_thread.daemon = True
        loop_thread.start()

        # wait watcher to be ready
        while not started:  # pragma: no cover
            time.sleep(0.01)
            assert loop_thread.isAlive()

        # write in watched file
        fd = open(files[0], "w")
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(files[2], "w")
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(files[1], "w")
        fd.write("hi")
        fd.close()

        # tricky to stop watching
        fd = open(stop_file, "w")
        fd.write("hi")
        fd.close()
        time.sleep(0.1)
        loop_thread.join(1)
        assert not loop_thread.isAlive()

        assert os.path.abspath(files[0]) == events[0]
        assert os.path.abspath(files[1]) == events[1]
コード例 #2
0
ファイル: test_filewatch.py プロジェクト: swayf/doit
    def testLoop(self, cwd):
        file1, file2, file3 = 'data/w1.txt', 'data/w2.txt', 'data/w3.txt'
        stop_file = 'data/stop'
        fw = FileModifyWatcher((file1, file2, stop_file))
        events = []
        should_stop = []
        started = []

        def handle_event(event):
            events.append(event.pathname)
            if event.pathname.endswith("stop"):
                should_stop.append(True)

        fw.handle_event = handle_event

        def loop_callback(notifier):
            started.append(True)
            # force loop to stop
            if should_stop:
                raise KeyboardInterrupt

        loop_thread = threading.Thread(target=fw.loop, args=(loop_callback, ))
        loop_thread.daemon = True
        loop_thread.start()

        # wait watcher to be ready
        while not started:  # pragma: no cover
            assert loop_thread.isAlive()

        # write in watched file
        fd = open(file1, 'w')
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(file3, 'w')
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(file2, 'w')
        fd.write("hi")
        fd.close()

        # tricky to stop watching
        fd = open(stop_file, 'w')
        fd.write("hi")
        fd.close()
        loop_thread.join(1)
        assert not loop_thread.isAlive()

        assert os.path.abspath(file1) == events[0]
        assert os.path.abspath(file2) == events[1]
コード例 #3
0
    def testLoop(self, cwd):
        file1, file2, file3 = 'data/w1.txt', 'data/w2.txt', 'data/w3.txt'
        stop_file = 'data/stop'
        fw = FileModifyWatcher((file1, file2, stop_file))
        events = []
        should_stop = []
        started = []
        def handle_event(event):
            events.append(event.pathname)
            if event.pathname.endswith("stop"):
                should_stop.append(True)
        fw.handle_event = handle_event

        def loop_callback(notifier):
            started.append(True)
            # force loop to stop
            if should_stop:
                raise KeyboardInterrupt
        loop_thread = threading.Thread(target=fw.loop, args=(loop_callback,))
        loop_thread.daemon = True
        loop_thread.start()

        # wait watcher to be ready
        while not started: # pragma: no cover
            assert loop_thread.isAlive()

        # write in watched file
        fd = open(file1, 'w')
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(file3, 'w')
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(file2, 'w')
        fd.write("hi")
        fd.close()

        # tricky to stop watching
        fd = open(stop_file, 'w')
        fd.write("hi")
        fd.close()
        loop_thread.join(1)
        assert not loop_thread.isAlive()

        assert os.path.abspath(file1) == events[0]
        assert os.path.abspath(file2) == events[1]
コード例 #4
0
    def testLoop(self, restore_cwd, tmpdir):
        files = ['data/w1.txt', 'data/w2.txt', 'data/w3.txt']
        stop_file = 'data/stop'
        tmpdir.mkdir('data')
        for fname in files + [stop_file]:
            tmpdir.join(fname).open('a').close()
        os.chdir(tmpdir.strpath)

        fw = FileModifyWatcher((files[0], files[1], stop_file))
        events = []
        should_stop = []
        started = []

        def handle_event(event):
            events.append(event.src_path)

        fw.handle_event = handle_event

        fw.loop()
        time.sleep(1)
        # write in watched file
        fd = open(files[0], 'w')
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(files[2], 'w')
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(files[1], 'w')
        fd.write("hi")
        fd.close()
        time.sleep(1)
        fw.observer.stop()
        fw.observer.join(1)

        assert os.path.abspath(files[0]) in events
        assert os.path.abspath(files[1]) in events
コード例 #5
0
    def testLoop(self, restore_cwd, tmpdir):
        files = ['data/w1.txt', 'data/w2.txt', 'data/w3.txt']
        stop_file = 'data/stop'
        tmpdir.mkdir('data')
        for fname in files + [stop_file]:
            tmpdir.join(fname).open('a').close()
        os.chdir(tmpdir.strpath)

        fw = FileModifyWatcher((files[0], files[1], stop_file))
        events = []
        should_stop = []
        started = []

        def handle_event(event):
            events.append(event.pathname)
            if event.pathname.endswith("stop"):
                should_stop.append(True)

        fw.handle_event = handle_event

        def loop_callback(notifier):
            started.append(True)
            # force loop to stop
            if should_stop:
                raise KeyboardInterrupt

        loop_thread = threading.Thread(target=fw.loop, args=(loop_callback, ))
        loop_thread.daemon = True
        loop_thread.start()

        # wait watcher to be ready
        while not started:  # pragma: no cover
            time.sleep(0.01)
            assert loop_thread.isAlive()

        # write in watched file
        fd = open(files[0], 'w')
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(files[2], 'w')
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(files[1], 'w')
        fd.write("hi")
        fd.close()

        # tricky to stop watching
        fd = open(stop_file, 'w')
        fd.write("hi")
        fd.close()
        time.sleep(0.1)
        loop_thread.join(1)

        if loop_thread.isAlive():  # pragma: no cover
            # this test is very flaky so we give it one more chance...
            # write on file to terminate thread
            fd = open(stop_file, 'w')
            fd.write("hi")
            fd.close()

            loop_thread.join(1)
            if loop_thread.is_alive():  # pragma: no cover
                raise Exception("thread not terminated")

        assert os.path.abspath(files[0]) == events[0]
        assert os.path.abspath(files[1]) == events[1]
コード例 #6
0
ファイル: test_filewatch.py プロジェクト: JeffSpies/doit
    def testLoop(self, restore_cwd, tmpdir):
        files = ['data/w1.txt', 'data/w2.txt', 'data/w3.txt']
        stop_file = 'data/stop'
        tmpdir.mkdir('data')
        for fname in files + [stop_file]:
            tmpdir.join(fname).open('a').close()
        os.chdir(tmpdir.strpath)

        fw = FileModifyWatcher((files[0], files[1], stop_file))
        events = []
        should_stop = []
        started = []
        def handle_event(event):
            events.append(event.pathname)
            if event.pathname.endswith("stop"):
                should_stop.append(True)
        fw.handle_event = handle_event

        def loop_callback(notifier):
            started.append(True)
            # force loop to stop
            if should_stop:
                raise KeyboardInterrupt
        loop_thread = threading.Thread(target=fw.loop, args=(loop_callback,))
        loop_thread.daemon = True
        loop_thread.start()

        # wait watcher to be ready
        while not started: # pragma: no cover
            time.sleep(0.01)
            assert loop_thread.isAlive()

        # write in watched file
        fd = open(files[0], 'w')
        fd.write("hi")
        fd.close()
        # write in non-watched file
        fd = open(files[2], 'w')
        fd.write("hi")
        fd.close()
        # write in another watched file
        fd = open(files[1], 'w')
        fd.write("hi")
        fd.close()

        # tricky to stop watching
        fd = open(stop_file, 'w')
        fd.write("hi")
        fd.close()
        time.sleep(0.1)
        loop_thread.join(1)

        if loop_thread.isAlive(): # pragma: no cover
            # this test is very flaky so we give it one more chance...
            # write on file to terminate thread
            fd = open(stop_file, 'w')
            fd.write("hi")
            fd.close()

            loop_thread.join(1)
            if loop_thread.is_alive(): # pragma: no cover
                raise Exception("thread not terminated")

        assert os.path.abspath(files[0]) == events[0]
        assert os.path.abspath(files[1]) == events[1]