Esempio n. 1
0
def work_apidoc(paths_to_watch_d):
    observer = Observer()

    for path in paths_to_watch_d:
        event_handler = EventHandler(
            patterns=path.get('patterns', ['*.py']),
            ignore_directories=path.get('ignore_directories', True),
        )

        observer.schedule(
            event_handler,
            path['path'],
            recursive=path.get('recursive', True),
        )

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    event_handler2 = LoggingEventHandler()
    observer.add_handler_for_watch(event_handler2, observer)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Esempio n. 2
0
def run():
    sudo('supervisorctl restart wirephone')
    try:
        event_handler = LoggingEventHandler()
        other_handler = CodeEvents()
        observer = Observer()
        watch = observer.schedule(event_handler, './wirephone', recursive=True)
        observer.add_handler_for_watch(other_handler, watch)
        observer.start()
        logging.info('Waiting for events.')
        while True:
            try:
                action,event = other_handler.queue.get(block=True, timeout=0.5)
            except Queue.Empty:
                continue
            if action == 'reload':
                other_handler.reloading = False
                local("osascript -e 'display notification \"File %s changed.\" with title \"Reloading....\"'" % event)
                logging.info('Reloading the source.')
                sudo('supervisorctl restart wirephone')
                local("osascript -e 'display notification \"Wirephone reloaded.\" with title \"Matrix Reloaded\"'")


    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Esempio n. 3
0
def run():
    sudo('supervisorctl restart wiregui')
    try:
        event_handler = LoggingEventHandler()
        other_handler = CodeEvents()
        observer = Observer()
        watch = observer.schedule(event_handler, './wiregui-server', recursive=True)
        observer.add_handler_for_watch(other_handler, watch)
        observer.start()
        logging.info('Waiting for events.')
        while True:
            try:
                action,event = other_handler.queue.get(block=True, timeout=0.5)
            except Queue.Empty:
                continue
            if action == 'reload':
                reload(event)
                other_handler.reloading = False
                
                
                


    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Esempio n. 4
0
def main(watch_path):
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    os.makedirs(watch_path) if not os.path.exists(watch_path) else None
    dog = Observer()
    image_handler = ImageChangeEventHandler()
    log_handler = LoggingEventHandler()
    watch_instance = dog.schedule(image_handler, watch_path)
    dog.add_handler_for_watch(log_handler, watch_instance)
    dog.start()
    print(u'watching', watch_path, u',interrupt watching with Ctrl + C')
    wait_interrupt()
    dog.stop()
    dog.join()
Esempio n. 5
0
# -*- coding: utf-8 -*-
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, LoggingEventHandler
# from watchdog.observers.api import ObservedWatch


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if event.src_path == "/tmp/watch.log":
            print("log file %s changed" % event.src_path)


if __name__ == '__main__':
    event_handler1 = MyHandler()
    observer = Observer()
    watch = observer.schedule(event_handler1, path="/", recursive=True)

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    event_handler2 = LoggingEventHandler()
    observer.add_handler_for_watch(event_handler2, watch)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Esempio n. 6
0
class HotterDeployer(object):
    def __init__(self, workspace_directory, tomcat_directory, hotterdeploy_dir,
                 liferay_context, do_polling, statics_directory):
        self.do_polling = do_polling
        self.workspace_directory = workspace_directory
        self.tomcat_directory = tomcat_directory
        self.liferay_context = liferay_context
        self.statics_directory = statics_directory
        self.portlets = {}
        self.themes = {}
        self.deploys = {}

        if hotterdeploy_dir == '':
            self.hotterdeploy_dir = os.path.abspath(
                os.path.join(tomcat_directory, '..', 'hotterdeploy'))
        else:
            self.hotterdeploy_dir = os.path.abspath(
                os.path.join(hotterdeploy_dir, 'hotterdeploy'))

        self.tomcat_temp_dir = os.path.join(tomcat_directory, 'temp')
        self.tomcat_webapps_dir = os.path.join(tomcat_directory, 'webapps')
        self.liferay_dir = os.path.join(self.tomcat_webapps_dir,
                                        self.liferay_context)

        if do_polling:
            self.observer = PollingObserverVFS(default_stat, listdir)
        else:
            self.observer = Observer()

        # Create our hotterdeploy directory and watch it for wars
        if not os.path.exists(self.hotterdeploy_dir):
            os.mkdir(self.hotterdeploy_dir)
        self.observer.schedule(OnDeployHandler(hotterDeployer=self),
                               self.hotterdeploy_dir,
                               recursive=False)

        # Scan tomcat temp directory for deployed portlets
        self._scan_temp()
        self.observer.schedule(OnTempDeployHandler(hotterDeployer=self),
                               self.tomcat_temp_dir,
                               recursive=False)

        # Scan tomcat webapps directory for deployed portlets
        self._scan_webapps()
        self.observer.schedule(OnWebappsDeployHandler(hotterDeployer=self),
                               self.tomcat_webapps_dir,
                               recursive=False)

        # Scan the working directory for portlets
        LOG.debug('Scanning workspace for portlets...')
        self._scan_wd(workspace_directory)
        LOG.debug('Done.')

        # path = os.path.join(self.workspace_directory, 'liferay-portal', 'credoc-newsletter-portlet')
        path = self.workspace_directory

        # IN_CREATE | IN_DELETE | IN_CLOSE_WRITE && src/main/webapp/WEB-INF
        w = self.observer.schedule(WorkSpaceHandler(hotterDeployer=self),
                                   path,
                                   recursive=True)
        # IN_CLOSE_WRITE && src/main/webapp
        self.observer.add_handler_for_watch(
            OnFileChangedHandler(hotterDeployer=self), w)
        # self.observer.schedule(OnFileChangedHandler(hotterDeployer=self), self.workspace_directory, recursive=True)
        # IN_CLOSE_WRITE && target/classes
        self.observer.add_handler_for_watch(
            OnClassChangedHandler(hotterDeployer=self), w)
        # self.observer.schedule(OnClassChangedHandler(hotterDeployer=self), self.workspace_directory, recursive=True)

        self.livereload_server = Server()

    def start(self):
        import time
        start_time = time.time()
        LOG.debug('Starting observer...')
        self.observer.start()
        LOG.debug('Starting observer took {0} seconds'.format(time.time() -
                                                              start_time))

        from livereload import LiveReloadInfoHandler
        LiveReloadInfoHandler.hotterDeployer = weakref.proxy(self)

        LOG.info(
            'Using {0}'.format('polling' if self.do_polling else 'FS events'))
        LOG.info('Serving livereload on http://{host}:{port}/info'.format(
            **vars(self.livereload_server)))

        try:
            self.livereload_server.serve()
        except KeyboardInterrupt:
            self.observer.stop()
            self.livereload_server.stop()
        self.observer.join()

    def __del__(self):
        if os.path.exists(self.hotterdeploy_dir):
            os.rmdir(self.hotterdeploy_dir)

    def _scan_wd(self, directory):
        self.portlets.update(
            scan_working_directory_for_portlet_contexts(directory))

    def _update_deploys(self):
        deploys = {}
        if hasattr(self, '_temp_deploys'):
            for name, path in self._temp_deploys.items():
                deploys[name] = path

        if hasattr(self, '_webapp_deploys'):
            for name, path in self._webapp_deploys.items():
                if name not in deploys:
                    deploys[name] = path

        self.deploys = deploys

    def _scan_temp(self):
        path = os.path.join(self.tomcat_directory, 'temp')
        self._temp_deploys = scan_tomcat_temporary_directory(path)
        self._update_deploys()

    def _scan_webapps(self):
        path = os.path.join(self.tomcat_directory, 'webapps')
        self._webapp_deploys = scan_tomcat_webapps_directory(path)
        self._update_deploys()

    def find_latest_temp_dir(self, portlet_name):
        '''
        Find the latest temp deploy directory for a given portlet:
        e.g. 21-my-portlet-name
        '''
        latest_subdir = self.deploys.get(portlet_name, None)
        return latest_subdir

    def trigger_browser_reload(self, path=None):
        LOG.debug('reloading browser')
        self.livereload_server.reload(path)
class HotterDeployer(object):
    def __init__(
            self,
            workspace_directory,
            tomcat_directory,
            hotterdeploy_dir,
            liferay_context,
            do_polling,
            statics_directory
            ):
        self.do_polling = do_polling
        self.workspace_directory = workspace_directory
        self.tomcat_directory = tomcat_directory
        self.liferay_context = liferay_context
        self.statics_directory = statics_directory
        self.portlets = {}
        self.themes = {}
        self.deploys = {}

        if hotterdeploy_dir == '':
            self.hotterdeploy_dir = os.path.abspath(os.path.join(tomcat_directory, '..', 'hotterdeploy'))
        else:
            self.hotterdeploy_dir = os.path.abspath(os.path.join(hotterdeploy_dir, 'hotterdeploy'))

        self.tomcat_temp_dir = os.path.join(tomcat_directory, 'temp')
        self.tomcat_webapps_dir = os.path.join(tomcat_directory, 'webapps')
        self.liferay_dir = os.path.join(self.tomcat_webapps_dir, self.liferay_context)

        if do_polling:
            self.observer = PollingObserverVFS(default_stat, listdir)
        else:
            self.observer = Observer()

        # Create our hotterdeploy directory and watch it for wars
        if not os.path.exists(self.hotterdeploy_dir):
            os.mkdir(self.hotterdeploy_dir)
        self.observer.schedule(OnDeployHandler(hotterDeployer=self), self.hotterdeploy_dir, recursive=False)

        # Scan tomcat temp directory for deployed portlets
        self._scan_temp()
        self.observer.schedule(OnTempDeployHandler(hotterDeployer=self), self.tomcat_temp_dir, recursive=False)

        # Scan tomcat webapps directory for deployed portlets
        self._scan_webapps()
        self.observer.schedule(OnWebappsDeployHandler(hotterDeployer=self), self.tomcat_webapps_dir, recursive=False)

        # Scan the working directory for portlets
        LOG.debug('Scanning workspace for portlets...')
        self._scan_wd(workspace_directory)
        LOG.debug('Done.')

        # path = os.path.join(self.workspace_directory, 'liferay-portal', 'credoc-newsletter-portlet')
        path = self.workspace_directory

        # IN_CREATE | IN_DELETE | IN_CLOSE_WRITE && src/main/webapp/WEB-INF
        w = self.observer.schedule(WorkSpaceHandler(hotterDeployer=self), path, recursive=True)
        # IN_CLOSE_WRITE && src/main/webapp
        self.observer.add_handler_for_watch(OnFileChangedHandler(hotterDeployer=self), w)
        # self.observer.schedule(OnFileChangedHandler(hotterDeployer=self), self.workspace_directory, recursive=True)
        # IN_CLOSE_WRITE && target/classes
        self.observer.add_handler_for_watch(OnClassChangedHandler(hotterDeployer=self), w)
        # self.observer.schedule(OnClassChangedHandler(hotterDeployer=self), self.workspace_directory, recursive=True)

        self.livereload_server = Server()

    def start(self):
        import time
        start_time = time.time()
        LOG.debug('Starting observer...')
        self.observer.start()
        LOG.debug('Starting observer took {0} seconds'.format(time.time() - start_time))

        from livereload import LiveReloadInfoHandler
        LiveReloadInfoHandler.hotterDeployer = weakref.proxy(self)

        LOG.info('Using {0}'.format('polling' if self.do_polling else 'FS events'))
        LOG.info('Serving livereload on http://{host}:{port}/info'.format(**vars(self.livereload_server)))

        try:
            self.livereload_server.serve()
        except KeyboardInterrupt:
            self.observer.stop()
            self.livereload_server.stop()
        self.observer.join()

    def __del__(self):
        if os.path.exists(self.hotterdeploy_dir):
            os.rmdir(self.hotterdeploy_dir)

    def _scan_wd(self, directory):
        self.portlets.update(scan_working_directory_for_portlet_contexts(directory))

    def _update_deploys(self):
        deploys = {}
        if hasattr(self, '_temp_deploys'):
            for name, path in self._temp_deploys.items():
                deploys[name] = path

        if hasattr(self, '_webapp_deploys'):
            for name, path in self._webapp_deploys.items():
                if name not in deploys:
                    deploys[name] = path

        self.deploys = deploys

    def _scan_temp(self):
        path = os.path.join(self.tomcat_directory, 'temp')
        self._temp_deploys = scan_tomcat_temporary_directory(path)
        self._update_deploys()

    def _scan_webapps(self):
        path = os.path.join(self.tomcat_directory, 'webapps')
        self._webapp_deploys = scan_tomcat_webapps_directory(path)
        self._update_deploys()

    def find_latest_temp_dir(self, portlet_name):
        '''
        Find the latest temp deploy directory for a given portlet:
        e.g. 21-my-portlet-name
        '''
        latest_subdir = self.deploys.get(portlet_name, None)
        return latest_subdir

    def trigger_browser_reload(self, path=None):
        LOG.debug('reloading browser')
        self.livereload_server.reload(path)
Esempio n. 8
0
class QiniuSync():
    def __init__(self, paths, reg_ignore):
        self.paths = paths
        self.reg_ignore = reg_ignore
        sys.stderr.write(self.summary())
        self.generate_conf()
        self.copy_ignorefile()
        self.observer = Observer()
        # 初次同步,生成对比库
        for path in config.WATCH_PATH:
            qiniu_syn(path)
        self.start_watch()

    # 生成配置文件
    def generate_conf(self):
        for path in self.paths:
            # 根据路径获取文件夹名称
            dir_str = get_foldername(path)
            json_data = {}
            json_data["src"] = str(path)
            json_data["dest"] = 'qiniu:access_key=' + config.QINIU_ACCESS_KEY + \
                                '&secret_key=' + config.QINIU_SECRET_KEY + \
                                '&bucket=' + config.QINIU_BUCKET + \
                                '&key_prefix=' + dir_str + '/'
            json_data["debug_level"] = 1
            # 根据路径生成文件名
            if PY2:
                filename = str(hashlib.md5(path).hexdigest()) + '.json'
            else:
                filename = str(hashlib.md5(
                    path.encode("utf8")).hexdigest()) + '.json'
            # 文件不存在则重新创建配置文件
            if not os.path.isfile(filename):
                with open(filename, 'w') as f:
                    f.write(json.dumps(json_data))

    # 拷贝屏蔽文件到同步目录(.qrsignore.txt)
    def copy_ignorefile(self):
        src = open(current_dir + '\.qrsignore.txt', 'r+')
        for path in self.paths:
            des_path = path + '/.qrsignore.txt'
            if not os.path.isfile(des_path):
                des = open(des_path, 'w+')
                des.writelines(src.read())
                des.close()
            else:
                continue
        src.close()

    # 启动监控
    def start_watch(self):
        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S',
                            filename='log.txt')
        # 根据路径创建多个监视对象
        paths = self.paths
        for path in paths:
            event_handler = QiniuSyncEventHandler(path, self.reg_ignore)
            watch = self.observer.schedule(event_handler, path, recursive=True)
            logging_handler = LoggingEventHandler()
            # 为watch添加log处理程序
            self.observer.add_handler_for_watch(logging_handler, watch)
        self.observer.start()
        self.sys_tray()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            self.observer.stop()
        self.observer.join()

    # 停止监控
    def stop_watch(self):
        self.observer.stop()
        exit()

    # 最小化
    def sys_tray(self):
        import itertools, glob
        from sys_tray_icon import SysTrayIcon
        # 获取图标
        icons = itertools.cycle(glob.glob('*.ico'))
        # 最小化显示文字
        hover_text = "QiniuSync"
        import ctypes
        import win32con
        wnd = ctypes.windll.kernel32.GetConsoleWindow()
        if wnd != 0:
            ctypes.windll.user32.ShowWindow(wnd, win32con.SW_HIDE)
            ctypes.windll.kernel32.CloseHandle(wnd)

        # 退出处理
        def quit_sync():
            self.stop_watch()

        # 最小化到托盘
        SysTrayIcon(next(icons),
                    hover_text,
                    menu_options=(),
                    on_quit=quit_sync,
                    default_menu_index=1,
                    wnd=wnd)

    # 输出信息
    def summary(self):
        info = ''
        info += '------------------------------------------------------\n'
        info += 'QiniuSync Version  : %s (python/%s)\n' % (__version__,
                                                           sys.version[:5])
        info += 'Author             : skywatcher\n'
        info += '------------------------------------------------------\n'
        return info
Esempio n. 9
0
class Schedule:
    def __init__(self, config):
        self.observer = Observer()
        self.handlers = {}
        self.watchers = {}
        self.counter = Counter()
        self.notifier = Notifier(config)
        self.offset_db = OffsetPersistence(config)

    def __make_key(self, filename):
        return path.abspath(urlsafe_b64decode(filename).decode())

    def add_watcher(self, filename):
        #如果监控的文件都在同一个目录下,下面这种方法只会启动一个inotify来进行监控这个目录下的所有文件,handler.start实际上调用了monitor的start
        filename = self.__make_key(filename)
        if handler.filename not in self.handlers.keys():
            handler = Watcher(filename, counter=self.counter, notifier=self.notifier, offset_db=self.offset_db)
            if path.dirname(handler.filename) not in self.watchers.keys():
                self.watchers[path.dirname(handler.filename)] = self.observer.schedule(handler, path.dirname(handler.filename), recursive=False)
            else:
                watch = self.watchers[path.dirname(handler.filename)]
                self.observer.add_handler_for_watch(handler, watch)
            self.handlers[handler.filename] = handler
            handler.start()


    def remove_watcher(self, filename):
        #判断key是否在watchers里面,有则从observer中剔除,然后从watchers字典内删除,并把handlers剔除并stop
        key = self.__make_key(filename)
        handler = self.handlers.pop(key)
        if handler is not None:
            watch = self.watchers[path.dirname(key)]
            self.observer.remove_handler_for_watch(handler, watch)
            handler.stop()
            if not self.observer._handlers[watch]:
                self.observer.unschedule(watch)
                self.watchers.pop(path.dirname(handler.filename))


    #添加和移除monitor,实质上就是调用Monitor类的add和remove方法来操作
    def add_monitor(self, filename, name, src):
        key = self.__make_key(filename)
        handler = self.handlers.get(key)
        if handler is None:
            logging.warning('watcher {0} not found, auto add it'.format(filename))
            handler.add_watcher(filename)
            handler = self.handlers.get(key)
        handler.monitor.add(filename, name, src)

    def remove_monitor(self, filename, name):
        key = self.__make_key(filename)
        handler = self.handlers.get(key)
        if handler is None:
            logging.warning('watcher {0} not found'.format(filename))
            return
        handler.monitor.remove(name)

    def start(self):
        self.observer.start()
        self.notifier.start()

    def join(self):
        self.observer.join()

    def stop(self):
        self.observer.stop()
        for handler in self.handlers.values():
            handler.stop()
        self.notifier.stop()
        self.offset_db.close()
Esempio n. 10
0
class WatchdogTests(unittest.TestCase):

    def setUp(self):
        self.events = queue.Queue()
        self.observer = Observer()
        self.handler = EventHandler(self.events, channel_id=5)
        self.watch = self.observer.schedule(event_handler=self.handler, path=TMP_DIR)
        self.observer.start()

    def testEventChannelID(self):
        with open(TMP_FILE, "w") as local_file:
            local_file.write(string_common)
        event = self.events.get(block=True, timeout=1)
        self.assertEqual(event.channel_id, 5)

    # Modifying the file should add a new task to the queue
    def testTaskInsertion(self):
        new_string = "This is a whole new line in file "+filename
        with open (TMP_FILE, "w") as local_file:
            local_file.write(new_string)
        while True:
            try:
                task = self.events.get(block=True, timeout=1)
            except queue.Empty:
                break
            self.assertEqual(task.src_path,local_dir+filename)
            self.assertEqual(task.dest_path, remote_dir+filename)

    # It should be possible to add a new Handler to an already scheduled watch
    def testHandlerAdding(self):
        fake_handle="I am a fake handler, don't mind me!"
        class FakeHandler(EventHandler):
            def handle_modification(self, event):
                self.tasks.put(fake_handle)
        fake_queue = queue.Queue()
        fake_handler = FakeHandler(fake_queue)
        self.observer.add_handler_for_watch(fake_handler, self.watch)
        new_string = "This is a whole new line in file " + filename
        with open(local_dir + filename, "w") as local_file:
            local_file.write(new_string)
        while True:
            try:
                task = fake_queue.get(block=True, timeout=1)
            except queue.Empty:
                break
            self.assertEqual(task, fake_handle)

    # A Handler blocking on one event (ex: inserting into a busy queue)
    # should not prevent handling of further events
    def testHandlerHanging(self):
        class HangingHandler(EventHandler):
            def on_any_event(self, event):
                print("Handler hanging...")
                time.sleep(1)
                print("Handler dispatching")
                super().on_any_event(event)

        self.observer.remove_handler_for_watch(self.handler, self.watch)
        slow_handler = HangingHandler(self.events)
        self.observer.add_handler_for_watch(slow_handler, self.watch)
        for i in range(5):
            with open(os.path.join(TMP_DIR, "f"+str(i)), "w") as local_file:
                local_file.write("Write #"+str(i))
            time.sleep(0.1)
        time.sleep(6)
        num = len(empty_tasks(self.events))
        print("[Hanging] "+str(num)+" events")
        self.assertTrue(num >= 5)

    # Scheduling a new watch while another one is running
    # In this test, each write should set off 2 events (open+close) as seen on the next test
    # However watchdog is nice enough to avoid adding similar events to its internal queue
    def testNewScheduling(self):
        self.assertTrue(self.events.empty()) # Queue starts off
        with open(remote_dir + filename, "w") as remote_file:
            remote_file.write("Going once")
        self.assertRaises(queue.Empty, self.events.get, timeout=1) # No Handler is watching the file yet

        handler2 = EventHandler(self.events)
        self.observer.schedule(event_handler=handler2, path=remote_dir, recursive=True)

        with open(remote_dir + filename, "w") as remote_file:
            remote_file.write("Going twice")
        l1 = empty_tasks(self.events) # Now it should work
        self.assertEqual(len(l1), 1) # Single event
        for task in l1:
            self.assertEqual(task.src_path, remote_dir + filename)
            self.assertEqual(task.dest_path, local_dir + filename)

        with open(local_dir + filename, "w") as local_file:
            local_file.write("Going thrice") # Writing to the local file still works
        l2 = empty_tasks(self.events)
        self.assertEqual(len(l2), 1)  # Single event
        for task in l2:
            self.assertEqual(task.src_path, local_dir + filename)
            self.assertEqual(task.dest_path, remote_dir + filename)

    # It should be possible to remove a scheduled watch
    def testWatchRemoval(self):
        handler2 = EventHandler(self.events)
        watch2 = self.observer.schedule(event_handler=handler2, path=remote_dir, recursive=True)
        for client in [ {"path":local_dir+filename, "watch":self.watch},
                      {"path":remote_dir+filename, "watch":watch2} ]:
            with open(client["path"], "w") as file:
                file.write("This will make an event")
            time.sleep(0.5)
            task = self.events.get(timeout=1)
            self.assertEquals(task.src_path, client["path"])

            self.observer.unschedule(client["watch"])
            with open(local_dir+filename, "w") as local_file:
                local_file.write("This won't")
            self.assertRaises(queue.Empty, self.events.get, timeout=1)

    # Each open() and each close() should produce an event
    # They are sometimes squashed into a single event if done
    # Quickly enough (i.e. "with open(file) as f: f.write()")
    def testEventsPerWrite(self):
        local_file = open(local_dir+filename, "w")
        self.assertTrue(len(empty_tasks(self.events)) == 1) # Opening sets off an event
        local_file.write("First")
        self.assertTrue(len(empty_tasks(self.events)) == 0) # Writing doesn't set off an event
        local_file.write("Second")
        self.assertTrue(len(empty_tasks(self.events)) == 0) # Writing doesn't set off an event
        local_file.close()
        self.assertTrue(len(empty_tasks(self.events)) == 1) # Closing sets off an event

    def testEventsForBigFileCopy(self):
        self.observer.schedule(self.handler, TMP_DIR, recursive=True)
        try:
            os.mkdir(TMP_DIR)
        except FileExistsError:
            pass
        try:
            shutil.copy(BIG_FILE, BIG_IN)
        except OSError:
            self.fail()
        num = empty_tasks(self.events)
        print("Used "+str(len(num))+" events")
Esempio n. 11
0
    '''
    指定されたパラメータでハンドラを生成します。
    :param params: パラメータ
    :return: ハンドラ
    '''
    if 'class' in params: # インスタンスの生成
        module = __import__("handler",fromlist=[params['class']])
        clazz = getattr(module,params['class'])
        handler = clazz()
    for key in params: # プロパティの設定
        if 'class' != key:
            setattr(handler, key, params[key])
    return handler

if __name__ == "__main__":
    parser = load_ini(INI_FILE)
    target_dir = get_target_dir(parser)
    handlers = load_handlers(parser)
    observer = Observer()
    for index, handler in enumerate(handlers):
        if index == 0:
            watch = observer.schedule(handler, target_dir, recursive=True)
        else:
            observer.add_handler_for_watch(handler, watch)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Esempio n. 12
0
File: main.py Progetto: qcq/WeChat
        uploadPicture.setDaemon(True)
        uploadPicture.start()
        logging.info("server is running to upload pictures to tencent.")
        netDisk.setDaemon(True)
        netDisk.start()
        logging.info(
            "server is running to providing service to baidu netdisk.")
        pictureMonitor = PicturePathHandler(patterns=[
            r'*.jpg', r'*.png', r'*.jpeg', r'*.gif', r'*.JPG', r'*.PNG',
            r'*.JPEG', r'*.GIF'
        ],
                                            ignore_patterns=[r'*.swap'],
                                            ignore_directories=True,
                                            case_sensitive=True)
        # below code to take pyeventbus work
        pictureMonitor.register(pictureMonitor)
        watch = observer.schedule(pictureMonitor,
                                  path='..//pictures',
                                  recursive=True)
        observer.add_handler_for_watch(LoggingEventHandler(), watch)
        observer.start()
        logging.info("start to monitor the path of picture.")
        logging.info("server is ready to provide the service.")

        app.run(Log)
    except KeyboardInterrupt:
        observer.stop()
    except Exception, exc:
        logging.warn('Exception happened:%s' % traceback.print_exc(),
                     exc_info=True)
Esempio n. 13
0
class QiniuSync():
    def __init__(self, paths, reg_ignore):
        self.paths = paths
        self.reg_ignore = reg_ignore
        sys.stderr.write(self.summary())
        self.generate_conf()
        self.copy_ignorefile()
        self.observer = Observer()
        # 初次同步,生成对比库
        for path in config.WATCH_PATH:
            qiniu_syn(path)
        self.start_watch()

    # 生成配置文件
    def generate_conf(self):
        for path in self.paths:
            # 根据路径获取文件夹名称
            dir_str = get_foldername(path)
            json_data = {}
            json_data["src"] = str(path)
            json_data["dest"] = 'qiniu:access_key=' + config.QINIU_ACCESS_KEY + \
                                '&secret_key=' + config.QINIU_SECRET_KEY + \
                                '&bucket=' + config.QINIU_BUCKET + \
                                '&key_prefix=' + dir_str + '/'
            json_data["debug_level"] = 1
            # 根据路径生成文件名
            if PY2:
                filename = str(hashlib.md5(path).hexdigest()) + '.json'
            else:
                filename = str(hashlib.md5(path.encode("utf8")).hexdigest()) + '.json'
            # 文件不存在则重新创建配置文件
            if not os.path.isfile(filename):
                with open(filename, 'w') as f:
                    f.write(json.dumps(json_data))

    # 拷贝屏蔽文件到同步目录(.qrsignore.txt)
    def copy_ignorefile(self):
        src = open(current_dir + '\.qrsignore.txt', 'r+')
        for path in self.paths:
            des_path = path + '/.qrsignore.txt'
            if not os.path.isfile(des_path):
                des = open(des_path, 'w+')
                des.writelines(src.read())
                des.close()
            else:
                continue
        src.close()

    # 启动监控
    def start_watch(self):
        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S', filename='log.txt')
        # 根据路径创建多个监视对象
        paths = self.paths
        for path in paths:
            event_handler = QiniuSyncEventHandler(path, self.reg_ignore)
            watch = self.observer.schedule(event_handler, path, recursive=True)
            logging_handler = LoggingEventHandler()
            # 为watch添加log处理程序
            self.observer.add_handler_for_watch(logging_handler, watch)
        self.observer.start()
        self.sys_tray()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            self.observer.stop()
        self.observer.join()

    # 停止监控
    def stop_watch(self):
        self.observer.stop()
        exit()

    # 最小化
    def sys_tray(self):
        import itertools, glob
        from sys_tray_icon import SysTrayIcon
        # 获取图标
        icons = itertools.cycle(glob.glob('*.ico'))
        # 最小化显示文字
        hover_text = "QiniuSync"
        import ctypes
        import win32con
        wnd = ctypes.windll.kernel32.GetConsoleWindow()
        if wnd != 0:
            ctypes.windll.user32.ShowWindow(wnd, win32con.SW_HIDE)
            ctypes.windll.kernel32.CloseHandle(wnd)

        # 退出处理
        def quit_sync():
            self.stop_watch()
        # 最小化到托盘
        if PY2:
            SysTrayIcon(icons.next(), hover_text, menu_options=(), on_quit=quit_sync, default_menu_index=1, wnd=wnd)
        else:
            SysTrayIcon(next(icons), hover_text, menu_options=(), on_quit=quit_sync, default_menu_index=1, wnd=wnd)

    # 输出信息
    def summary(self):
        info = ''
        info += '------------------------------------------------------\n'
        info += 'QiniuSync Version  : %s (python/%s)\n' % (__version__, sys.version[:5])
        info += 'Author             : skywatcher\n'
        info += '------------------------------------------------------\n'
        return info
Esempio n. 14
0
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, LoggingEventHandler
from watchdog.observers.api import ObservedWatch


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if event.src_path == r"C:\Users\Admin\Desktop\python monitor":
            print("log file %s changed!" % event.src_path)


if __name__ == "__main__":
    event_handler1 = MyHandler()
    observer = Observer()
    watch = observer.schedule(event_handler1, path='.', recursive=True)
    # 基礎設定
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')

    event_handler2 = LoggingEventHandler()  #標記標記~觸發事件顯示資訊
    observer.add_handler_for_watch(event_handler2,
                                   watch)  # 为watch新添加一个event handler
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:  #KeyboardInterrupt	引發當用戶按下中斷程序執行
        observer.stop()
    observer.join()