Esempio n. 1
0
	def run(self):
		if not os.path.isfile(self._path):
			iWatchError(self._observer, "Missing target or target is not a regular file!")
		else:
			last_mtime = os.stat(self._path).st_mtime
			try:
				while True:
					mtime = os.stat(self._path).st_mtime
					if mtime > last_mtime:
						# File was modified
						last_mtime = mtime
						process_event = iProcessEvent(self)
						event = pyinotify_Event(
							{
							'event_name': 'IN_MODIFY',
							'path': self._path,
							'name': None,
							}
						)
						process_event.process_default(event)
				
					self._terminate_event.wait(1)
					if self._terminate_event.isSet():
						break
			except:
				iWatchError(self._observer, "Could not stat target!")
Esempio n. 2
0
	def run(self):
		""" Our thread's main executable """
		# Send a custom WATCH_INIT event.
		# Plugins may use this to do any "one time" initializations.
		process_event = iProcessEvent(self)
		
		process_event.process_default(pyinotify_Event(
			{
			'event_name': 'WATCH_INIT',
			'path': self._path
			}
		))
		
		self._watch_manager = WatchManager()
		self._notifier = Notifier(self._watch_manager, iProcessEvent(self))
		try:
			self._watches = self._watch_manager.add_watch(self._path, EventsCodes.ALL_EVENTS, rec=True, auto_add=True)
			for watch in self._watches.keys():
				if self._watches[watch] == -1:
					# Error: path is missing?
					iWatchError(self._observer, "Error watching %s. Maybe file or directory don't exist?" % watch)
					return
			
			# Rock'n'Roll baby!
			while True:
				self._notifier.process_events()
				if self._notifier.check_events(timeout=1000):
					self._notifier.read_events()
				# Check if our config should be updated
				if self._config_changed_event.isSet():
					self._config_changed_event.clear()
					self._reconfigure()
					# Notify plugins that a configuration might be changed.
					# They should act accordingly...
					process_event.process_default(pyinotify_Event(
						{
						'event_name': 'WATCH_RECONFIG',
						'path': self._path
						}
					))
				# Check if we have to terminate:
				if self._error_event.isSet():
					self._terminate_event.set()
				if self._terminate_event.isSet():
					self._terminate_event.clear()
					self._notifier.stop()
					break
			
			# Send a custom final event: WATCH_DEAD
			# A plugin may use this to cleanup anything 
			# left behind in the cache.
			
			process_event.process_default(pyinotify_Event(
				{
				'event_name': 'WATCH_DEAD',
				'path': self._path
				}
			))
		
		except NotifierError, data:
			self._notifier.stop()
			iWatchError(self._observer, "Error while watching %s: %s" % (self._path, data))