| 1 |
# watchmanclient.py - Watchman client for the fsmonitor extension |
| 2 |
# |
| 3 |
# Copyright 2013-2016 Facebook, Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import getpass |
| 11 |
|
| 12 |
from mercurial import ( |
| 13 |
encoding, |
| 14 |
util, |
| 15 |
) |
| 16 |
from mercurial.utils import ( |
| 17 |
procutil, |
| 18 |
stringutil, |
| 19 |
) |
| 20 |
|
| 21 |
from . import pywatchman |
| 22 |
|
| 23 |
|
| 24 |
class Unavailable(Exception): |
| 25 |
def __init__(self, msg, warn=True, invalidate=False): |
| 26 |
self.msg = msg |
| 27 |
self.warn = warn |
| 28 |
if self.msg == b'timed out waiting for response': |
| 29 |
self.warn = False |
| 30 |
self.invalidate = invalidate |
| 31 |
|
| 32 |
def __bytes__(self): |
| 33 |
if self.warn: |
| 34 |
return b'warning: Watchman unavailable: %s' % self.msg |
| 35 |
else: |
| 36 |
return b'Watchman unavailable: %s' % self.msg |
| 37 |
|
| 38 |
__str__ = encoding.strmethod(__bytes__) |
| 39 |
|
| 40 |
|
| 41 |
class WatchmanNoRoot(Unavailable): |
| 42 |
def __init__(self, root, msg): |
| 43 |
self.root = root |
| 44 |
super().__init__(msg) |
| 45 |
|
| 46 |
|
| 47 |
class client: |
| 48 |
def __init__(self, ui, root, timeout=1.0): |
| 49 |
err = None |
| 50 |
if not self._user: |
| 51 |
err = b"couldn't get user" |
| 52 |
warn = True |
| 53 |
if self._user in ui.configlist(b'fsmonitor', b'blacklistusers'): |
| 54 |
err = b'user %s in blacklist' % self._user |
| 55 |
warn = False |
| 56 |
|
| 57 |
if err: |
| 58 |
raise Unavailable(err, warn) |
| 59 |
|
| 60 |
self._timeout = timeout |
| 61 |
self._watchmanclient = None |
| 62 |
self._root = root |
| 63 |
self._ui = ui |
| 64 |
self._firsttime = True |
| 65 |
|
| 66 |
def settimeout(self, timeout): |
| 67 |
self._timeout = timeout |
| 68 |
if self._watchmanclient is not None: |
| 69 |
self._watchmanclient.setTimeout(timeout) |
| 70 |
|
| 71 |
def getcurrentclock(self): |
| 72 |
result = self.command(b'clock') |
| 73 |
if not hasattr(result, 'clock'): |
| 74 |
raise Unavailable( |
| 75 |
b'clock result is missing clock value', invalidate=True |
| 76 |
) |
| 77 |
return result.clock |
| 78 |
|
| 79 |
def clearconnection(self): |
| 80 |
self._watchmanclient = None |
| 81 |
|
| 82 |
def available(self): |
| 83 |
return self._watchmanclient is not None or self._firsttime |
| 84 |
|
| 85 |
@util.propertycache |
| 86 |
def _user(self): |
| 87 |
try: |
| 88 |
return getpass.getuser() |
| 89 |
except KeyError: |
| 90 |
# couldn't figure out our user |
| 91 |
return None |
| 92 |
|
| 93 |
def _command(self, *args): |
| 94 |
watchmanargs = (args[0], self._root) + args[1:] |
| 95 |
try: |
| 96 |
if self._watchmanclient is None: |
| 97 |
self._firsttime = False |
| 98 |
watchman_exe = self._ui.configpath( |
| 99 |
b'fsmonitor', b'watchman_exe' |
| 100 |
) |
| 101 |
self._watchmanclient = pywatchman.client( |
| 102 |
timeout=self._timeout, |
| 103 |
useImmutableBser=True, |
| 104 |
binpath=procutil.tonativestr(watchman_exe), |
| 105 |
) |
| 106 |
return self._watchmanclient.query(*watchmanargs) |
| 107 |
except pywatchman.CommandError as ex: |
| 108 |
if 'unable to resolve root' in ex.msg: |
| 109 |
raise WatchmanNoRoot( |
| 110 |
self._root, stringutil.forcebytestr(ex.msg) |
| 111 |
) |
| 112 |
raise Unavailable(stringutil.forcebytestr(ex.msg)) |
| 113 |
except pywatchman.WatchmanError as ex: |
| 114 |
raise Unavailable(stringutil.forcebytestr(ex)) |
| 115 |
|
| 116 |
def command(self, *args): |
| 117 |
try: |
| 118 |
try: |
| 119 |
return self._command(*args) |
| 120 |
except WatchmanNoRoot: |
| 121 |
# this 'watch' command can also raise a WatchmanNoRoot if |
| 122 |
# watchman refuses to accept this root |
| 123 |
self._command(b'watch') |
| 124 |
return self._command(*args) |
| 125 |
except Unavailable: |
| 126 |
# this is in an outer scope to catch Unavailable form any of the |
| 127 |
# above _command calls |
| 128 |
self._watchmanclient = None |
| 129 |
raise |