Mercurial > hg
annotate hgext/fsmonitor/watchmanclient.py @ 48553:3b6b43a7ace4
logcmdutil: raise `StateError` when file to follow doesn't exist
Differential Revision: https://phab.mercurial-scm.org/D11969
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 06 Jan 2022 22:09:40 -0800 |
parents | 806d14efec8d |
children | 6000f5b25c9b |
rev | line source |
---|---|
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
1 # watchmanclient.py - Watchman client for the fsmonitor extension |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
2 # |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
3 # Copyright 2013-2016 Facebook, Inc. |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
4 # |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
7 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
8 from __future__ import absolute_import |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
9 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
10 import getpass |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
11 |
43398
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
12 from mercurial import ( |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
13 encoding, |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
14 util, |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
15 ) |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
16 from mercurial.utils import ( |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
17 procutil, |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
18 stringutil, |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
19 ) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
20 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
21 from . import pywatchman |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
22 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
23 |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
24 class Unavailable(Exception): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
25 def __init__(self, msg, warn=True, invalidate=False): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
26 self.msg = msg |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
27 self.warn = warn |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
28 if self.msg == b'timed out waiting for response': |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
29 self.warn = False |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
30 self.invalidate = invalidate |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
31 |
43398
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
32 def __bytes__(self): |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
33 if self.warn: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
34 return b'warning: Watchman unavailable: %s' % self.msg |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
35 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
36 return b'Watchman unavailable: %s' % self.msg |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
37 |
43398
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
38 __str__ = encoding.strmethod(__bytes__) |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
39 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
40 |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
41 class WatchmanNoRoot(Unavailable): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
42 def __init__(self, root, msg): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
43 self.root = root |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
44 super(WatchmanNoRoot, self).__init__(msg) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
45 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
46 |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
47 class client(object): |
42650
ab1900323b1d
fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents:
41968
diff
changeset
|
48 def __init__(self, ui, root, timeout=1.0): |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
49 err = None |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
50 if not self._user: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
51 err = b"couldn't get user" |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
52 warn = True |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
53 if self._user in ui.configlist(b'fsmonitor', b'blacklistusers'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
54 err = b'user %s in blacklist' % self._user |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
55 warn = False |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
56 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
57 if err: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
58 raise Unavailable(err, warn) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
59 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
60 self._timeout = timeout |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
61 self._watchmanclient = None |
42650
ab1900323b1d
fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents:
41968
diff
changeset
|
62 self._root = root |
ab1900323b1d
fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents:
41968
diff
changeset
|
63 self._ui = ui |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
64 self._firsttime = True |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
65 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
66 def settimeout(self, timeout): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
67 self._timeout = timeout |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
68 if self._watchmanclient is not None: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
69 self._watchmanclient.setTimeout(timeout) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
70 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
71 def getcurrentclock(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
72 result = self.command(b'clock') |
43115
4aa72cdf616f
py3: delete b'' prefix from safehasattr arguments
Martin von Zweigbergk <martinvonz@google.com>
parents:
43077
diff
changeset
|
73 if not util.safehasattr(result, 'clock'): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
74 raise Unavailable( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
75 b'clock result is missing clock value', invalidate=True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
76 ) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
77 return result.clock |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
78 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
79 def clearconnection(self): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
80 self._watchmanclient = None |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
81 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
82 def available(self): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
83 return self._watchmanclient is not None or self._firsttime |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
84 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
85 @util.propertycache |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
86 def _user(self): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
87 try: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
88 return getpass.getuser() |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
89 except KeyError: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
90 # couldn't figure out our user |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
91 return None |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
92 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
93 def _command(self, *args): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
94 watchmanargs = (args[0], self._root) + args[1:] |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
95 try: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
96 if self._watchmanclient is None: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
97 self._firsttime = False |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
98 watchman_exe = self._ui.configpath( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
99 b'fsmonitor', b'watchman_exe' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
100 ) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
101 self._watchmanclient = pywatchman.client( |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
102 timeout=self._timeout, |
41968
57264906a996
watchman: add the possibility to set the exact watchman binary location
Boris Feld <boris.feld@octobus.net>
parents:
30649
diff
changeset
|
103 useImmutableBser=True, |
43385
6469c23a40a2
fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43115
diff
changeset
|
104 binpath=procutil.tonativestr(watchman_exe), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42650
diff
changeset
|
105 ) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
106 return self._watchmanclient.query(*watchmanargs) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
107 except pywatchman.CommandError as ex: |
44074
806d14efec8d
fsmonitor: properly handle str ex.msg
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43398
diff
changeset
|
108 if 'unable to resolve root' in ex.msg: |
43398
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
109 raise WatchmanNoRoot( |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
110 self._root, stringutil.forcebytestr(ex.msg) |
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
111 ) |
44074
806d14efec8d
fsmonitor: properly handle str ex.msg
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43398
diff
changeset
|
112 raise Unavailable(stringutil.forcebytestr(ex.msg)) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
113 except pywatchman.WatchmanError as ex: |
43398
9a8f8c6ed965
fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43385
diff
changeset
|
114 raise Unavailable(stringutil.forcebytestr(ex)) |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
115 |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
116 def command(self, *args): |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
117 try: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
118 try: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
119 return self._command(*args) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
120 except WatchmanNoRoot: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
121 # this 'watch' command can also raise a WatchmanNoRoot if |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
122 # watchman refuses to accept this root |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
123 self._command(b'watch') |
28433
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
124 return self._command(*args) |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
125 except Unavailable: |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
126 # this is in an outer scope to catch Unavailable form any of the |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
127 # above _command calls |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
128 self._watchmanclient = None |
3b67f27bb908
fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
129 raise |