hgext/inotify/client.py
author Matt Mackall <mpm@selenic.com>
Mon, 25 Jan 2010 00:05:27 -0600
changeset 10282 08a0f04b56bd
parent 10263 25e572394f5c
child 11567 34cc8b84407f
child 11628 68a30daead3f
permissions -rw-r--r--
many, many trivial check-code fixups
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
# client.py - inotify status client
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
#
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com>
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
     5
# Copyright 2009 Nicolas Dumazet <nicdumz@gmail.com>
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8067
diff changeset
     7
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9900
diff changeset
     8
# GNU General Public License version 2 or any later version.
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
7225
59b4ae211584 i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents: 7145
diff changeset
    10
from mercurial.i18n import _
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    11
import common, server
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    12
import errno, os, socket, struct
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    13
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    14
class QueryFailed(Exception):
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    15
    pass
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    16
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    17
def start_server(function):
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    18
    """
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    19
    Decorator.
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    20
    Tries to call function, if it fails, try to (re)start inotify server.
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    21
    Raise QueryFailed if something went wrong
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    22
    """
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    23
    def decorated_function(self, *args):
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    24
        result = None
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    25
        try:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    26
            return function(self, *args)
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    27
        except (OSError, socket.error), err:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    28
            autostart = self.ui.configbool('inotify', 'autostart', True)
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    29
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    30
            if err[0] == errno.ECONNREFUSED:
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    31
                self.ui.warn(_('inotify-client: found dead inotify server '
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    32
                               'socket; removing it\n'))
9351
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8788
diff changeset
    33
                os.unlink(os.path.join(self.root, '.hg', 'inotify.sock'))
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    34
            if err[0] in (errno.ECONNREFUSED, errno.ENOENT) and autostart:
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9351
diff changeset
    35
                self.ui.debug('(starting inotify server)\n')
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    36
                try:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    37
                    try:
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9467
diff changeset
    38
                        server.start(self.ui, self.dirstate, self.root,
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9467
diff changeset
    39
                                     dict(daemon=True, daemon_pipefds=''))
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    40
                    except server.AlreadyStartedException, inst:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    41
                        # another process may have started its own
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    42
                        # inotify server while this one was starting.
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    43
                        self.ui.debug(str(inst))
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    44
                except Exception, inst:
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    45
                    self.ui.warn(_('inotify-client: could not start inotify '
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    46
                                   'server: %s\n') % inst)
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    47
                else:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    48
                    try:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    49
                        return function(self, *args)
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    50
                    except socket.error, err:
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    51
                        self.ui.warn(_('inotify-client: could not talk to new '
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    52
                                       'inotify server: %s\n') % err[-1])
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    53
            elif err[0] in (errno.ECONNREFUSED, errno.ENOENT):
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    54
                # silently ignore normal errors if autostart is False
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9351
diff changeset
    55
                self.ui.debug('(inotify server not running)\n')
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    56
            else:
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    57
                self.ui.warn(_('inotify-client: failed to contact inotify '
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
    58
                               'server: %s\n') % err[-1])
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    59
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    60
        self.ui.traceback()
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    61
        raise QueryFailed('inotify query failed')
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    62
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    63
    return decorated_function
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    64
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    65
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    66
class client(object):
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    67
    def __init__(self, ui, repo):
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    68
        self.ui = ui
9351
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8788
diff changeset
    69
        self.dirstate = repo.dirstate
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8788
diff changeset
    70
        self.root = repo.root
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    71
        self.sock = socket.socket(socket.AF_UNIX)
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    72
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    73
    def _connect(self):
9351
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8788
diff changeset
    74
        sockpath = os.path.join(self.root, '.hg', 'inotify.sock')
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    75
        try:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    76
            self.sock.connect(sockpath)
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    77
        except socket.error, err:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    78
            if err[0] == "AF_UNIX path too long":
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    79
                sockpath = os.readlink(sockpath)
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    80
                self.sock.connect(sockpath)
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    81
            else:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    82
                raise
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    83
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    84
    def _send(self, type, data):
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    85
        """Sends protocol version number, and the data"""
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    86
        self.sock.sendall(chr(common.version) + type + data)
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    87
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    88
        self.sock.shutdown(socket.SHUT_WR)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    89
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    90
    def _receive(self, type):
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    91
        """
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    92
        Read data, check version number, extract headers,
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    93
        and returns a tuple (data descriptor, header)
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    94
        Raises QueryFailed on error
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    95
        """
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
    96
        cs = common.recvcs(self.sock)
8788
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    97
        try:
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    98
            version = ord(cs.read(1))
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    99
        except TypeError:
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
   100
            # empty answer, assume the server crashed
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   101
            self.ui.warn(_('inotify-client: received empty answer from inotify '
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   102
                           'server'))
8788
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
   103
            raise QueryFailed('server crashed')
5d8021ac0e19 inotify: raise QueryFailed when the server crash
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
   104
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   105
        if version != common.version:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   106
            self.ui.warn(_('(inotify: received response from incompatible '
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   107
                      'server version %d)\n') % version)
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
   108
            raise QueryFailed('incompatible server version')
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   109
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   110
        readtype = cs.read(4)
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   111
        if readtype != type:
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   112
            self.ui.warn(_('(inotify: received \'%s\' response when expecting'
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   113
                       ' \'%s\')\n') % (readtype, type))
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   114
            raise QueryFailed('wrong response type')
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   115
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   116
        hdrfmt = common.resphdrfmts[type]
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   117
        hdrsize = common.resphdrsizes[type]
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   118
        try:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   119
            resphdr = struct.unpack(hdrfmt, cs.read(hdrsize))
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   120
        except struct.error:
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
   121
            raise QueryFailed('unable to retrieve query response headers')
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   122
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   123
        return cs, resphdr
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   124
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   125
    def query(self, type, req):
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   126
        self._connect()
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   127
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   128
        self._send(type, req)
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   129
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   130
        return self._receive(type)
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   131
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
   132
    @start_server
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   133
    def statusquery(self, names, match, ignored, clean, unknown=True):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   134
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   135
        def genquery():
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   136
            for n in names:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   137
                yield n
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   138
            states = 'almrx!'
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   139
            if ignored:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   140
                raise ValueError('this is insanity')
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   141
            if clean:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   142
                states += 'c'
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   143
            if unknown:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   144
                states += '?'
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   145
            yield states
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   146
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   147
        req = '\0'.join(genquery())
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   148
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   149
        cs, resphdr = self.query('STAT', req)
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   150
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   151
        def readnames(nbytes):
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   152
            if nbytes:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   153
                names = cs.read(nbytes)
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   154
                if names:
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   155
                    return filter(match, names.split('\0'))
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   156
            return []
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   157
        results = map(readnames, resphdr[:-1])
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   158
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   159
        if names:
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   160
            nbytes = resphdr[-1]
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   161
            vdirs = cs.read(nbytes)
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   162
            if vdirs:
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   163
                for vdir in vdirs.split('\0'):
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   164
                    match.dir(vdir)
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   165
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   166
        return results
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   167
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   168
    @start_server
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   169
    def debugquery(self):
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   170
        cs, resphdr = self.query('DBUG', '')
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   171
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   172
        nbytes = resphdr[0]
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   173
        names = cs.read(nbytes)
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   174
        return names.split('\0')