def test_watchdog(fixtures): """Checks that the file system observer triggers imports on new/changed files""" tmpdir = fixtures["root"] acq_dir = tmpdir.join("12345678T000000Z_inst_zab") node = st.StorageNode.get(st.StorageNode.name == "x") import watchdog.events as ev watchdog_handler = auto_import.RegisterFile(node) # new acquisition file f = acq_dir.join("new_file.log") f.write("") assert file_copy_count("new_file.log") == 0 watchdog_handler.on_created(ev.FileCreatedEvent(str(f))) assert file_copy_count("new_file.log") == 1 # this file is outside any acqs and should be ignored g = tmpdir.join("some_file.log") g.write("Where is my acq?!") assert file_copy_count("some_file.log") == 0 watchdog_handler.on_created(ev.FileCreatedEvent(str(g))) assert file_copy_count("some_file.log") == 0 # now delete the lock and try reimport, which should succeed lock = acq_dir.join(".foo.zxc.lock") lock.remove() assert file_copy_count("foo.zxc") == 0 watchdog_handler.on_deleted(ev.FileDeletedEvent(str(lock))) assert file_copy_count("foo.zxc") == 1
def test_on_modify(handler: XacroUpdateHandler, xacro_file, xacro_dir, robot_description_server, canonicalize_xml): # Invalid types handler.on_modified(wevt.FileCreatedEvent(xacro_file)) handler.on_modified(wevt.FileDeletedEvent(xacro_file)) handler.on_modified(wevt.FileMovedEvent(xacro_file, '')) handler.on_modified(wevt.DirCreatedEvent(xacro_dir)) handler.on_modified(wevt.DirMovedEvent(xacro_dir, '')) handler.on_modified(wevt.DirDeletedEvent(xacro_dir)) handler.on_modified(wevt.DirModifiedEvent(xacro_dir)) # Invalid file handler.on_modified(wevt.FileModifiedEvent('')) rclpy.spin_once(robot_description_server, timeout_sec=1) assert robot_description_server.get_parameter( 'robot_description').value == '' # Valid file handler.on_modified(wevt.FileModifiedEvent(xacro_file)) rclpy.spin_once(robot_description_server, timeout_sec=1) expected = canonicalize_xml(handler.xacro_observer.xacro_tree.xml_string()) actual = canonicalize_xml( robot_description_server.get_parameter('robot_description').value) assert expected == actual
def test_cursor_prune(fsp): fsp._event_window = 20 cs1 = fsp.latest_cursor for i in range(100): ev = watchdog_events.FileCreatedEvent("/file%s" % i) fsp._on_any_event(ev) i = 0 last = None for ev in fsp.events(): cpos = ev.new_cursor i += 1 last = ev assert last.oid == "/file99" assert fsp.current_cursor == cpos assert i == 100 fsp.current_cursor = cs1 i = 0 for ev in fsp.events(): cpos = ev.new_cursor last = ev i += 1 assert last.oid == "/file99" assert fsp.current_cursor == cpos assert i == 20
def HandleRenameFile(event: evt.FileMovedEvent) -> None: ''' Event handler for a renamed file in the watched folder. Checks to see if the new name is the name of an existing reference file, if not it raises a new file event which is caught by HandleNewFile. Parameters ---------- event : evt.FileMovedEvent DESCRIPTION. ''' print(event) # Filter out any temp download files if (event.src_path.split(".")[-1] == "crdownload"): return global bibFolder time.sleep(5) title = ExtractTitle(event.dest_path) # Check if there is a bib file in the bib folder already. # Currently only uses .ris files # If file doesn't exist, pass event to new file handler if (not Path(bibFolder + title + ".ris").is_file()): HandleNewFile(evt.FileCreatedEvent(event.dest_path)) # Ris file exists, rename pdf file to the same name as the ris file else: newTitle = "/".join( event.dest_path.split("/")[:-1]) + "/" + title + ".pdf" rename(event.dest_path, newTitle)
def test_add_remove(self): e = events.FileCreatedEvent('added.jpg') self.playlist._wrap_asset_iter.on_created(e) self.assertEqual(self.playlist.length(), 3) e = events.FileDeletedEvent('added.jpg') self.playlist._wrap_asset_iter.on_deleted(e) self.assertEqual(self.playlist.length(), 2)
def test_created(repo_path, fake_handler): fake_handler.dispatch(events.FileCreatedEvent(os.path.join( repo_path, 'test'))) assert fake_handler.on_any_event.call_count == 1 assert fake_handler.on_created.call_count == 1 assert fake_handler.on_deleted.call_count == 0 assert fake_handler.on_modified.call_count == 0 assert fake_handler.on_moved.call_count == 0
def test_put_with_watchdog_events(): # FileSystemEvent.__ne__() uses the key property without # doing any type checking. Since _last_item is set to # None in __init__(), an AttributeError is raised when # FileSystemEvent.__ne__() tries to use None.key queue = SkipRepeatsQueue() dummy_file = 'dummy.txt' event = events.FileCreatedEvent(dummy_file) queue.put(event) assert queue.get() is event
def test_on_create_generates_output(self): markdown = os.path.join(self.site.path, 'index.md') open(markdown, 'w').close() handler = SiteHandler(self.director) event = events.FileCreatedEvent(markdown) handler.on_created(event) html = os.path.join(self.director.config.outdir, 'index.html') self.assertTrue(os.path.exists(html))
def test_add_random(self): asset_name_lst = [ 'test/media/home/20190601_12440.png', 'test/media/home/IMG_6849.png' ] e = events.FileCreatedEvent('added.jpg') self.playlist._wrap_asset_iter.on_created(e) asset_name_lst.insert(0, 'added.jpg') self.assertEqual(self.playlist.length(), 3) self.assertEqual( self.playlist.get_next(True).filename, asset_name_lst[0])
def test_file_on_create(self): content = b"now is the winter of our discontent" checksum = md5(content).hexdigest() p = osp.join(self.ws, "foo.txt") with open(p, "wb") as f: f.write(content) ev = events.FileCreatedEvent(p) inode = dict(node_path=p) yield self.h._add_hash_to_inode(ev, inode) self.assertIn("md5", inode, "checksum was not added to inode") self.assertEquals(checksum, inode["md5"])
def _dispatch_test_helper(self, path, ignore_directories=False): """patterns: 'relative/path/*.py', 'relative/path/src/' ignore_patterns: 'relative/path/*.rst', 'relative/path/__pycache__/', 'relative/path/htmlcov/' """ from watchdog import events class EchoAutoRunTrick(AutoRunTrick): log = [] def __del__(self): del type(self).log def on_any_event(self, event): type(self).log += ['on_any_event'] def on_created(self, event): type(self).log += ['on_created'] def on_modified(self, event): type(self).log += ['on_modified'] def on_moved(self, event): type(self).log += ['on_moved'] def on_deleted(self, event): type(self).log += ['on_deleted'] included = ['relative/path/*.py', 'relative/path/src/'] excluded = ['relative/path/*.rst', 'relative/path/__pycache__/', 'relative/path/htmlcov/'] handler = EchoAutoRunTrick(patterns=included, ignore_patterns=excluded, ignore_directories=ignore_directories) created = events.FileCreatedEvent(path) modified = events.FileModifiedEvent(path) deleted = events.FileDeletedEvent(path) moved = events.FileMovedEvent(path, 'relative/path/yummy.rst') dircreated = events.DirCreatedEvent(path) dirmodified = events.DirModifiedEvent(path) dirdeleted = events.DirDeletedEvent(path) dirmoved = events.DirMovedEvent(path, 'relative/path/htmlcov/') fevents = (created, modified, moved, deleted) devents = (dircreated, dirmodified, dirmoved, dirdeleted) event_types = ['on_created', 'on_modified', 'on_moved', 'on_deleted'] return handler, fevents, devents, event_types
def events(self): if self._ignore: self._pool = OrderedDict([(k, v) for k, v in self._pool.items() if not is_ignored(k)]) self._final = OrderedDict([(k, v) for k, v in self._final.items() if not is_ignored(k)]) self._initial = OrderedDict([(k, v) for k, v in self._initial.items() if not is_ignored(k)]) i_pool = {v: k for k, v in self._pool.items()} i_final = {v: k for k, v in self._final.items()} i_initial = {v: k for k, v in self._initial.items()} moved = set() created = set(self._final.keys()) - set(self._initial.keys()) deleted = set(self._initial.keys()) - set(self._final.keys()) modified = set(i for i in self._pool.values() if i.modified and not i.is_folder) # Probably don't need both loops here but better safe than sorry # If an item has been created and deleted it is actuall a move for key in set(created): src = i_initial.get(self._final[key]) if src: created.remove(key) moved.add((src, key)) for key in set(deleted): dest = i_final.get(self._initial[key]) if dest: deleted.remove(key) moved.add((key, dest)) # Sort by place in the file hierarchy # Children come before parents # NOTE: Create events must be sorted THE OPPOSITE direction sorter = lambda x: x.count(os.path.sep) # Windows reports folder deletes are file deletes + modifies # If a child exists for any file assume it is a directory for delete in deleted: for other in deleted: if delete != other and other.startswith(delete): self._initial[delete].is_folder = True break evts = list( itertools.chain( (events.DirMovedEvent(src, dest) if self._final[dest].is_folder else events.FileMovedEvent(src, dest) for src, dest in sorted(moved, key=lambda x: x[0].count(os.path.sep), reverse=True)), (events.DirDeletedEvent(x) if self._initial[x].is_folder else events.FileDeletedEvent(x) for x in sorted(deleted, key=sorter, reverse=True)), (events.DirCreatedEvent(x) if self._final[x].is_folder else events.FileCreatedEvent(x) for x in sorted(created, key=sorter)), (events.FileModifiedEvent(i_pool[x]) for x in modified if x in i_final and not i_pool[x] in created), )) mapped = set([(getattr(event, 'dest_path', event.src_path), event.event_type) for event in evts if event.is_directory and not event.event_type == EVENT_TYPE_CREATED]) # Do our best to dedup all found events. # If there is a matching event type at a parent path disregard the children def check(event): segments = getattr(event, 'dest_path', event.src_path).split(os.path.sep) for i in range(len(segments) - 1): if (os.path.sep.join(segments[:i + 1]), event.event_type) in mapped: return False return True return list(filter(check, evts))