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