hgext/fsmonitor/watchmanclient.py
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
Wed, 14 Feb 2024 22:48:09 -0500
changeset 51383 880740426a34
parent 50928 d718eddf01d9
child 51863 f4733654f144
permissions -rw-r--r--
crecord: add a showsearch function This function takes a regex and searches either forward or backward, moving the current item to the found item, if any, and unfolding the relevant context.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
     9
import getpass
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    10
43398
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    11
from mercurial import (
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    12
    encoding,
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    13
    util,
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    14
)
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    15
from mercurial.utils import (
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    16
    procutil,
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    17
    stringutil,
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    18
)
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    19
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    20
from . import pywatchman
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    22
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    23
class Unavailable(Exception):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    24
    def __init__(self, msg, warn=True, invalidate=False):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    25
        self.msg = msg
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    26
        self.warn = warn
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    27
        if self.msg == b'timed out waiting for response':
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    28
            self.warn = False
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    29
        self.invalidate = invalidate
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    30
43398
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    31
    def __bytes__(self):
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    32
        if self.warn:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
            return b'warning: Watchman unavailable: %s' % self.msg
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    34
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
            return b'Watchman unavailable: %s' % self.msg
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    36
43398
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    37
    __str__ = encoding.strmethod(__bytes__)
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
    38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    39
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    40
class WatchmanNoRoot(Unavailable):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    41
    def __init__(self, root, msg):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    42
        self.root = root
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    43
        super(WatchmanNoRoot, self).__init__(msg)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    44
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    45
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    46
class client:
42650
ab1900323b1d fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents: 41968
diff changeset
    47
    def __init__(self, ui, root, timeout=1.0):
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    48
        err = None
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    49
        if not self._user:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
            err = b"couldn't get user"
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    51
            warn = True
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    52
        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
    53
            err = b'user %s in blacklist' % self._user
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    54
            warn = False
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    55
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    56
        if err:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    57
            raise Unavailable(err, warn)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    58
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    59
        self._timeout = timeout
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    60
        self._watchmanclient = None
42650
ab1900323b1d fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents: 41968
diff changeset
    61
        self._root = root
ab1900323b1d fsmonitor: refactor watchmanclient.client to accept ui and repo path
Augie Fackler <augie@google.com>
parents: 41968
diff changeset
    62
        self._ui = ui
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    63
        self._firsttime = True
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    64
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    65
    def settimeout(self, timeout):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    66
        self._timeout = timeout
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    67
        if self._watchmanclient is not None:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    68
            self._watchmanclient.setTimeout(timeout)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    69
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    70
    def getcurrentclock(self):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
        result = self.command(b'clock')
50928
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48946
diff changeset
    72
        if not hasattr(result, 'clock'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    73
            raise Unavailable(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
                b'clock result is missing clock value', invalidate=True
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    75
            )
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    76
        return result.clock
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    77
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    78
    def clearconnection(self):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    79
        self._watchmanclient = None
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    80
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    81
    def available(self):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    82
        return self._watchmanclient is not None or self._firsttime
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    83
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    84
    @util.propertycache
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    85
    def _user(self):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    86
        try:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    87
            return getpass.getuser()
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    88
        except KeyError:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    89
            # couldn't figure out our user
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    90
            return None
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    91
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    92
    def _command(self, *args):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    93
        watchmanargs = (args[0], self._root) + args[1:]
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    94
        try:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    95
            if self._watchmanclient is None:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
    96
                self._firsttime = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
                watchman_exe = self._ui.configpath(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    98
                    b'fsmonitor', b'watchman_exe'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
                )
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   100
                self._watchmanclient = pywatchman.client(
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   101
                    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
   102
                    useImmutableBser=True,
43385
6469c23a40a2 fsmonitor: refresh pywatchman with upstream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43115
diff changeset
   103
                    binpath=procutil.tonativestr(watchman_exe),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   104
                )
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   105
            return self._watchmanclient.query(*watchmanargs)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   106
        except pywatchman.CommandError as ex:
44074
806d14efec8d fsmonitor: properly handle str ex.msg
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43398
diff changeset
   107
            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
   108
                raise WatchmanNoRoot(
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
   109
                    self._root, stringutil.forcebytestr(ex.msg)
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
   110
                )
44074
806d14efec8d fsmonitor: properly handle str ex.msg
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43398
diff changeset
   111
            raise Unavailable(stringutil.forcebytestr(ex.msg))
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   112
        except pywatchman.WatchmanError as ex:
43398
9a8f8c6ed965 fsmonitor: normalize exception types to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43385
diff changeset
   113
            raise Unavailable(stringutil.forcebytestr(ex))
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   114
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   115
    def command(self, *args):
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   116
        try:
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
                return self._command(*args)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   119
            except WatchmanNoRoot:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   120
                # this 'watch' command can also raise a WatchmanNoRoot if
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   121
                # 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
   122
                self._command(b'watch')
28433
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   123
                return self._command(*args)
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   124
        except Unavailable:
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   125
            # 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
   126
            # above _command calls
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   127
            self._watchmanclient = None
3b67f27bb908 fsmonitor: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff changeset
   128
            raise