hgext/inotify/client.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Wed, 10 Sep 2008 23:20:04 +0200
changeset 7027 ab57069232b4
parent 6753 ed5ffb2c12f3
parent 7024 ee308d44ad76
child 7145 6f4a253f2a64
permissions -rw-r--r--
merge with -stable
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>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
#
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
# of the GNU General Public License, incorporated herein by reference.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
from mercurial.i18n import gettext as _
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
from mercurial import ui
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
import common
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
import os, select, socket, stat, struct, sys
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
6753
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6239
diff changeset
    14
def query(ui, repo, names, match, ignored, clean, unknown=True):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
    sock = socket.socket(socket.AF_UNIX)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
    sockpath = repo.join('inotify.sock')
7024
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    17
    try:
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    18
        sock.connect(sockpath)
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    19
    except socket.error, err:
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    20
        if err[0] == "AF_UNIX path too long":
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    21
            sockpath = os.readlink(sockpath)
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    22
            sock.connect(sockpath)
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    23
        else:
ee308d44ad76 inotify: add client code for long pathname handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    24
            raise
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    25
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    26
    def genquery():
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    27
        for n in names or []:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
            yield n
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    29
        states = 'almrx!'
6753
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6239
diff changeset
    30
        if ignored:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    31
            raise ValueError('this is insanity')
6753
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6239
diff changeset
    32
        if clean: states += 'n'
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6239
diff changeset
    33
        if unknown: states += '?'
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    34
        yield states
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    35
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    36
    req = '\0'.join(genquery())
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    37
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
    sock.sendall(chr(common.version))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    39
    sock.sendall(req)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    40
    sock.shutdown(socket.SHUT_WR)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    41
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    42
    cs = common.recvcs(sock)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    43
    version = ord(cs.read(1))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    44
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    45
    if version != common.version:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    46
        ui.warn(_('(inotify: received response from incompatible server '
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
                  'version %d)\n') % version)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    48
        return None
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    49
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
    try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    51
        resphdr = struct.unpack(common.resphdrfmt, cs.read(common.resphdrsize))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    52
    except struct.error:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
        return None
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    54
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
    def readnames(nbytes):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
        if nbytes:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
            names = cs.read(nbytes)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
            if names:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
                return filter(match, names.split('\0'))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
        return []
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    61
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    62
    return map(readnames, resphdr)