def watch(path, pipe): path = path pipe = pipe observer = Observer() handler = FSHandler(pipe) observer.schedule(handler, path=path, recursive=True) # Write a null to the pipe so we know we're ready os.write(pipe, chr(0)) observer.run()
def run(self, *args, **kwargs): try: Observer.run(self, *args, **kwargs) except: pass
class AutoReloadWatchdog(object): def __init__(self, server): from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from watchdog.events import FileCreatedEvent from watchdog.events import FileModifiedEvent self.server = server self.files = {} self.modules = {} class EventHandler(FileSystemEventHandler): def __init__(self, autoreload): self.autoreload = autoreload def process(self): l = self.autoreload.files.keys() self.autoreload.files.clear() self.autoreload.process_data(l) self.autoreload.process_python(l) def on_created(self, event): if isinstance(event, FileCreatedEvent): _logger.debug('File created: %s', event.src_path) self.autoreload.files[event.src_path] = 1 self.process() def on_modified(self, event): if isinstance(event, FileModifiedEvent): _logger.debug('File modified: %s', event.src_path) self.autoreload.files[event.src_path] = 1 self.process() #self.wm = pyinotify.WatchManager() self.handler = EventHandler(self) self.observer = Observer() #self.notifier = pyinotify.Notifier(self.wm, self.handler, timeout=0) #mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE # IN_MOVED_FROM, IN_MOVED_TO ? for path in openerp.tools.config.options["addons_path"].split(','): _logger.info('Watching addons folder %s', path) #self.wm.add_watch(path, mask, rec=True) self.observer.schedule(self.handler, path=path, recursive=True) def process_data(self, files): xml_files = [i for i in files if i.endswith('.xml')] addons_path = openerp.tools.config.options["addons_path"].split(',') for i in xml_files: for path in addons_path: if i.startswith(path): # find out which addons path the file belongs to # and extract it's module name right = i[len(path) + 1:].split('/') if len(right) < 2: continue module = right[0] self.modules[module] = 1 if self.modules: _logger.info( 'autoreload: xml change detected, autoreload activated') openerp.service.server.restart() def process_python(self, files): # process python changes py_files = [i for i in files if i.endswith('.py')] py_errors = [] # TODO keep python errors until they are ok if py_files: for i in py_files: try: source = open(i, 'rb').read() + '\n' compile(source, i, 'exec') except SyntaxError: py_errors.append(i) if py_errors: _logger.info( 'autoreload: python code change detected, errors found') for i in py_errors: _logger.info('autoreload: SyntaxError %s', i) else: _logger.info( 'autoreload: python code updated, autoreload activated') openerp.service.server.restart() def check_thread(self): # Check if some files have been touched in the addons path. # If true, check if the touched file belongs to an installed module # in any of the database used in the registry manager. self.observer.run() def run(self): t = threading.Thread(target=self.check_thread) t.setDaemon(True) t.start() _logger.info('AutoReload watcher running')
class Command(BaseCommand): monitor = None option_list = BaseCommand.option_list + ( make_option("--path", action="store", dest="path", help="Music library path to watch"), make_option("--start", action="store_true", dest="start", help="Start watching directory for changes"), make_option("--stop", action="store_true", dest="stop", help="Stop watching directory for changes"), ) def handle(self, *args, **options): if options["path"] is None: print "Required arguments: path" return if not os.path.exists(options["path"]): print "Path does not exist" return pidFile = os.path.dirname( os.path.abspath(__file__)) + "/../../jukebox_watch.pid" if options["start"]: if os.path.exists(pidFile): print "Watch daemon already running, pid file exists" return self.watch(pidFile, options['path']) elif options["stop"]: if not os.path.exists(pidFile): print "Daemon not running" return print "Stopping daemon..." pid = int(open(pidFile).read()) os.kill(pid, SIGTSTP) def watch(self, pidFile, path): pid = daemon.pidfile.TimeoutPIDLockFile(pidFile, 10) print "Starting jukebox_watch daemon..." self.daemon = daemon.DaemonContext(uid=os.getuid(), gid=os.getgid(), pidfile=pid, working_directory=os.getcwd(), detach_process=True, signal_map={SIGTSTP: self.shutdown}) with self.daemon: # create watchmanager, eventhandler and notifier handler = EventHandler() self.monitor = Observer() self.monitor.schedule(handler, path, recursive=True) self.monitor.run() def shutdown(self, signal, action): if self.monitor is not None: self.monitor.stop() self.daemon.close() sys.exit(0)
class Command(BaseCommand): monitor = None option_list = BaseCommand.option_list + ( make_option("--path", action="store", dest="path", help="Music library path to watch" ), make_option("--start", action="store_true", dest="start", help="Start watching directory for changes" ), make_option("--stop", action="store_true", dest="stop", help="Stop watching directory for changes" ), ) def handle(self, *args, **options): if options["path"] is None: print "Required arguments: path" return if not os.path.exists(options["path"]): print "Path does not exist" return pidFile = os.path.dirname( os.path.abspath(__file__) ) + "/../../jukebox_watch.pid" if options["start"]: if os.path.exists(pidFile): print "Watch daemon already running, pid file exists" return self.watch(pidFile, options['path']) elif options["stop"]: if not os.path.exists(pidFile): print "Daemon not running" return print "Stopping daemon..." pid = int(open(pidFile).read()) os.kill(pid, SIGTSTP) def watch(self, pidFile, path): pid = daemon.pidfile.TimeoutPIDLockFile( pidFile, 10 ) print "Starting jukebox_watch daemon..." self.daemon = daemon.DaemonContext( uid=os.getuid(), gid=os.getgid(), pidfile=pid, working_directory=os.getcwd(), detach_process=True, signal_map={ SIGTSTP: self.shutdown } ) with self.daemon: # create watchmanager, eventhandler and notifier handler = EventHandler() self.monitor = Observer() self.monitor.schedule(handler, path, recursive=True) self.monitor.run() def shutdown(self, signal, action): if self.monitor is not None: self.monitor.stop() self.daemon.close() sys.exit(0)
class AutoReloadWatchdog(object): def __init__(self, server): from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from watchdog.events import FileCreatedEvent from watchdog.events import FileModifiedEvent self.server = server self.files = {} self.modules = {} class EventHandler(FileSystemEventHandler): def __init__(self, autoreload): self.autoreload = autoreload def process(self): l = self.autoreload.files.keys() self.autoreload.files.clear() self.autoreload.process_data(l) self.autoreload.process_python(l) def on_created(self, event): if isinstance(event, FileCreatedEvent): _logger.debug('File created: %s', event.src_path) self.autoreload.files[event.src_path] = 1 self.process() def on_modified(self, event): if isinstance(event, FileModifiedEvent): _logger.debug('File modified: %s', event.src_path) self.autoreload.files[event.src_path] = 1 self.process() #self.wm = pyinotify.WatchManager() self.handler = EventHandler(self) self.observer = Observer() #self.notifier = pyinotify.Notifier(self.wm, self.handler, timeout=0) #mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE # IN_MOVED_FROM, IN_MOVED_TO ? for path in openerp.tools.config.options["addons_path"].split(','): _logger.info('Watching addons folder %s', path) #self.wm.add_watch(path, mask, rec=True) self.observer.schedule(self.handler, path=path, recursive=True) def process_data(self, files): xml_files = [i for i in files if i.endswith('.xml')] addons_path = openerp.tools.config.options["addons_path"].split(',') for i in xml_files: for path in addons_path: if i.startswith(path): # find out which addons path the file belongs to # and extract it's module name right = i[len(path) + 1:].split('/') if len(right) < 2: continue module = right[0] self.modules[module]=1 if self.modules: _logger.info('autoreload: xml change detected, autoreload activated') openerp.service.server.restart() def process_python(self, files): # process python changes py_files = [i for i in files if i.endswith('.py')] py_errors = [] # TODO keep python errors until they are ok if py_files: for i in py_files: try: source = open(i, 'rb').read() + '\n' compile(source, i, 'exec') except SyntaxError: py_errors.append(i) if py_errors: _logger.info('autoreload: python code change detected, errors found') for i in py_errors: _logger.info('autoreload: SyntaxError %s',i) else: _logger.info('autoreload: python code updated, autoreload activated') openerp.service.server.restart() def check_thread(self): # Check if some files have been touched in the addons path. # If true, check if the touched file belongs to an installed module # in any of the database used in the registry manager. self.observer.run() def run(self): t = threading.Thread(target=self.check_thread) t.setDaemon(True) t.start() _logger.info('AutoReload watcher running')