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