def test_since_stream(self): events = [] def callback(*args): events.append(args) # two files in same directory import os path1 = os.path.realpath(self._make_tempdir()) + '/' f = self._make_temporary(path1)[0] g = self._make_temporary(path1)[0] from fsevents import Stream, FS_FLAGHISTORYDONE stream = Stream(callback, path1, ids = True) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() #create one file import time while not observer.isAlive(): time.sleep(0.1) del events[:] f.close() time.sleep(0.2) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() self.assertEqual(len(events),1) self.assertEqual(events[0][:-1], (path1, self.create_and_remove_mask)) #create a second file g.close() stream = Stream(callback, path1, since = events[0][2]) del events[:] # new observer observer = Observer() observer.schedule(stream) observer.start() time.sleep(0.2) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() self.assertEqual(len(events),2) #FIXME: why do events arrive here in reversed order? self.assertEqual(events[1], (path1, self.create_and_remove_mask)) self.assertEqual(events[0], (path1[:-1], FS_FLAGHISTORYDONE))
def test_start_then_watch(self): events = [] def callback(*args): events.append(args) f, path = self._make_temporary() from fsevents import Stream stream = Stream(callback, path) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) del events[:] f.close() time.sleep(0.2) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() self.assertEqual(events, [(path, self.create_and_remove_mask)])
def __init__(self, path, f, ignore=None): logging.info("Using FSEvents") self.path = path self.f = f self.ignore = ignore self.observer = Observer() self.observer.daemon = True self._streams = [] self._streams.append(createStream(self.path, path, self._update)) self._streamed_folders = [self.path] def add_stream(p): if p in self._streamed_folders: return self._streams.append(createStream(p, file_path, self._update)) self._streamed_folders.append(p) # Start an extra listener for all symlinks for file_path in fileList(self.path, folders=True): if os.path.islink(file_path): if os.path.isdir(file_path): add_stream(os.path.realpath(file_path)) else: add_stream(os.path.dirname(os.path.realpath(file_path)))
def main(): observer = Observer() observer.start() path = '/Users/seungjin/Desktop' def callback(event): #print "mask: " + str(event.mask) #print "cookie: " + str(event.cookie) #print "name: " + str(event.name) print event if event.mask == 256: #looks like new file comming newFile(str(event.name)) elif event.mask == 512: #looks line file deleted rmFile(str(event.name)) elif event.mask == 2: #looks like overwriting? print "hihihihi" def newFile(filename): print "new file is comming" #pushing this file into cloud gs = Googlestorage() #print gs.list_objects() gs.upload_objects(filename) def rmFile(filename): #print "%s is removed" % filename gs = Googlestorage( ) # this is evil.. do i need to make global goolgestorage object??? idk gs.delete_objects_and_buckets(filename) stream = Stream(callback, path, file_events=True) observer.schedule(stream)
def watch(self): self.observer = Observer() self.observer.start() self.stream = Stream(self.file_event_callback, self.root, file_events=True) self.observer.schedule(self.stream)
def _loop_darwin(self): # pragma: no cover """loop implementation for darwin platform""" from fsevents import Observer #pylint: disable=F0401 from fsevents import Stream #pylint: disable=F0401 from fsevents import IN_MODIFY #pylint: disable=F0401 observer = Observer() handler = self._handle def fsevent_callback(event): if event.mask == IN_MODIFY: handler(event) for watch_this in self.watch_dirs: stream = Stream(fsevent_callback, watch_this, file_events=True) observer.schedule(stream) observer.daemon = True observer.start() try: # hack to keep main thread running... import time while True: time.sleep(99999) except (SystemExit, KeyboardInterrupt): pass
def test_existing_directories_are_not_reported(self): import os from fsevents import Stream, Observer events = [] def callback(event): events.append(event) stream = Stream(callback, self.tempdir, file_events=True) new1 = os.path.join(self.tempdir, "newdir1") new2 = os.path.join(self.tempdir, "newdir2") os.mkdir(new1) observer = Observer() observer.schedule(stream) observer.start() import time while not observer.isAlive(): time.sleep(0.1) del events[:] time.sleep(1) os.mkdir(new2) try: time.sleep(1.1) observer.stop() observer.unschedule(stream) observer.join() from fsevents import IN_CREATE self.assertEqual(len(events), 1) self.assertEqual(events[0].mask, IN_CREATE) self.assertEqual(events[0].name, os.path.realpath(new2)) finally: os.rmdir(new1) os.rmdir(new2)
def __init__(self, directories, **kwargs): self._changes = _changes = [] # Path to current module module_dir = directories[0] # Paths to watch paths = [module_dir] # Explicitly adding paths outside of module dir. for path in NON_MODULE_DIRS: paths.append(path) self.observer = Observer() def callback(event, mask=None): # Get extension try: ext = os.path.splitext(event.name)[1] except IndexError: ext = None # Add to changes if we're watching a file with this extension. if ext in WATCHED_EXTENSIONS: _changes.append(event.name) self.stream = Stream(callback, file_events=True, *paths)
def start(self): self.stream = Stream(self.directory_changed, *self.target_dirs) self.observer = Observer() self.observer.schedule(self.stream) self.observer.daemon = True # Kill observer when main thread killed. self.observer.start() # start observer in the other thread.
def test_single_file_added_multiple_streams(self): events = [] def callback(*args): events.append(args) f, path = self._make_temporary() from fsevents import Stream stream1 = Stream(callback, path) stream2 = Stream(callback, path) from fsevents import Observer observer = Observer() observer.schedule(stream1) observer.schedule(stream2) observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) time.sleep(0.1) del events[:] f.close() time.sleep(0.2) # stop and join observer observer.stop() observer.unschedule(stream1) observer.unschedule(stream2) observer.join() self.assertEquals(events, [(path, 0), (path, 0)])
def test_start_no_watch(self): events = [] def callback(*args): events.append(args) from fsevents import Observer observer = Observer() f, path = self._make_temporary() observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) del events[:] f.close() time.sleep(0.2) # stop and join observer observer.stop() observer.join() self.assertEqual(events, [])
def test_single_file_added_with_observer_unscheduled(self): events = [] def callback(*args): events.append(args) f, path = self._make_temporary() from fsevents import Stream stream = Stream(callback, path) from fsevents import Observer observer = Observer() observer.start() import time while not observer.isAlive(): time.sleep(0.1) observer.schedule(stream) observer.unschedule(stream) # add single file del events[:] f.close() time.sleep(0.1) # stop and join observer observer.stop() observer.join() self.assertEqual(events, [])
def watch_files(paths): global observer observer = Observer() stream = Stream(update_file, file_events=True, *paths) observer.schedule(stream) log.info("Starting observer") observer.daemon = True observer.start() log.info("Observer started")
def start_events(self): """Setup the observer.""" self.get_tree(cached=False) if self.observer is None: self.observer = Observer() stream = Stream(self.fsevent_handler, self.real_root, ids=True) self.observer.schedule(stream) if not self.observer.is_alive(): self.observer.start()
def watch_local_files(): print "Watching for changes to: {}".format(", ".join(TEMPLATES.keys())) observer = Observer() stream = Stream(event_callback, os.getcwd(), file_events=True) observer.schedule(stream) try: observer.start() while True: time.sleep(86400) except KeyboardInterrupt: observer.stop()
def main(): arg_parser = argparse.ArgumentParser() arg_parser.add_argument('path') arg_parser.add_argument('-c', '--cmd', required=True, nargs='+', help='Console command for execute') arg_parser.add_argument('-d', '--delay', type=int, default=5, nargs='?', help='Synchronization delay in seconds') arg_parser.add_argument('-f', '--files', action='store_true', help="show changed files snapshot") arg_parser.add_argument('-v', '--verbose', action='store_true', help='increase verbosity') try: args = arg_parser.parse_args() path = args.path if not os.path.isdir(path): raise IOError('Path `%s` is not a directory' % path) event_handler = EventHandler() callback_params = { 'event_handler': event_handler, 'cmd': args.cmd, 'verbose': args.verbose, 'show_files': args.files } timer = TimerInterval(callback_func, args.delay, **callback_params) stream = Stream(event_handler.callback_handler, path, file_events=True) observer = Observer() observer.schedule(stream) observer.start() timer.start() except IOError as e: print e except KeyboardInterrupt: observer.unschedule(stream) observer.stop() timer.stop()
def loop(self): observer = Observer() observer.schedule(self.stream) observer.daemon = True observer.start() try: while True: # Cannot use observer.join() as it is not interrupted by # Ctrl-C time.sleep(10000) finally: observer.unschedule(self.stream) observer.stop()
def get_notified(path_regexps, project_path): """Get notified when files change, and minify them. """ observer = Observer() observer.start() def file_changed(file_change_event): """Callback for when a file has changed""" file_path = file_change_event.name if is_minifiable(file_path) and needs_minifying(file_path): tell_user_and_minify(file_path) stream = Stream(file_changed, project_path, file_events=True) observer.schedule(stream)
def test_multiple_files_added(self): events = [] def callback(*args): events.append(args) from fsevents import Observer observer = Observer() from fsevents import Stream observer.start() # wait until activation import time while not observer.isAlive(): time.sleep(0.1) time.sleep(0.1) # two files in same directory import os path1 = os.path.realpath(self._make_tempdir()) + '/' f = self._make_temporary(path1)[0] g = self._make_temporary(path1)[0] # one file in a separate directory path2 = os.path.realpath(self._make_tempdir()) + '/' h = self._make_temporary(path2)[0] stream = Stream(callback, path1, path2) observer.schedule(stream) try: del events[:] f.close() g.close() h.close() time.sleep(0.2) self.assertEqual( sorted(events), sorted([(path1, self.create_and_remove_mask), (path2, self.create_and_remove_mask)])) finally: f.close() g.close() h.close() os.rmdir(path1) os.rmdir(path2) # stop and join observer observer.stop() observer.unschedule(stream) observer.join()
def main(): usage = 'usage: %prog [options] CALL_DIRECTORY' parser = OptionParser(usage=usage) parser.add_option('-p', '--port', dest='port', help='port to listen on') parser.add_option('-a', '--address', dest='address', help='address to listen on') (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() sys.exit(1) call_definition_path = args[0] if options.port: port = int(options.port) else: port = PORT if options.address: ip_address = options.address else: ip_address = IP_ADDRESS # Monitor the call definition path to restart the # server if any of the files change, or new ones # are added observer = Observer() observer.start() stream = Stream(definition_change, call_definition_path) observer.schedule(stream) global call_handler call_handler = CallHandler(call_definition_path) server_class = BaseHTTPServer.HTTPServer global httpd httpd = server_class((ip_address, port), HttpRequestHandler) print 'WebServiceSimulator started' try: httpd.serve_forever() except KeyboardInterrupt: pass finally: print 'Shutting down web service simulator' httpd.server_close() sys.exit(0)
def start_scanner(path): """ watch for file events in the supplied path """ try: observer = Observer() observer.start() stream = Stream(file_modified, path, file_events=True) observer.schedule(stream) print "Watching for changes. Press Ctrl-C to stop." while 1: pass except (KeyboardInterrupt, OSError, IOError): observer.unschedule(stream) observer.stop()
def main(): logging.basicConfig(level=logging.INFO) load_config() observer = Observer() observer.setDaemon(True) observer.start() observer.schedule(Stream(callback, ".", file_events=True)) try: while True: time.sleep(10) except KeyboardInterrupt: pass finally: observer.stop() print("")
def main(): path = os.path.expanduser('~') enemy_name = '.DS_Store' observer = Observer() def callback(path, mask): try: full_path = os.path.join(path, enemy_name) os.remove(full_path) except: pass stream = Stream(callback, path) observer.schedule(stream) observer.start() subprocess.call(['find', path, '-name', '\\' + enemy_name, '-delete']) observer.join()
def loop(self): observer = Observer() observer.schedule(self.stream) observer.daemon = True observer.start() try: while True: try: # Cannot use blocking get() as it is not interrupted by # Ctrl-C if self.wait_queue.get(10000) is True: self.force_restart() except Empty: pass finally: observer.unschedule(self.stream) observer.stop()
def test_single_file_created_and_modified(self): events = [] def callback(event): events.append(event) from fsevents import Stream stream = Stream(callback, self.tempdir, file_events=True) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) del events[:] time.sleep(2.1) import os f = open(os.path.join(self.tempdir, "test"), "w") f.write("abc") f.flush() time.sleep(1.0) f.write("def") f.flush() f.close() time.sleep(0.1) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() os.unlink(f.name) from fsevents import IN_CREATE, IN_MODIFY self.assertEqual(len(events), 2) self.assertEqual(events[0].mask, IN_CREATE) self.assertEqual(events[0].name, os.path.realpath(f.name)) self.assertEqual(events[1].mask, IN_MODIFY) self.assertEqual(events[1].name, os.path.realpath(f.name))
def test_single_file_moved(self): events = [] def callback(event): events.append(event) import os f = open(os.path.join(self.tempdir, "test"), "w") f.write("abc") f.flush() f.close() from fsevents import Stream stream = Stream(callback, self.tempdir, file_events=True) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) del events[:] time.sleep(2.1) new = "%s.new" % f.name os.rename(f.name, new) time.sleep(0.1) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() os.unlink(new) from fsevents import IN_MOVED_FROM from fsevents import IN_MOVED_TO self.assertEqual(len(events), 2) self.assertEqual(events[0].mask, IN_MOVED_FROM) self.assertEqual(events[0].name, os.path.realpath(f.name)) self.assertEqual(events[1].mask, IN_MOVED_TO) self.assertEqual(events[1].name, os.path.realpath(new)) self.assertEqual(events[0].cookie, events[1].cookie)
def test_single_file_added_to_subdirectory(self): events = [] def callback(*args): events.append(args) import os directory = self._make_tempdir() subdirectory = os.path.realpath(os.path.join(directory, 'subdir')) + '/' os.mkdir(subdirectory) import time time.sleep(0.1) try: from fsevents import Stream stream = Stream(callback, directory) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() # add single file while not observer.isAlive(): time.sleep(0.1) del events[:] f = open(os.path.join(subdirectory, "test"), "w") f.write("abc") f.close() time.sleep(0.2) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() self.assertEqual(len(events), 1) self.assertEqual(events, [(subdirectory, self.modified_mask)]) finally: os.unlink(f.name) os.rmdir(subdirectory) os.rmdir(directory)
def _loop_darwin(self): # pragma: no cover """loop implementation for darwin platform""" from fsevents import Observer #pylint: disable=F0401 from fsevents import Stream #pylint: disable=F0401 from fsevents import IN_MODIFY #pylint: disable=F0401 observer = Observer() handler = self._handle def fsevent_callback(event): if event.mask == IN_MODIFY: handler(event) for watch_this in self.watch_dirs: stream = Stream(fsevent_callback, watch_this, file_events=True) observer.schedule(stream) observer.daemon = True observer.run()
def test_single_directory_deleted(self): events = [] def callback(event): events.append(event) import os new1 = os.path.join(self.tempdir, "newdir1") new2 = os.path.join(self.tempdir, "newdir2") try: os.mkdir(new1) os.mkdir(new2) import time time.sleep(0.2) from fsevents import Stream stream = Stream(callback, self.tempdir, file_events=True) from fsevents import Observer observer = Observer() observer.schedule(stream) observer.start() # add single file import time while not observer.isAlive(): time.sleep(0.1) del events[:] time.sleep(0.1) os.rmdir(new2) time.sleep(1.0) # stop and join observer observer.stop() observer.unschedule(stream) observer.join() from fsevents import IN_DELETE self.assertEqual(len(events), 1) self.assertEqual(events[0].mask, IN_DELETE) self.assertEqual(events[0].name, os.path.realpath(new2)) finally: os.rmdir(new1)
def main(): signal.signal(signal.SIGINT, signal_handler) parser = argparse.ArgumentParser( description='Sync files to a remote server of scp.') parser.add_argument('path', type=str, metavar=('local', 'remote'), nargs=2, help='path for the monitor') args = parser.parse_args() print args.path observer = Observer() stream = Stream(lambda x, y: callback(x, y, args.path[1]), args.path[0]) observer.schedule(stream) try: observer.start() while True: # instead of this infinite loop, you can do pass # whatever processing you wanted except KeyboardInterrupt: observer.stop()