def on_any_event(self, event): from string import Template if self.drop_during_process and self.process and self.process.poll() is None: return if event.is_directory: object_type = 'directory' else: object_type = 'file' context = { 'watch_src_path': event.src_path, 'watch_dest_path': '', 'watch_event_type': event.event_type, 'watch_object': object_type, } if self.shell_command is None: if has_attribute(event, 'dest_path'): context.update({'dest_path': event.dest_path}) command = 'echo "${watch_event_type} ${watch_object} from ${watch_src_path} to ${watch_dest_path}"' else: command = 'echo "${watch_event_type} ${watch_object} ${watch_src_path}"' else: if has_attribute(event, 'dest_path'): context.update({'watch_dest_path': event.dest_path}) command = self.shell_command command = Template(command).safe_substitute(**context) self.process = subprocess.Popen(command, shell=True) if self.wait_for_process: self.process.wait()
def on_any_event(self, event): from string import Template if self.drop_during_process and self.process and self.process.poll( ) is None: return if event.is_directory: object_type = 'directory' else: object_type = 'file' context = { 'watch_src_path': event.src_path, 'watch_dest_path': '', 'watch_event_type': event.event_type, 'watch_object': object_type, } if self.shell_command is None: if has_attribute(event, 'dest_path'): context.update({'dest_path': event.dest_path}) command = 'echo "${watch_event_type} ${watch_object} from ${watch_src_path} to ${watch_dest_path}"' else: command = 'echo "${watch_event_type} ${watch_object} ${watch_src_path}"' else: if has_attribute(event, 'dest_path'): context.update({'watch_dest_path': event.dest_path}) command = self.shell_command command = Template(command).safe_substitute(**context) self.process = subprocess.Popen(command, shell=True) if self.wait_for_process: self.process.wait()
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return if has_attribute(event, 'src_path') and has_attribute( event, 'dest_path'): paths = [event.src_path, event.dest_path] elif has_attribute(event, 'src_path'): paths = [event.src_path] elif has_attribute(event, 'dest_path'): paths = [event.dest_path] else: paths = [] if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted, } event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return if has_attribute(event, 'src_path') and has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] elif has_attribute(event, 'src_path'): paths = [event.src_path] elif has_attribute(event, 'dest_path'): paths = [event.dest_path] else: paths = [] if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted, } event_type = event.event_type _method_map[event_type](event)
def on_any_event(self, event): from string import Template if event.is_directory: object_type = "directory" else: object_type = "file" context = { "watch_src_path": event.src_path, "watch_dest_path": "", "watch_event_type": event.event_type, "watch_object": object_type, } if self.shell_command is None: if has_attribute(event, "dest_path"): context.update({"dest_path": event.dest_path}) command = 'echo "${watch_event_type} ${watch_object} from ${watch_src_path} to ${watch_dest_path}"' else: command = 'echo "${watch_event_type} ${watch_object} ${watch_src_path}"' else: if has_attribute(event, "dest_path"): context.update({"watch_dest_path": event.dest_path}) command = self.shell_command command = Template(command).safe_substitute(**context) process = subprocess.Popen(command, shell=True) if self.wait_for_process: process.wait()
def dispatch(self, event): """Dispatch an event after filtering. We handle creation and move events only. :param event: watchdog event. :returns: None """ if event.event_type not in (EVENT_TYPE_CREATED, EVENT_TYPE_MOVED): return if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any(r.match(p) for r in self.ignore_regexes for p in paths): return if any(r.match(p) for r in self.regexes for p in paths): self._loop.call_soon_threadsafe(asyncio. async, self._process_file(event))
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any(r.match(p) for r in self.ignore_regexes for p in paths): return if any(r.match(p) for r in self.regexes for p in paths): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_ATTR_MODIFIED: self.on_attr_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted, } event_type = event.event_type _method_map[event_type](event)
def on_any_event(self, event): if self.drop_during_process and self.process and self.process.poll() is None: return if event.is_directory: object_type = 'directory' else: object_type = 'file' env = os.environ.copy() env.update({ 'WATCH_SRC_PATH': event.src_path, 'WATCH_EVENT_TYPE': event.event_type, 'WATCH_OBJECT': object_type, }) if has_attribute(event, 'dest_path'): env['WATCH_DEST_PATH'] = event.dest_path if self.shell_command is None: if 'WATCH_DEST_PATH' in env: command = 'echo "$WATCH_EVENT_TYPE $WATCH_OBJECT from $WATCH_SRC_PATH to $WATCH_DEST_PATH"' else: command = 'echo "$WATCH_EVENT_TYPE $WATCH_OBJECT $WATCH_SRC_PATH"' else: command = self.shell_command self.process = subprocess.Popen(command, shell=True, env=env) if self.wait_for_process: self.process.wait()
def assert_patterns(event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = filter_paths(paths, patterns, ignore_patterns) assert_true(filtered_paths)
def get_file_event_paths(events): for event in events: for key in ('dest_path', 'src_path'): if has_attribute(event, key): path = unicode_paths.decode(getattr(event, key)) if not (path.startswith('.autocheck.') or os.path.isdir(path)): yield os.path.relpath(path)
def dispatch(self, event): try: paths = [event.src_path] if has_attribute(event, 'dest_path'): paths.append(event.dest_path) paths = [ os.path.relpath(os.path.normpath(os.path.abspath(path)), start=self.source_base_path) for path in paths ] event_type_to_name = { EVENT_TYPE_MOVED: "move", EVENT_TYPE_CREATED: "create", EVENT_TYPE_MODIFIED: "modify", EVENT_TYPE_DELETED: "delete" } vprint("local change type: {} paths: {}", event_type_to_name[event.event_type], paths) for path in paths: pattern = find_matching_pattern(path, self.source_ignore_patterns, match_subpath=True) if pattern: vprint("ignoring change for path {}, pattern: {}", path, pattern) return self.queue.put(event) except: import traceback traceback.print_exc()
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any(r.match(p) for r in self.ignore_regexes for p in paths): return if any(r.match(p) for r in self.regexes for p in paths): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted, } event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): # There isn't any point in triggering builds on new directory creation. # It's the creation or modification of files that indicate something # meaningful enough changed for a build. if event.is_directory: return # Collect paths of interest from the event. paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) for path in paths: _LOG.debug('File event: %s', path) # Check for matching paths among the one or two in the event. matching_path = None for path in paths: if self.path_matches(path): _LOG.debug('Detected event: %s', path) matching_path = path break if matching_path: self.handle_matched_event(matching_path)
def dispatch(self, event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns): self.on_any_event(event)
def process(self, event): item_path = event.dest_path if (has_attribute( event, 'dest_path')) else event.src_path item_name = item_path.replace(self.in_path + "/", "") item_proc_path = item_path.replace(self.in_path, self.proc_path) item_out_path = item_path.replace(self.in_path, self.out_path) item_fail_path = item_path.replace(self.in_path, self.fail_path) logging.info("found item %s", item_path) if (self.proc_cmd != ""): proc_cmd = [] proc_cmd.append(self.proc_cmd) proc_cmd.append(item_name) shutil.move(item_path, item_proc_path) logging.debug("item %s moved to processing path %s", item_path, item_proc_path) try: logging.info("running command %s %s in folder %s", self.proc_cmd, item_name, self.proc_path) subprocess.run(args=proc_cmd, cwd=self.proc_path, check=True, timeout=self.proc_timeout) logging.debug("command finished") if (os.path.exists(item_proc_path)): shutil.move(item_proc_path, item_out_path) logging.info("item %s moved to output path %s", item_name, item_out_path) else: logging.error( "item %s not found in process path %s after command execution", item_name, self.proc_path) except subprocess.CalledProcessError as e: logging.error( "command %s encountered an error.\nReturn code: %s\nSTDERR: %s", e.cmd, e.returncode, e.stderr) if (os.path.exists(item_proc_path)): shutil.move(item_proc_path, item_fail_path) logging.info("item %s moved to fail path %s", item_name, item_fail_path) except subprocess.TimeoutExpired as e: logging.error("command %s timed-out after %i seconds", e.cmd, e.timeout) if (os.path.exists(item_proc_path)): shutil.move(item_proc_path, item_fail_path) logging.info("item %s moved to fail path %s", item_name, item_fail_path) except: logging.error("command %s encountered an error", e.cmd) if (os.path.exists(item_proc_path)): shutil.move(item_proc_path, item_fail_path) logging.info("item %s moved to fail path %s", item_name, item_fail_path) else: shutil.move(item_path, item_out_path) logging.info("item %s moved to output path %s", item_path, item_out_path)
def assert_patterns(event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = filter_paths(paths, included_patterns=patterns, excluded_patterns=ignore_patterns, case_sensitive=False) self.assertTrue(filtered_paths)
def assert_patterns(event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = filter_paths(paths, included_patterns=['*.py', '*.txt'], excluded_patterns=["*.pyc"], case_sensitive=False) assert filtered_paths
def assert_regexes(handler, event): if has_attribute(event, "dest_path"): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = set() for p in paths: if any(r.match(p) for r in handler.regexes): filtered_paths.add(p) self.assertTrue(filtered_paths)
def assert_patterns(event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = filter_paths(paths, included_patterns=patterns, excluded_patterns=ignore_patterns, case_sensitive=False) assert_true(filtered_paths)
def assert_regexes(handler, event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = set() for p in paths: if any(r.match(p) for r in handler.regexes): filtered_paths.add(p) self.assertTrue(filtered_paths)
def assert_patterns(event): if has_attribute(event, 'dest_path'): paths = [event.src_path, event.dest_path] else: paths = [event.src_path] filtered_paths = filter_paths( paths, included_patterns=['*.py', '*.txt'], excluded_patterns=["*.pyc"], case_sensitive=False) assert filtered_paths
def dispatch(self, event): if event.src_path and self.spec.match_file( unicode_paths.decode(event.src_path)): if self.debugging: print(f"Ignoring source change on {event.src_path}") return if has_attribute(event, "dest_path") and self.spec.match_file( unicode_paths.decode(event.dest_path)): if self.debugging: print(f"Ignoring destination change on {event.dest_path}") return super().dispatch(event)
def dispatch(self, event): """Dispatches events to the appropriate methods.""" current_time = datetime.now() if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, "dest_path"): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if match_any_paths( paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive, ): _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted, } event_type = event.event_type event_tuple = (event.src_path, event_type) if not self._coalesce: self.on_any_event(*self._args, event=event, **self._kwargs) _method_map[event_type](*self._args, event=event, **self._kwargs) elif event_tuple in self._src_path_timing: if (current_time - self._src_path_timing[event_tuple] > self._min_delta_time): # Update the time self._src_path_timing[event_tuple] = datetime.now() self.on_any_event(*self._args, event=event, **self._kwargs) _method_map[event_type](*self._args, event=event, **self._kwargs) else: self._src_path_timing[event_tuple] = datetime.now() self.on_any_event(*self._args, event=event, **self._kwargs) _method_map[event_type](*self._args, event=event, **self._kwargs)
def dispatch(self, event): if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive): self.on_any_event(event) _method_map = {EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted} event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any((r.match(p) for r in self.ignore_regexes for p in paths)): return if any((r.match(p) for r in self.regexes for p in paths)): self.on_any_event(event) _method_map = {EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted} event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): """Only dispatch if the event does not correspond to an ignored file. Args: event (watchdog.events.FileSystemEvent) """ if event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(os.path.realpath( unicode_paths.decode(event.dest_path))) if event.src_path: paths.append(os.path.realpath( unicode_paths.decode(event.src_path))) paths = [p for p in paths if not p.startswith(os.path.realpath(self.vcs.repository_dir())) and not self.vcs.path_is_ignored(p)] if len(paths) > 0: super(VcsEventHandler, self).dispatch(event)
def dispatch(self, event): try: paths = [event.src_path] if has_attribute(event, 'dest_path'): paths.append(event.dest_path) paths = [os.path.relpath(os.path.normpath(os.path.abspath(path)), start=self.source_base_path) for path in paths] event_type_to_name = {EVENT_TYPE_MOVED: "move", EVENT_TYPE_CREATED: "create", EVENT_TYPE_MODIFIED: "modify", EVENT_TYPE_DELETED: "delete"} vprint("local change type: {} paths: {}", event_type_to_name[event.event_type], paths) for path in paths: pattern = find_matching_pattern(path, self.source_ignore_patterns, match_subpath=True) if pattern: vprint("ignoring change for path {}, pattern: {}", path, pattern) return self.queue.put(event) except: import traceback traceback.print_exc()
def dispatch(self, event): if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any((r.match(p) for r in self.ignore_regexes for p in paths)): return if any((r.match(p) for r in self.regexes for p in paths)): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted } event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive): self.on_any_event(event) _method_map = { EVENT_TYPE_MODIFIED: self.on_modified, EVENT_TYPE_MOVED: self.on_moved, EVENT_TYPE_CREATED: self.on_created, EVENT_TYPE_DELETED: self.on_deleted } event_type = event.event_type _method_map[event_type](event)
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if match_any_paths(paths, included_patterns=self.patterns, excluded_patterns=self.ignore_patterns, case_sensitive=self.case_sensitive): super(PatternMatchingEventHandler, self).dispatch(event)
def dispatch(self, event): """Dispatches events to the appropriate methods. :param event: The event object representing the file system event. :type event: :class:`FileSystemEvent` """ if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any(r.match(p) for r in self.ignore_regexes for p in paths): return if any(r.match(p) for r in self.regexes for p in paths): super(RegexMatchingEventHandler, self).dispatch(event)
def organize(self, event): if has_attribute(event, 'dest_path'): file_path = event.dest_path else: file_path = event.src_path what = 'directory' if event.is_directory else 'file' if what == "file": self.filename = file_path.split("\\")[-1] assigned_folder = self.assign_folder(what) if assigned_folder is None: return elif what == "directory": self.filename = file_path.split("\\")[-1] assigned_folder = self.assign_folder(what) try: os.rename(file_path, os.path.join(assigned_folder, self.filename)) except FileExistsError as e: logging.exception(e) logging.error("File already exists. Renaming.") os.rename( file_path, os.path.join( assigned_folder, "_".join([ self.filename, datetime.datetime.now().strftime("%d_%m_%Y-%H_%M_%S") ]))) except (NotImplementedError, FileNotFoundError) as e: logging.exception(e) logging.debug(f"Creating directory - {assigned_folder}...") os.mkdir(assigned_folder) os.rename(file_path, os.path.join(assigned_folder, self.filename)) except Exception as e: logging.exception(e)
def dispatch(self, event): """Dispatch an event after filtering. We handle creation and move events only. :param event: watchdog event. :returns: None """ if event.event_type not in (EVENT_TYPE_CREATED, EVENT_TYPE_MOVED): return if self.ignore_directories and event.is_directory: return paths = [] if has_attribute(event, 'dest_path'): paths.append(unicode_paths.decode(event.dest_path)) if event.src_path: paths.append(unicode_paths.decode(event.src_path)) if any(r.match(p) for r in self.ignore_regexes for p in paths): return if any(r.match(p) for r in self.regexes for p in paths): self._loop.call_soon_threadsafe(asyncio.async, self._process_file(event))
DirModifiedEvent,\ DirMovedEvent,\ DirCreatedEvent,\ FileDeletedEvent,\ FileModifiedEvent,\ FileMovedEvent,\ FileCreatedEvent,\ EVENT_TYPE_MODIFIED,\ EVENT_TYPE_CREATED,\ EVENT_TYPE_DELETED,\ EVENT_TYPE_MOVED libc_string = ctypes_find_library('c', 'libc.so.6') libc = ctypes.CDLL(libc_string, use_errno=True) if (not has_attribute(libc, 'inotify_init') or not has_attribute(libc, 'inotify_add_watch') or not has_attribute(libc, 'inotify_rm_watch')): raise ImportError("Unsupported libc version found: %s" % libc_string) # #include <sys/inotify.h> # char *strerror(int errnum); #strerror = ctypes.CFUNCTYPE(c_char_p, c_int)( # ("strerror", libc)) # #include <sys/inotify.h> # int inotify_init(void); inotify_init = ctypes.CFUNCTYPE(c_int, use_errno=True)( ("inotify_init", libc)) # #include <sys/inotify.h>
from __future__ import with_statement import os import errno import struct import threading import ctypes from functools import reduce from ctypes import c_int, c_char_p, c_uint32 from watchdog.utils import has_attribute, ctypes_find_library libc_string = ctypes_find_library("c", "libc.so") libc = ctypes.CDLL(libc_string, use_errno=True) if ( not has_attribute(libc, "inotify_init") or not has_attribute(libc, "inotify_add_watch") or not has_attribute(libc, "inotify_rm_watch") ): raise ImportError("Unsupported libc version found: %s" % libc_string) inotify_add_watch = ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)(("inotify_add_watch", libc)) inotify_rm_watch = ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)(("inotify_rm_watch", libc)) inotify_init = ctypes.CFUNCTYPE(c_int, use_errno=True)(("inotify_init", libc)) try: inotify_init1 = ctypes.CFUNCTYPE(c_int, c_int, use_errno=True)(("inotify_init1", libc)) except AttributeError:
# errors, e.g.on android IOError "No usable temporary directory found" # will be raised. pass if libc_path is not None: return ctypes.CDLL(libc_path) # Fallbacks try: return ctypes.CDLL('libc.so') except (OSError, IOError): return ctypes.CDLL('libc.so.6') libc = _load_libc() if not has_attribute(libc, 'inotify_init') or \ not has_attribute(libc, 'inotify_add_watch') or \ not has_attribute(libc, 'inotify_rm_watch'): raise UnsupportedLibc("Unsupported libc version found: %s" % libc._name) inotify_add_watch = ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)( ("inotify_add_watch", libc)) inotify_rm_watch = ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)( ("inotify_rm_watch", libc)) inotify_init = ctypes.CFUNCTYPE(c_int, use_errno=True)( ("inotify_init", libc)) class InotifyConstants(object):
try: return ctypes.CDLL('libc.so.6') except (OSError, IOError): pass # uClibc try: return ctypes.CDLL('libc.so.0') except (OSError, IOError) as err: raise err libc = _load_libc() if not has_attribute(libc, 'inotify_init') or \ not has_attribute(libc, 'inotify_add_watch') or \ not has_attribute(libc, 'inotify_rm_watch'): raise UnsupportedLibc("Unsupported libc version found: %s" % libc._name) inotify_add_watch = ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)( ("inotify_add_watch", libc)) inotify_rm_watch = ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)( ("inotify_rm_watch", libc)) inotify_init = ctypes.CFUNCTYPE(c_int, use_errno=True)( ("inotify_init", libc)) class InotifyConstants(object):
# limitations under the License. from __future__ import with_statement import os import errno import struct import threading import ctypes from functools import reduce from ctypes import c_int, c_char_p, c_uint32 from watchdog.utils import has_attribute, ctypes_find_library libc_string = ctypes_find_library('c', 'libc.so') libc = ctypes.CDLL(libc_string, use_errno=True) if (not has_attribute(libc, 'inotify_init') or not has_attribute(libc, 'inotify_add_watch') or not has_attribute(libc, 'inotify_rm_watch')): raise ImportError("Unsupported libc version found: %s" % libc_string) inotify_add_watch = ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)( ("inotify_add_watch", libc)) inotify_rm_watch = ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)(("inotify_rm_watch", libc)) inotify_init = ctypes.CFUNCTYPE(c_int, use_errno=True)(("inotify_init", libc))