##// END OF EJS Templates
fsmonitor: fix exception message scraping...
zphricz -
r30649:b0a0f7b9 default
parent child Browse files
Show More
@@ -1,109 +1,109 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 util
13 13
14 14 from . import pywatchman
15 15
16 16 class Unavailable(Exception):
17 17 def __init__(self, msg, warn=True, invalidate=False):
18 18 self.msg = msg
19 19 self.warn = warn
20 20 if self.msg == 'timed out waiting for response':
21 21 self.warn = False
22 22 self.invalidate = invalidate
23 23
24 24 def __str__(self):
25 25 if self.warn:
26 26 return 'warning: Watchman unavailable: %s' % self.msg
27 27 else:
28 28 return 'Watchman unavailable: %s' % self.msg
29 29
30 30 class WatchmanNoRoot(Unavailable):
31 31 def __init__(self, root, msg):
32 32 self.root = root
33 33 super(WatchmanNoRoot, self).__init__(msg)
34 34
35 35 class client(object):
36 36 def __init__(self, repo, timeout=1.0):
37 37 err = None
38 38 if not self._user:
39 39 err = "couldn't get user"
40 40 warn = True
41 41 if self._user in repo.ui.configlist('fsmonitor', 'blacklistusers'):
42 42 err = 'user %s in blacklist' % self._user
43 43 warn = False
44 44
45 45 if err:
46 46 raise Unavailable(err, warn)
47 47
48 48 self._timeout = timeout
49 49 self._watchmanclient = None
50 50 self._root = repo.root
51 51 self._ui = repo.ui
52 52 self._firsttime = True
53 53
54 54 def settimeout(self, timeout):
55 55 self._timeout = timeout
56 56 if self._watchmanclient is not None:
57 57 self._watchmanclient.setTimeout(timeout)
58 58
59 59 def getcurrentclock(self):
60 60 result = self.command('clock')
61 61 if not util.safehasattr(result, 'clock'):
62 62 raise Unavailable('clock result is missing clock value',
63 63 invalidate=True)
64 64 return result.clock
65 65
66 66 def clearconnection(self):
67 67 self._watchmanclient = None
68 68
69 69 def available(self):
70 70 return self._watchmanclient is not None or self._firsttime
71 71
72 72 @util.propertycache
73 73 def _user(self):
74 74 try:
75 75 return getpass.getuser()
76 76 except KeyError:
77 77 # couldn't figure out our user
78 78 return None
79 79
80 80 def _command(self, *args):
81 81 watchmanargs = (args[0], self._root) + args[1:]
82 82 try:
83 83 if self._watchmanclient is None:
84 84 self._firsttime = False
85 85 self._watchmanclient = pywatchman.client(
86 86 timeout=self._timeout,
87 87 useImmutableBser=True)
88 88 return self._watchmanclient.query(*watchmanargs)
89 89 except pywatchman.CommandError as ex:
90 if ex.msg.startswith('unable to resolve root'):
90 if 'unable to resolve root' in ex.msg:
91 91 raise WatchmanNoRoot(self._root, ex.msg)
92 92 raise Unavailable(ex.msg)
93 93 except pywatchman.WatchmanError as ex:
94 94 raise Unavailable(str(ex))
95 95
96 96 def command(self, *args):
97 97 try:
98 98 try:
99 99 return self._command(*args)
100 100 except WatchmanNoRoot:
101 101 # this 'watch' command can also raise a WatchmanNoRoot if
102 102 # watchman refuses to accept this root
103 103 self._command('watch')
104 104 return self._command(*args)
105 105 except Unavailable:
106 106 # this is in an outer scope to catch Unavailable form any of the
107 107 # above _command calls
108 108 self._watchmanclient = None
109 109 raise
General Comments 0
You need to be logged in to leave comments. Login now