Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

374 строки
15 KiB

  1. import logging
  2. import os
  3. import sys
  4. import warnings
  5. from enum import IntEnum
  6. from pathlib import Path
  7. from typing import TYPE_CHECKING, AsyncGenerator, Callable, Generator, Optional, Set, Tuple, Union
  8. import anyio
  9. from ._rust_notify import RustNotify
  10. from .filters import DefaultFilter
  11. __all__ = 'watch', 'awatch', 'Change', 'FileChange'
  12. logger = logging.getLogger('watchfiles.main')
  13. class Change(IntEnum):
  14. """
  15. Enum representing the type of change that occurred.
  16. """
  17. added = 1
  18. """A new file or directory was added."""
  19. modified = 2
  20. """A file or directory was modified, can be either a metadata or data change."""
  21. deleted = 3
  22. """A file or directory was deleted."""
  23. def raw_str(self) -> str:
  24. return self.name
  25. FileChange = Tuple[Change, str]
  26. """
  27. A tuple representing a file change, first element is a [`Change`][watchfiles.Change] member, second is the path
  28. of the file or directory that changed.
  29. """
  30. if TYPE_CHECKING:
  31. import asyncio
  32. from typing import Protocol
  33. import trio
  34. AnyEvent = Union[anyio.Event, asyncio.Event, trio.Event]
  35. class AbstractEvent(Protocol):
  36. def is_set(self) -> bool: ...
  37. def watch(
  38. *paths: Union[Path, str],
  39. watch_filter: Optional[Callable[['Change', str], bool]] = DefaultFilter(),
  40. debounce: int = 1_600,
  41. step: int = 50,
  42. stop_event: Optional['AbstractEvent'] = None,
  43. rust_timeout: int = 5_000,
  44. yield_on_timeout: bool = False,
  45. debug: Optional[bool] = None,
  46. raise_interrupt: bool = True,
  47. force_polling: Optional[bool] = None,
  48. poll_delay_ms: int = 300,
  49. recursive: bool = True,
  50. ignore_permission_denied: Optional[bool] = None,
  51. ) -> Generator[Set[FileChange], None, None]:
  52. """
  53. Watch one or more paths and yield a set of changes whenever files change.
  54. The paths watched can be directories or files, directories are watched recursively - changes in subdirectories
  55. are also detected.
  56. #### Force polling
  57. Notify will fall back to file polling if it can't use file system notifications, but we also force Notify
  58. to use polling if the `force_polling` argument is `True`; if `force_polling` is unset (or `None`), we enable
  59. force polling thus:
  60. * if the `WATCHFILES_FORCE_POLLING` environment variable exists and is not empty:
  61. * if the value is `false`, `disable` or `disabled`, force polling is disabled
  62. * otherwise, force polling is enabled
  63. * otherwise, we enable force polling only if we detect we're running on WSL (Windows Subsystem for Linux)
  64. It is also possible to change the poll delay between iterations, it can be changed to maintain a good response time
  65. and an appropiate CPU consumption using the `poll_delay_ms` argument, we change poll delay thus:
  66. * if file polling is enabled and the `WATCHFILES_POLL_DELAY_MS` env var exists and it is numeric, we use that
  67. * otherwise, we use the argument value
  68. Args:
  69. *paths: filesystem paths to watch.
  70. watch_filter: callable used to filter out changes which are not important, you can either use a raw callable
  71. or a [`BaseFilter`][watchfiles.BaseFilter] instance,
  72. defaults to an instance of [`DefaultFilter`][watchfiles.DefaultFilter]. To keep all changes, use `None`.
  73. debounce: maximum time in milliseconds to group changes over before yielding them.
  74. step: time to wait for new changes in milliseconds, if no changes are detected in this time, and
  75. at least one change has been detected, the changes are yielded.
  76. stop_event: event to stop watching, if this is set, the generator will stop iteration,
  77. this can be anything with an `is_set()` method which returns a bool, e.g. `threading.Event()`.
  78. rust_timeout: maximum time in milliseconds to wait in the rust code for changes, `0` means no timeout.
  79. yield_on_timeout: if `True`, the generator will yield upon timeout in rust even if no changes are detected.
  80. debug: whether to print information about all filesystem changes in rust to stdout, if `None` will use the
  81. `WATCHFILES_DEBUG` environment variable.
  82. raise_interrupt: whether to re-raise `KeyboardInterrupt`s, or suppress the error and just stop iterating.
  83. force_polling: See [Force polling](#force-polling) above.
  84. poll_delay_ms: delay between polling for changes, only used if `force_polling=True`.
  85. recursive: if `True`, watch for changes in sub-directories recursively, otherwise watch only for changes in the
  86. top-level directory, default is `True`.
  87. ignore_permission_denied: if `True`, will ignore permission denied errors, otherwise will raise them by default.
  88. Setting the `WATCHFILES_IGNORE_PERMISSION_DENIED` environment variable will set this value too.
  89. Yields:
  90. The generator yields sets of [`FileChange`][watchfiles.main.FileChange]s.
  91. ```py title="Example of watch usage"
  92. from watchfiles import watch
  93. for changes in watch('./first/dir', './second/dir', raise_interrupt=False):
  94. print(changes)
  95. ```
  96. """
  97. force_polling = _default_force_polling(force_polling)
  98. poll_delay_ms = _default_poll_delay_ms(poll_delay_ms)
  99. ignore_permission_denied = _default_ignore_permission_denied(ignore_permission_denied)
  100. debug = _default_debug(debug)
  101. with RustNotify(
  102. [str(p) for p in paths], debug, force_polling, poll_delay_ms, recursive, ignore_permission_denied
  103. ) as watcher:
  104. while True:
  105. raw_changes = watcher.watch(debounce, step, rust_timeout, stop_event)
  106. if raw_changes == 'timeout':
  107. if yield_on_timeout:
  108. yield set()
  109. else:
  110. logger.debug('rust notify timeout, continuing')
  111. elif raw_changes == 'signal':
  112. if raise_interrupt:
  113. raise KeyboardInterrupt
  114. else:
  115. logger.warning('KeyboardInterrupt caught, stopping watch')
  116. return
  117. elif raw_changes == 'stop':
  118. return
  119. else:
  120. changes = _prep_changes(raw_changes, watch_filter)
  121. if changes:
  122. _log_changes(changes)
  123. yield changes
  124. else:
  125. logger.debug('all changes filtered out, raw_changes=%s', raw_changes)
  126. async def awatch( # C901
  127. *paths: Union[Path, str],
  128. watch_filter: Optional[Callable[[Change, str], bool]] = DefaultFilter(),
  129. debounce: int = 1_600,
  130. step: int = 50,
  131. stop_event: Optional['AnyEvent'] = None,
  132. rust_timeout: Optional[int] = None,
  133. yield_on_timeout: bool = False,
  134. debug: Optional[bool] = None,
  135. raise_interrupt: Optional[bool] = None,
  136. force_polling: Optional[bool] = None,
  137. poll_delay_ms: int = 300,
  138. recursive: bool = True,
  139. ignore_permission_denied: Optional[bool] = None,
  140. ) -> AsyncGenerator[Set[FileChange], None]:
  141. """
  142. Asynchronous equivalent of [`watch`][watchfiles.watch] using threads to wait for changes.
  143. Arguments match those of [`watch`][watchfiles.watch] except `stop_event`.
  144. All async methods use [anyio](https://anyio.readthedocs.io/en/latest/) to run the event loop.
  145. Unlike [`watch`][watchfiles.watch] `KeyboardInterrupt` cannot be suppressed by `awatch` so they need to be caught
  146. where `asyncio.run` or equivalent is called.
  147. Args:
  148. *paths: filesystem paths to watch.
  149. watch_filter: matches the same argument of [`watch`][watchfiles.watch].
  150. debounce: matches the same argument of [`watch`][watchfiles.watch].
  151. step: matches the same argument of [`watch`][watchfiles.watch].
  152. stop_event: `anyio.Event` which can be used to stop iteration, see example below.
  153. rust_timeout: matches the same argument of [`watch`][watchfiles.watch], except that `None` means
  154. use `1_000` on Windows and `5_000` on other platforms thus helping with exiting on `Ctrl+C` on Windows,
  155. see [#110](https://github.com/samuelcolvin/watchfiles/issues/110).
  156. yield_on_timeout: matches the same argument of [`watch`][watchfiles.watch].
  157. debug: matches the same argument of [`watch`][watchfiles.watch].
  158. raise_interrupt: This is deprecated, `KeyboardInterrupt` will cause this coroutine to be cancelled and then
  159. be raised by the top level `asyncio.run` call or equivalent, and should be caught there.
  160. See [#136](https://github.com/samuelcolvin/watchfiles/issues/136)
  161. force_polling: if true, always use polling instead of file system notifications, default is `None` where
  162. `force_polling` is set to `True` if the `WATCHFILES_FORCE_POLLING` environment variable exists.
  163. poll_delay_ms: delay between polling for changes, only used if `force_polling=True`.
  164. `poll_delay_ms` can be changed via the `WATCHFILES_POLL_DELAY_MS` environment variable.
  165. recursive: if `True`, watch for changes in sub-directories recursively, otherwise watch only for changes in the
  166. top-level directory, default is `True`.
  167. ignore_permission_denied: if `True`, will ignore permission denied errors, otherwise will raise them by default.
  168. Setting the `WATCHFILES_IGNORE_PERMISSION_DENIED` environment variable will set this value too.
  169. Yields:
  170. The generator yields sets of [`FileChange`][watchfiles.main.FileChange]s.
  171. ```py title="Example of awatch usage"
  172. import asyncio
  173. from watchfiles import awatch
  174. async def main():
  175. async for changes in awatch('./first/dir', './second/dir'):
  176. print(changes)
  177. if __name__ == '__main__':
  178. try:
  179. asyncio.run(main())
  180. except KeyboardInterrupt:
  181. print('stopped via KeyboardInterrupt')
  182. ```
  183. ```py title="Example of awatch usage with a stop event"
  184. import asyncio
  185. from watchfiles import awatch
  186. async def main():
  187. stop_event = asyncio.Event()
  188. async def stop_soon():
  189. await asyncio.sleep(3)
  190. stop_event.set()
  191. stop_soon_task = asyncio.create_task(stop_soon())
  192. async for changes in awatch('/path/to/dir', stop_event=stop_event):
  193. print(changes)
  194. # cleanup by awaiting the (now complete) stop_soon_task
  195. await stop_soon_task
  196. asyncio.run(main())
  197. ```
  198. """
  199. if raise_interrupt is not None:
  200. warnings.warn(
  201. 'raise_interrupt is deprecated, KeyboardInterrupt will cause this coroutine to be cancelled and then '
  202. 'be raised by the top level asyncio.run call or equivalent, and should be caught there. See #136.',
  203. DeprecationWarning,
  204. )
  205. if stop_event is None:
  206. stop_event_: AnyEvent = anyio.Event()
  207. else:
  208. stop_event_ = stop_event
  209. force_polling = _default_force_polling(force_polling)
  210. poll_delay_ms = _default_poll_delay_ms(poll_delay_ms)
  211. ignore_permission_denied = _default_ignore_permission_denied(ignore_permission_denied)
  212. debug = _default_debug(debug)
  213. with RustNotify(
  214. [str(p) for p in paths], debug, force_polling, poll_delay_ms, recursive, ignore_permission_denied
  215. ) as watcher:
  216. timeout = _calc_async_timeout(rust_timeout)
  217. CancelledError = anyio.get_cancelled_exc_class()
  218. while True:
  219. async with anyio.create_task_group() as tg:
  220. try:
  221. raw_changes = await anyio.to_thread.run_sync(watcher.watch, debounce, step, timeout, stop_event_)
  222. except (CancelledError, KeyboardInterrupt):
  223. stop_event_.set()
  224. # suppressing KeyboardInterrupt wouldn't stop it getting raised by the top level asyncio.run call
  225. raise
  226. tg.cancel_scope.cancel()
  227. if raw_changes == 'timeout':
  228. if yield_on_timeout:
  229. yield set()
  230. else:
  231. logger.debug('rust notify timeout, continuing')
  232. elif raw_changes == 'stop':
  233. return
  234. elif raw_changes == 'signal':
  235. # in theory the watch thread should never get a signal
  236. raise RuntimeError('watch thread unexpectedly received a signal')
  237. else:
  238. changes = _prep_changes(raw_changes, watch_filter)
  239. if changes:
  240. _log_changes(changes)
  241. yield changes
  242. else:
  243. logger.debug('all changes filtered out, raw_changes=%s', raw_changes)
  244. def _prep_changes(
  245. raw_changes: Set[Tuple[int, str]], watch_filter: Optional[Callable[[Change, str], bool]]
  246. ) -> Set[FileChange]:
  247. # if we wanted to be really snazzy, we could move this into rust
  248. changes = {(Change(change), path) for change, path in raw_changes}
  249. if watch_filter:
  250. changes = {c for c in changes if watch_filter(c[0], c[1])}
  251. return changes
  252. def _log_changes(changes: Set[FileChange]) -> None:
  253. if logger.isEnabledFor(logging.INFO): # pragma: no branch
  254. count = len(changes)
  255. plural = '' if count == 1 else 's'
  256. if logger.isEnabledFor(logging.DEBUG):
  257. logger.debug('%d change%s detected: %s', count, plural, changes)
  258. else:
  259. logger.info('%d change%s detected', count, plural)
  260. def _calc_async_timeout(timeout: Optional[int]) -> int:
  261. """
  262. see https://github.com/samuelcolvin/watchfiles/issues/110
  263. """
  264. if timeout is None:
  265. if sys.platform == 'win32':
  266. return 1_000
  267. else:
  268. return 5_000
  269. else:
  270. return timeout
  271. def _default_force_polling(force_polling: Optional[bool]) -> bool:
  272. """
  273. See docstring for `watch` above for details.
  274. See samuelcolvin/watchfiles#167 and samuelcolvin/watchfiles#187 for discussion and rationale.
  275. """
  276. if force_polling is not None:
  277. return force_polling
  278. env_var = os.getenv('WATCHFILES_FORCE_POLLING')
  279. if env_var:
  280. return env_var.lower() not in {'false', 'disable', 'disabled'}
  281. else:
  282. return _auto_force_polling()
  283. def _default_poll_delay_ms(poll_delay_ms: int) -> int:
  284. """
  285. See docstring for `watch` above for details.
  286. """
  287. env_var = os.getenv('WATCHFILES_POLL_DELAY_MS')
  288. if env_var and env_var.isdecimal():
  289. return int(env_var)
  290. else:
  291. return poll_delay_ms
  292. def _default_debug(debug: Optional[bool]) -> bool:
  293. if debug is not None:
  294. return debug
  295. env_var = os.getenv('WATCHFILES_DEBUG')
  296. return bool(env_var)
  297. def _auto_force_polling() -> bool:
  298. """
  299. Whether to auto-enable force polling, it should be enabled automatically only on WSL.
  300. See samuelcolvin/watchfiles#187 for discussion.
  301. """
  302. import platform
  303. uname = platform.uname()
  304. return 'microsoft-standard' in uname.release.lower() and uname.system.lower() == 'linux'
  305. def _default_ignore_permission_denied(ignore_permission_denied: Optional[bool]) -> bool:
  306. if ignore_permission_denied is not None:
  307. return ignore_permission_denied
  308. env_var = os.getenv('WATCHFILES_IGNORE_PERMISSION_DENIED')
  309. return bool(env_var)