summaryrefslogtreecommitdiffstats
path: root/powerline/lib/watcher/uv.py
blob: 272db0ff8e66fb3dc7e24bc67dd1ae58aa4bddac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import os

from collections import defaultdict
from threading import RLock
from functools import partial
from threading import Thread
from errno import ENOENT

from powerline.lib.path import realpath
from powerline.lib.encoding import get_preferred_file_name_encoding


class UvNotFound(NotImplementedError):
	pass


pyuv = None
pyuv_version_info = None


def import_pyuv():
	global pyuv
	global pyuv_version_info
	if not pyuv:
		try:
			pyuv = __import__('pyuv')
		except ImportError:
			raise UvNotFound
		else:
			pyuv_version_info = tuple((int(c) for c in pyuv.__version__.split('.')))


class UvThread(Thread):
	daemon = True

	def __init__(self, loop):
		self.uv_loop = loop
		self.async_handle = pyuv.Async(loop, self._async_cb)
		super(UvThread, self).__init__()

	def _async_cb(self, handle):
		self.uv_loop.stop()
		self.async_handle.close()

	def run(self):
		self.uv_loop.run()

	def join(self):
		self.async_handle.send()
		return super(UvThread, self).join()


_uv_thread = None


def start_uv_thread():
	global _uv_thread
	if _uv_thread is None:
		loop = pyuv.Loop()
		_uv_thread = UvThread(loop)
		_uv_thread.start()
	return _uv_thread.uv_loop


def normpath(path, fenc):
	path = realpath(path)
	if isinstance(path, bytes):
		return path.decode(fenc)
	else:
		return path


class UvWatcher(object):
	def __init__(self):
		import_pyuv()
		self.watches = {}
		self.lock = RLock()
		self.loop = start_uv_thread()
		self.fenc = get_preferred_file_name_encoding()
		if pyuv_version_info >= (1, 0):
			self._start_watch = self._start_watch_1_x
		else:
			self._start_watch = self._start_watch_0_x

	def _start_watch_1_x(self, path):
		handle = pyuv.fs.FSEvent(self.loop)
		handle.start(path, 0, partial(self._record_event, path))
		self.watches[path] = handle

	def _start_watch_0_x(self, path):
		self.watches[path] = pyuv.fs.FSEvent(
			self.loop,
			path,
			partial(self._record_event, path),
			pyuv.fs.UV_CHANGE | pyuv.fs.UV_RENAME
		)

	def watch(self, path):
		path = normpath(path, self.fenc)
		with self.lock:
			if path not in self.watches:
				try:
					self._start_watch(path)
				except pyuv.error.FSEventError as e:
					code = e.args[0]
					if code == pyuv.errno.UV_ENOENT:
						raise OSError(ENOENT, 'No such file or directory: ' + path)
					else:
						raise

	def unwatch(self, path):
		path = normpath(path, self.fenc)
		with self.lock:
			try:
				watch = self.watches.pop(path)
			except KeyError:
				return
		watch.close(partial(self._stopped_watching, path))

	def is_watching(self, path):
		with self.lock:
			return normpath(path, self.fenc) in self.watches

	def __del__(self):
		try:
			lock = self.lock
		except AttributeError:
			pass
		else:
			with lock:
				while self.watches:
					path, watch = self.watches.popitem()
					watch.close(partial(self._stopped_watching, path))


class UvFileWatcher(UvWatcher):
	def __init__(self):
		super(UvFileWatcher, self).__init__()
		self.events = defaultdict(list)

	def _record_event(self, path, fsevent_handle, filename, events, error):
		with self.lock:
			self.events[path].append(events)
			if events | pyuv.fs.UV_RENAME:
				if not os.path.exists(path):
					self.watches.pop(path).close()

	def _stopped_watching(self, path, *args):
		self.events.pop(path, None)

	def __call__(self, path):
		path = normpath(path, self.fenc)
		with self.lock:
			events = self.events.pop(path, None)
		if events:
			return True
		if path not in self.watches:
			self.watch(path)
			return True
		return False


class UvTreeWatcher(UvWatcher):
	is_dummy = False

	def __init__(self, basedir, ignore_event=None):
		super(UvTreeWatcher, self).__init__()
		self.ignore_event = ignore_event or (lambda path, name: False)
		self.basedir = normpath(basedir, self.fenc)
		self.modified = True
		self.watch_directory(self.basedir)

	def watch_directory(self, path):
		for root, dirs, files in os.walk(normpath(path, self.fenc)):
			self.watch_one_directory(root)

	def watch_one_directory(self, dirname):
		try:
			self.watch(dirname)
		except OSError:
			pass

	def _stopped_watching(self, path, *args):
		pass

	def _record_event(self, path, fsevent_handle, filename, events, error):
		if not self.ignore_event(path, filename):
			self.modified = True
			if events == pyuv.fs.UV_CHANGE | pyuv.fs.UV_RENAME:
				# Stat changes to watched directory are UV_CHANGE|UV_RENAME. It 
				# is weird.
				pass
			elif events | pyuv.fs.UV_RENAME:
				if not os.path.isdir(path):
					self.unwatch(path)
				else:
					full_name = os.path.join(path, filename)
					if os.path.isdir(full_name):
						# For some reason mkdir and rmdir both fall into this 
						# category
						self.watch_directory(full_name)

	def __call__(self):
		return self.__dict__.pop('modified', False)