async def async_start_monitoring(self, event): """Start monitoring of events and devices. Start inotify watching for events, start event monitoring for those already connected, and start monitoring for device connection/disconnection. """ # start watching self.watcher = aionotify.Watcher() self.watcher.watch( alias="devinput", path=DEVINPUT, flags=aionotify.Flags.CREATE | aionotify.Flags.ATTRIB | aionotify.Flags.DELETE, ) await self.watcher.setup(self.hass.loop) # add initial devices (do this AFTER starting watcher in order to # avoid race conditions leading to missing device connections) initial_start_monitoring = set() descriptors = list_devices(DEVINPUT) for descriptor in descriptors: dev, handler = self.get_device_handler(descriptor) if handler is None: continue self.active_handlers_by_descriptor[descriptor] = handler initial_start_monitoring.add(handler.async_start_monitoring(dev)) if initial_start_monitoring: await asyncio.wait(initial_start_monitoring) self.monitor_task = self.hass.async_create_task(self.async_monitor_devices())
async def start_child(): """ Start the child process that will look for changes in modules. """ logger.info('Started to watch for code changes') loop = asyncio.get_event_loop() watcher = aionotify.Watcher() flags = (aionotify.Flags.MODIFY | aionotify.Flags.DELETE | aionotify.Flags.ATTRIB | aionotify.Flags.MOVED_TO | aionotify.Flags.MOVED_FROM | aionotify.Flags.CREATE | aionotify.Flags.DELETE_SELF | aionotify.Flags.MOVE_SELF) watched_dirs = list_dirs() for dir_name in watched_dirs: watcher.watch(path=dir_name, flags=flags) await watcher.setup(loop) while True: evt = await watcher.get_event() file_path = path.join(evt.alias, evt.name) if file_path in watched_dirs or file_path.endswith('.py'): await asyncio.sleep(settings.CODE_RELOAD_DEBOUNCE) break watcher.close() exit_for_reload()
async def watch_handler(): flags = aionotify.Flags.MODIFY | aionotify.Flags.CREATE | aionotify.Flags.MOVED_TO | aionotify.Flags.IGNORED dir_ = os.path.dirname(handler.__file__) file_ = os.path.basename(handler.__file__) watcher = aionotify.Watcher() watcher.watch(dir_, flags) await watcher.setup(loop) while True: event = await watcher.get_event() if event.name == '' and flags & aionotify.Flags.IGNORED: print( "inotify watch was removed by OS, cannot reload automatically") break if event.name != file_: continue try: reload(handler) except Exception as e: print("Exception reloading: {}".format(e)) watcher.close()
def __init__(self, config): """ Build a Controller instance. If another controller is already instantiated on the system (or if this is instantiated somewhere other than a robot) then this method will raise a RuntimeError. """ if not opentrons.config.IS_ROBOT: MODULE_LOG.warning( 'This is intended to run on a robot, and while it can connect ' 'to a smoothie via a usb/serial adapter unexpected things ' 'using gpios (such as smoothie reset or light management) ' 'will fail') self.config = config or opentrons.config.robot_configs.load() self._gpio_chardev = build_gpio_chardev('gpiochip0') self._board_revision = BoardRevision.UNKNOWN # We handle our own locks in the hardware controller thank you self._smoothie_driver = driver_3_0.SmoothieDriver_3_0_0( config=self.config, gpio_chardev=self._gpio_chardev, handle_locks=False) self._cached_fw_version: Optional[str] = None try: self._module_watcher = aionotify.Watcher() self._module_watcher.watch( alias='modules', path='/dev', flags=(aionotify.Flags.CREATE | aionotify.Flags.DELETE)) except AttributeError: MODULE_LOG.warning( 'Failed to initiate aionotify, cannot watch modules ' 'or door, likely because not running on linux')
async def start(self, file_path, locale=None, kwargs=None): """ Setup the watching utilities, start the loop and load data a first time. """ self._file_path = os.path.realpath(file_path) self._locale = locale if kwargs: self._kwargs = kwargs if settings.I18N_LIVE_RELOAD: loop = asyncio.get_event_loop() self._running = True self._watcher = aionotify.Watcher() self._watcher.watch( path=os.path.dirname(self._file_path), flags=aionotify.Flags.MOVED_TO | aionotify.Flags.MODIFY, ) await self._watcher.setup(loop) await self._load() loop.create_task(self._watch()) else: await self._load()
async def run_forever(self): '''Process magic commands.''' open(self.path, 'w').close() try: watcher = aionotify.Watcher() watcher.watch(self.path, aionotify.Flags.CLOSE_WRITE) await watcher.setup(self.loop) try: while True: try: await watcher.get_event() await self._handle() self.logger.debug( f'Detected status change for magic file') except concurrent.futures.CancelledError: # Break loop (likely normal exit through task cancellation) break except Exception: # pylint: disable=broad-except self.logger.exception( f'Exception while watching magic file') finally: watcher.unwatch(self.path) watcher.close() finally: if os.path.exists(self.path): os.unlink(self.path)
def on_subscribe(observer, scheduler): watcher = aionotify.Watcher() async def read_events(): nonlocal observer await watcher.setup(loop) while True: event = await watcher.get_event() loop.call_soon(observer.on_next, Event(id=event.alias, path=event.name)) watcher.close() def on_next(item): if type(item) is AddWatch: watcher.watch(alias=item.id, path=item.path, flags=item.flags) elif type(item) is Start: asyncio.ensure_future(read_events()) else: observer.on_error("unknown item: {}".format(type(item))) sink.request.subscribe(on_next=on_next, on_error=lambda e: observer.on_error(e))
async def main(loop): # Setup the watcher watcher = aionotify.Watcher() watcher.watch(alias='log', path='./tmp.log', flags=aionotify.Flags.MODIFY) await watcher.setup(loop) async for event in aiter(watcher): print(event)
def __init__(self, path, flags=None, loop=None, logger=None): self.path = path self.flags = flags or (aionotify.Flags.MODIFY | aionotify.Flags.CREATE | aionotify.Flags.DELETE) self.loop = loop or asyncio.get_event_loop() self.logger = logger or logging self.watcher = aionotify.Watcher()
def setUp(self): if AIODEBUG: self.loop.set_debug(True) self.watcher = aionotify.Watcher() self._testdir = tempfile.TemporaryDirectory(dir=TESTDIR) self.testdir = self._testdir.name # Schedule a loop shutdown self.loop.call_later(self.timeout, self.loop.stop)
def file_change_monitor(template_system, filepath, is_yaml=False, var_name='file'): """ On file change updates template context with a file content and renders it. Example #1 (Yaml file) : {{ load_module('legion.ltemplate.render', filepath='config.yml', is_yaml=True, var_name='conf') }} <b>Hosts values:</b> <ul> {% for item in conf: %} <li>{{ item.hosts }}</li> {% endfor %} </ul> Example #2 (Txt file) : {{ load_module('legion.io.render', filepath='config.txt') }} <h1>Config file Context:</h1> <pre>{{ file }}</pre> :param template_system: Object, that contains 'render' callback function :param filepath: Path to a File, relative to template file or absolute :type filepath: str :param is_yaml_file: Indicates if file is in yaml format and it should be parsed :type is_yaml_file: bool :param var_name: variable name, used to store file content in context :type var_name: str :return: None """ if not os.path.isabs(filepath): filepath = os.path.join( os.path.dirname(template_system.template_file_path), filepath) LOGGER.info( 'Starting file change monitor for file {}, is_yaml={} as variable={}'. format(filepath, is_yaml, var_name)) with open(filepath) as f: if is_yaml: template_system.render(**{var_name: yaml.load(f)}) else: template_system.render(**{var_name: f.read()}) LOGGER.debug('Initializing watcher for file {}'.format(filepath)) watcher = aionotify.Watcher() watcher.watch(path=filepath, flags=aionotify.Flags.MODIFY) LOGGER.debug('Awaiting setup of watcher for file {}'.format(filepath)) event_loop = asyncio.new_event_loop() asyncio.set_event_loop(event_loop) future = asyncio.ensure_future( loop(event_loop, watcher, template_system, filepath, is_yaml, var_name)) event_loop.run_until_complete(future)
async def watch(path, immediate=True): if immediate: yield readline(path) watcher = aionotify.Watcher() watcher.watch(path=path, flags=aionotify.Flags.MODIFY) await watcher.setup(asyncio.get_event_loop()) while True: await watcher.get_event() yield readline(path) watcher.close()
async def get_watchers(config_calendars): watchers = [] loop = asyncio.get_event_loop() for calendar in config_calendars.values(): path = pathlib.Path(calendar['path']) watcher = aionotify.Watcher() watcher.watch(str(path.expanduser()), flags=ALL_EVENTS) await watcher.setup(loop) logging.info(f'Watcher setup for {path}') watchers.append(watcher) return watchers
async def watch_path(loop, path): watcher = aionotify.Watcher() watcher.watch(path=path, flags=aionotify.Flags.MODIFY) # TODO: ideally need a recursive setup. watcher.watch(path=os.path.join(path, 'tests'), flags=aionotify.Flags.MODIFY) await watcher.setup(loop) try: yield watcher finally: watcher.close()
def __init__(self, loop, executor, db, files_path, watch_path, clean_interval=20): self.loop = loop self.executor = executor self.db = db self.files_path = files_path self.watch_path = watch_path self.clean_interval = clean_interval self.watcher = aionotify.Watcher() self.watcher.watch(self.files_path, FILES_FLAGS, alias="files") self.watcher.watch(self.watch_path, WATCH_FLAGS, alias="watch")
async def watch(connections, initial_files=[]): assert not docs, 'Watch already started' async def doc(filename): if filename not in docs: docs[filename] = await Document(filename, connections) return docs[filename] async def do(filename, body): d = await doc(filename) d.new_body(body) for filename in initial_files: await do(filename, open(filename, 'r').read()) watcher = aionotify.Watcher() watcher.watch(path='.', flags=aionotify.Flags.CLOSE_WRITE) loop = asyncio.get_event_loop() await watcher.setup(asyncio.get_event_loop()) while True: event = await watcher.get_event() # print('event:', event) filename = event.name if filename in initial_files: body = open(filename, 'r').read() await do(filename, body) if filename == '.requests': contents = open('.requests', 'r').read() params = dotdict() lines = contents.split('\n') for i, line in enumerate(lines): if ' ' not in line: continue k, v = line.split(' ', 1) if k == '---': body = '\n'.join(lines[i + 1:]) break params[k] = v params.body = body for k, v in params.items(): if 'cursor_' in k: params[k] = int(v) # print(pformat(params)) if params.type == 'process': await do(params.bufname, body) elif params.type in {'restart', 'complete', 'inspect'}: d = await doc(params.bufname) await d[params.type](**params) else: print('Unknown request:', pformat(params))
async def auto_reload_modules(self): watcher = aionotify.Watcher() watcher.watch(alias='default', path='modules/default', flags=aionotify.Flags.MODIFY) watcher.watch(alias='panmodules', path='modules/panmodules', flags=aionotify.Flags.MODIFY) watcher.watch(alias='kitteh', path='modules/kitteh', flags=aionotify.Flags.MODIFY) await watcher.setup(loop) while True: event = await watcher.get_event() module_name = event.alias + "." + event.name.replace(".py", "") try: self.reload_module(module_name) except Exception as e: print(e) watcher.close()
async def start(self) -> None: if not self.path or not self.path.exists(): self.abort(full_text=self.format_file_not_found.format(path=self.path)) return watcher = aionotify.Watcher() watcher.watch(str(self.path), flags=self.flags) try: loop = asyncio.get_event_loop() await watcher.setup(loop) while True: await self.run() self.event = await watcher.get_event() except Exception as e: self.exception(e)
async def monitor_devices(query): """Monitor for new devices matching a query.""" watcher = aionotify.Watcher() watcher.watch("/dev/input/by-path/", aionotify.Flags.CREATE) await watcher.setup(asyncio.get_event_loop()) while True: event = await watcher.get_event() if event.name in query: try: name = query[event.name] path = "/dev/input/by-path/" + event.name device = evdev.InputDevice(path) yield name, device except OSError: pass
async def inotify_websocket(request): print('request', request) websocket = web.WebSocketResponse() await websocket.prepare(request) watcher = aionotify.Watcher() watcher.watch(path=static_dir, flags=aionotify.Flags.CLOSE_WRITE) loop = asyncio.get_event_loop() await watcher.setup(loop) while True: event = await watcher.get_event() # print(event) await websocket.send_str(event.name) watcher.close() return websocket
async def watch(loop, path, server_ip): watcher = aionotify.Watcher() watcher.watch(path, aionotify.Flags.DELETE | aionotify.Flags.CLOSE_WRITE) await watcher.setup(loop) while True: event = await watcher.get_event() flags = aionotify.Flags.parse(event.flags) LOG.debug(f'{event.name}, {flags}') # TODO Handle directory creation and set up a new task to monitor # sub directories. Preliminary investigation suggests another call # to loop.create_event(watch(loop, path + event.name + '/') will # work. if flags == [aionotify.Flags.CLOSE_WRITE]: await send_file(loop, path + event.name, server_ip) elif flags == [aionotify.Flags.DELETE]: await send_delete_file(loop, path + event.name, server_ip)
def __init__(self, files_to_watch=files, loop=None, base='static/js/'): self.loop = loop or asyncio.get_event_loop() watcher = aionotify.Watcher() for file in files_to_watch: watcher.watch(alias='modif' + file, path=base + file, flags=aionotify.Flags.MODIFY) self.watcher = watcher self.tp = ThreadPoolExecutor(2) asyncio.ensure_future(self.run(), loop=self.loop) if not self.loop.is_running(): try: self.loop.run_forever() except KeyboardInterrupt: pass finally: self.watcher.close() self.loop.stop() self.loop.close()
async def main(loop): done = asyncio.Future() # Setup the watcher watcher = aionotify.Watcher() watcher.watch(alias='log', path='./tmp.log', flags=aionotify.Flags.MODIFY) await watcher.setup(loop) def on_completed(): print("completed") done.set_result(0) disposable = from_aiter(aiter(watcher), loop).subscribe( on_next=lambda event: print("next: {}".format(event)), on_error=lambda e: print("error: {}".format(e)), on_completed=on_completed, ) await done disposable.dispose()
async def inotify(self, watcher, root_node, path): loop = asyncio.get_event_loop() await watcher.setup(loop) while True: event = await watcher.get_event() file_path = "{}/{}".format(path, event.name) statinfo = os.lstat(file_path) type = "file" if S_ISDIR(statinfo.st_mode): type = "folder" # create node for the current file node = self.addNode( file_path, { "Name": event.name, "Type": type, "UID": statinfo.st_uid, "GID": statinfo.st_gid }) # link the file with its parent node self.addEdge(root_node.id, node.id, {"RelationType": "ownership"}) if S_ISDIR(statinfo.st_mode): # if folder start another watcher sub_watcher = aionotify.Watcher() sub_watcher.watch(alias=event.name, path=file_path, flags=aionotify.Flags.CREATE) loop.create_task(self.inotify(sub_watcher, node, file_path)) elif S_ISLNK(statinfo.st_mode): # if link add an edge between two node src = os.readlink(file_path) self.addEdge(node.id, hashlib.md5(src.encode()).hexdigest(), { "RelationType": "symlink", "Directed": True })
def __init__(self, executor, db, files_path, watch_path, clean_interval=20): self.loop = asyncio.get_event_loop() self.executor = executor self.db = db self.files_path = files_path self.watch_path = watch_path self.clean_interval = clean_interval try: self.watcher = aionotify.Watcher() except AttributeError: self.watcher = None if self.watcher: self.watcher.watch(self.files_path, FILES_FLAGS, alias="files") self.watcher.watch(self.watch_path, WATCH_FLAGS, alias="watch")
async def _watch_status(self, job_id, job_paths): """Watch the status file changes.""" status_path = job_paths['status.json'] watcher = aionotify.Watcher() watcher.watch(status_path, aionotify.Flags.CLOSE_WRITE) await watcher.setup(self.loop) try: while True: try: await self._read_status(job_id, job_paths) await watcher.get_event() self.logger.debug(f'Detected status change for job {job_id}') except concurrent.futures.CancelledError: # Break loop (likely normal exit through task cancellation) break except Exception: # pylint: disable=broad-except self.logger.exception(f'Exception while watching status of job {job_id}') finally: watcher.unwatch(status_path) watcher.close()
def onOpen(self): folder = self.factory.kwargs["folder"] # create the host node host_node = self.addNode("HOST", {"Name": "Host", "Type": "host"}) # create the root folder node root_node = self.addNode("ROOT", { "Name": folder, "Type": "root-folder" }) # link host and root folder node self.addEdge(host_node.id, root_node.id, {"RelationType": "ownership"}) # start watching the root folder watcher = aionotify.Watcher() watcher.watch(alias='root', path=folder, flags=aionotify.Flags.CREATE) loop = asyncio.get_event_loop() loop.create_task(self.inotify(watcher, root_node, folder))
async def watch(connections, initial_files=None): docs = {} async def do(filename, body): if filename not in docs: docs[filename] = await Document(filename) docs[filename].new_body(body) for filename in initial_files or []: asyncio.create_task(do(filename, open(filename, 'r').read())) watcher = aionotify.Watcher() watcher.watch(path='.', flags=aionotify.Flags.CLOSE_WRITE) loop = asyncio.get_event_loop() await watcher.setup(asyncio.get_event_loop()) while True: event = await watcher.get_event() # print('event:', event) filename = event.name if filename in (initial_files or []): body = open(filename, 'r').read() asyncio.create_task(do(filename, body)) if filename == '.requests': contents = open('.requests', 'r').read() params = dotdict() lines = contents.split('\n') for i, line in enumerate(lines): if ' ' not in line: continue k, v = line.split(' ', 1) if k == '---': body = '\n'.join(lines[i+1:]) break params[k] = v # print(pformat(params)) if params.type == 'process': asyncio.create_task(do(params.bufname, body)) else: print('Unknown request:', pformat(params))
async def monitor_and_process_resources(self): while True: with closing(aionotify.Watcher()) as watcher: self.current_watcher = watcher self.watcher_availible.set() try: try: await self.process_resources(watcher) except Exception as e: if logger.isEnabledFor(logging.DEBUG): logger.exception('Error in process_resources: ') else: logger.error('Error in process_resources: %s', e) await watcher.setup(asyncio.get_event_loop()) await watcher.get_event() except OSError as e: logger.error(e) break self.watcher_availible.clear() self.current_watcher = None logger.info('Reprocessing static resources.') await asyncio.sleep(1)
def __init__( self, index, initial_state=salobj.State.STANDBY, simulation_mode=0, ): """ Initialize DSM CSC. Parameters ---------- index : `int` Index for the DSM. This enables the control of multiple DSMs. initial_state : `lsst.ts.salobj.State`, optional State to place CSC in after initialization. simulation_mode : `int`, optional Flag to determine mode of operation. """ self.telemetry_directory = None self.telemetry_loop_task = salobj.make_done_future() self.telemetry_watcher = aionotify.Watcher() self.simulated_telemetry_ui_config_written = False self.simulated_telemetry_loop_task = salobj.make_done_future() self.simulation_loop_time = None super().__init__( "DSM", index, initial_state=initial_state, simulation_mode=simulation_mode, ) ch = logging.StreamHandler() self.log.addHandler(ch) self.finish_csc_setup()