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