hgext/inotify/server.py
author Siddharth Agarwal <sid0@fb.com>
Wed, 02 Oct 2013 14:20:26 -0700
changeset 19866 993b24488679
parent 18107 c60d09dc2164
child 19869 4210a05c4329
permissions -rw-r--r--
inotify: add pidfile to parent options An upcoming patch will move pidfile writing from the parent to the child. This means that if the pid file isn't specified on the command-line but is specified as a config option, it needs to be added to the parent's opts dict.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
     1
# server.py - common entry point for inotify status server
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
#
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
     3
# Copyright 2009 Nicolas Dumazet <nicdumz@gmail.com>
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     5
# 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
     6
# GNU General Public License version 2 or any later version.
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
6961
12163fb21fce i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents: 6909
diff changeset
     8
from mercurial.i18n import _
18097
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
     9
from mercurial import cmdutil, posix, osutil, util
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
import common
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    12
import errno
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    13
import os
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    14
import socket
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    15
import stat
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    16
import struct
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
    17
import sys
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    19
class AlreadyStartedException(Exception):
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    20
    pass
10494
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    21
class TimeoutException(Exception):
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    22
    pass
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    23
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
def join(a, b):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    25
    if a:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    26
        if a[-1] == '/':
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    27
            return a + b
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
        return a + '/' + b
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    29
    return b
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    30
8787
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
    31
def split(path):
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
    32
    c = path.rfind('/')
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
    33
    if c == -1:
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
    34
        return '', path
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    35
    return path[:c], path[c + 1:]
8787
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
    36
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    37
walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
    39
def walk(dirstate, absroot, root):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    40
    '''Like os.walk, but only yields regular files.'''
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
    # This function is critical to performance during startup.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    43
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    44
    def walkit(root, reporoot):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    45
        files, dirs = [], []
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    46
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
        try:
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
    48
            fullpath = join(absroot, root)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    49
            for name, kind in osutil.listdir(fullpath):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
                if kind == stat.S_IFDIR:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    51
                    if name == '.hg':
8325
f2559645643a inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8324
diff changeset
    52
                        if not reporoot:
8323
589a82fb02a2 inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8322
diff changeset
    53
                            return
8325
f2559645643a inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8324
diff changeset
    54
                    else:
f2559645643a inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8324
diff changeset
    55
                        dirs.append(name)
8381
f52fcc864df4 inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8336
diff changeset
    56
                        path = join(root, name)
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
    57
                        if dirstate._ignore(path):
8381
f52fcc864df4 inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8336
diff changeset
    58
                            continue
f52fcc864df4 inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8336
diff changeset
    59
                        for result in walkit(path, False):
f52fcc864df4 inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8336
diff changeset
    60
                            yield result
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    61
                elif kind in (stat.S_IFREG, stat.S_IFLNK):
8334
0695288e8c37 inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8325
diff changeset
    62
                    files.append(name)
8324
b923d599c309 inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8323
diff changeset
    63
            yield fullpath, dirs, files
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    64
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    65
        except OSError, err:
9116
f90bbf1ea09f inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9115
diff changeset
    66
            if err.errno == errno.ENOTDIR:
f90bbf1ea09f inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9115
diff changeset
    67
                # fullpath was a directory, but has since been replaced
f90bbf1ea09f inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9115
diff changeset
    68
                # by a file.
f90bbf1ea09f inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9115
diff changeset
    69
                yield fullpath, dirs, files
f90bbf1ea09f inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9115
diff changeset
    70
            elif err.errno not in walk_ignored_errors:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    71
                raise
8320
a1305c1c8d8e inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8319
diff changeset
    72
a1305c1c8d8e inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8319
diff changeset
    73
    return walkit(root, root == '')
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    74
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    75
class directory(object):
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    76
    """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    77
    Representing a directory
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    78
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    79
    * path is the relative path from repo root to this directory
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    80
    * files is a dict listing the files in this directory
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    81
        - keys are file names
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    82
        - values are file status
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    83
    * dirs is a dict listing the subdirectories
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    84
        - key are subdirectories names
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    85
        - values are directory objects
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    86
    """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    87
    def __init__(self, relpath=''):
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    88
        self.path = relpath
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    89
        self.files = {}
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    90
        self.dirs = {}
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    91
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    92
    def dir(self, relpath):
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    93
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    94
        Returns the directory contained at the relative path relpath.
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    95
        Creates the intermediate directories if necessary.
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    96
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    97
        if not relpath:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    98
            return self
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
    99
        l = relpath.split('/')
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   100
        ret = self
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   101
        while l:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   102
            next = l.pop(0)
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   103
            try:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   104
                ret = ret.dirs[next]
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   105
            except KeyError:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   106
                d = directory(join(ret.path, next))
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   107
                ret.dirs[next] = d
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   108
                ret = d
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   109
        return ret
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   110
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   111
    def walk(self, states, visited=None):
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   112
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   113
        yield (filename, status) pairs for items in the trees
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   114
        that have status in states.
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   115
        filenames are relative to the repo root
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   116
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   117
        for file, st in self.files.iteritems():
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   118
            if st in states:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   119
                yield join(self.path, file), st
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   120
        for dir in self.dirs.itervalues():
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   121
            if visited is not None:
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   122
                visited.add(dir.path)
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   123
            for e in dir.walk(states):
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   124
                yield e
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   125
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   126
    def lookup(self, states, path, visited):
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   127
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   128
        yield root-relative filenames that match path, and whose
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   129
        status are in states:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   130
        * if path is a file, yield path
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   131
        * if path is a directory, yield directory files
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   132
        * if path is not tracked, yield nothing
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   133
        """
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   134
        if path[-1] == '/':
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   135
            path = path[:-1]
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   136
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   137
        paths = path.split('/')
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   138
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   139
        # we need to check separately for last node
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   140
        last = paths.pop()
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   141
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   142
        tree = self
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   143
        try:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   144
            for dir in paths:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   145
                tree = tree.dirs[dir]
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   146
        except KeyError:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   147
            # path is not tracked
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   148
            visited.add(tree.path)
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   149
            return
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   150
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   151
        try:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   152
            # if path is a directory, walk it
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   153
            target = tree.dirs[last]
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   154
            visited.add(target.path)
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   155
            for file, st in target.walk(states, visited):
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   156
                yield file
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   157
        except KeyError:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   158
            try:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   159
                if tree.files[last] in states:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   160
                    # path is a file
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   161
                    visited.add(tree.path)
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   162
                    yield path
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   163
            except KeyError:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   164
                # path is not tracked
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   165
                pass
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   166
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   167
class repowatcher(object):
8610
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   168
    """
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   169
    Watches inotify events
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   170
    """
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   171
    statuskeys = 'almr!?'
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   172
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   173
    def __init__(self, ui, dirstate, root):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   174
        self.ui = ui
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   175
        self.dirstate = dirstate
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   176
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   177
        self.wprefix = join(root, '')
9349
56fb15ad8fb1 inotify: server: use wprefix everywhere, introduce prefixlen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9348
diff changeset
   178
        self.prefixlen = len(self.wprefix)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   179
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   180
        self.tree = directory()
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   181
        self.statcache = {}
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   182
        self.statustrees = dict([(s, directory()) for s in self.statuskeys])
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   183
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   184
        self.ds_info = self.dirstate_info()
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   185
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   186
        self.last_event = None
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   187
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   188
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   189
    def handle_timeout(self):
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   190
        pass
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   191
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   192
    def dirstate_info(self):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   193
        try:
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   194
            st = os.lstat(self.wprefix + '.hg/dirstate')
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   195
            return st.st_mtime, st.st_ino
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   196
        except OSError, err:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   197
            if err.errno != errno.ENOENT:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   198
                raise
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   199
            return 0, 0
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   200
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   201
    def filestatus(self, fn, st):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   202
        try:
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   203
            type_, mode, size, time = self.dirstate._map[fn][:4]
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   204
        except KeyError:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   205
            type_ = '?'
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   206
        if type_ == 'n':
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   207
            st_mode, st_size, st_mtime = st
7082
be81b4788115 inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents: 6998
diff changeset
   208
            if size == -1:
be81b4788115 inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents: 6998
diff changeset
   209
                return 'l'
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   210
            if size and (size != st_size or (mode ^ st_mode) & 0100):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   211
                return 'm'
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   212
            if time != int(st_mtime):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   213
                return 'l'
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   214
            return 'n'
11545
db9d16233787 inotify: check all components of filenames against hgignore (issue884)
Renato Cunha <renatoc@gmail.com>
parents: 10494
diff changeset
   215
        if type_ == '?' and self.dirstate._dirignore(fn):
db9d16233787 inotify: check all components of filenames against hgignore (issue884)
Renato Cunha <renatoc@gmail.com>
parents: 10494
diff changeset
   216
            # we must check not only if the file is ignored, but if any part
db9d16233787 inotify: check all components of filenames against hgignore (issue884)
Renato Cunha <renatoc@gmail.com>
parents: 10494
diff changeset
   217
            # of its path match an ignore pattern
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   218
            return 'i'
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   219
        return type_
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   220
8599
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   221
    def updatefile(self, wfn, osstat):
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   222
        '''
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   223
        update the file entry of an existing file.
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   224
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   225
        osstat: (mode, size, time) tuple, as returned by os.lstat(wfn)
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   226
        '''
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   227
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   228
        self._updatestatus(wfn, self.filestatus(wfn, osstat))
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   229
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   230
    def deletefile(self, wfn, oldstatus):
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   231
        '''
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   232
        update the entry of a file which has been deleted.
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   233
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   234
        oldstatus: char in statuskeys, status of the file before deletion
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   235
        '''
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   236
        if oldstatus == 'r':
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   237
            newstatus = 'r'
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   238
        elif oldstatus in 'almn':
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   239
            newstatus = '!'
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   240
        else:
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   241
            newstatus = None
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   242
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   243
        self.statcache.pop(wfn, None)
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   244
        self._updatestatus(wfn, newstatus)
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   245
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   246
    def _updatestatus(self, wfn, newstatus):
8382
6f44b1adc948 inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8381
diff changeset
   247
        '''
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   248
        Update the stored status of a file.
8382
6f44b1adc948 inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8381
diff changeset
   249
8599
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   250
        newstatus: - char in (statuskeys + 'ni'), new status to apply.
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   251
                   - or None, to stop tracking wfn
8382
6f44b1adc948 inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8381
diff changeset
   252
        '''
8787
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
   253
        root, fn = split(wfn)
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   254
        d = self.tree.dir(root)
8599
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   255
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   256
        oldstatus = d.files.get(fn)
8599
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   257
        # oldstatus can be either:
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   258
        # - None : fn is new
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   259
        # - a char in statuskeys: fn is a (tracked) file
1f706b1b62f3 inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8557
diff changeset
   260
8382
6f44b1adc948 inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8381
diff changeset
   261
        if self.ui.debugflag and oldstatus != newstatus:
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   262
            self.ui.note(_('status: %r %s -> %s\n') %
8382
6f44b1adc948 inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8381
diff changeset
   263
                             (wfn, oldstatus, newstatus))
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   264
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   265
        if oldstatus and oldstatus in self.statuskeys \
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   266
            and oldstatus != newstatus:
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   267
            del self.statustrees[oldstatus].dir(root).files[fn]
9348
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   268
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   269
        if newstatus in (None, 'i'):
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   270
            d.files.pop(fn, None)
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   271
        elif oldstatus != newstatus:
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   272
            d.files[fn] = newstatus
9348
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   273
            if newstatus != 'n':
954f7a879495 inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9117
diff changeset
   274
                self.statustrees[newstatus].dir(root).files[fn] = newstatus
7892
67e59a9886d5 Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 7451
diff changeset
   275
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   276
    def check_deleted(self, key):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   277
        # Files that had been deleted but were present in the dirstate
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   278
        # may have vanished from the dirstate; we must clean them up.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   279
        nuke = []
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   280
        for wfn, ignore in self.statustrees[key].walk(key):
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   281
            if wfn not in self.dirstate:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   282
                nuke.append(wfn)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   283
        for wfn in nuke:
8787
9aca76502280 inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8786
diff changeset
   284
            root, fn = split(wfn)
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   285
            del self.statustrees[key].dir(root).files[fn]
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   286
            del self.tree.dir(root).files[fn]
6287
c86207d41512 Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   287
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   288
    def update_hgignore(self):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   289
        # An update of the ignore file can potentially change the
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   290
        # states of all unknown and ignored files.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   291
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   292
        # XXX If the user has other ignore files outside the repo, or
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   293
        # changes their list of ignore files at run time, we'll
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   294
        # potentially never see changes to them.  We could get the
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   295
        # client to report to us what ignore data they're using.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   296
        # But it's easier to do nothing than to open that can of
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   297
        # worms.
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   298
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   299
        if '_ignore' in self.dirstate.__dict__:
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   300
            delattr(self.dirstate, '_ignore')
6961
12163fb21fce i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents: 6909
diff changeset
   301
            self.ui.note(_('rescanning due to .hgignore change\n'))
8604
578f2a0049cd inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8600
diff changeset
   302
            self.handle_timeout()
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   303
            self.scan()
6287
c86207d41512 Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   304
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   305
    def getstat(self, wpath):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   306
        try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   307
            return self.statcache[wpath]
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   308
        except KeyError:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   309
            try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   310
                return self.stat(wpath)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   311
            except OSError, err:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   312
                if err.errno != errno.ENOENT:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   313
                    raise
6287
c86207d41512 Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   314
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   315
    def stat(self, wpath):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   316
        try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   317
            st = os.lstat(join(self.wprefix, wpath))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   318
            ret = st.st_mode, st.st_size, st.st_mtime
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   319
            self.statcache[wpath] = ret
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   320
            return ret
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7220
diff changeset
   321
        except OSError:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   322
            self.statcache.pop(wpath, None)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   323
            raise
6287
c86207d41512 Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   324
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   325
class socketlistener(object):
8610
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   326
    """
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   327
    Listens for client queries on unix socket inotify.sock
8ef1f63e554c inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8609
diff changeset
   328
    """
9350
b789ea382fc0 inotify: server: use dirstate instead of repo
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9349
diff changeset
   329
    def __init__(self, ui, root, repowatcher, timeout):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   330
        self.ui = ui
8335
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   331
        self.repowatcher = repowatcher
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   332
        try:
18097
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   333
            self.sock = posix.unixdomainserver(
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   334
                lambda p: os.path.join(root, '.hg', p),
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   335
                'inotify')
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   336
        except (OSError, socket.error), err:
18107
c60d09dc2164 inotify: on Python < 2.6, socket.error lacks errno
Bryan O'Sullivan <bryano@fb.com>
parents: 18099
diff changeset
   337
            if err.args[0] == errno.EADDRINUSE:
18097
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   338
                raise AlreadyStartedException(_('cannot start: '
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   339
                                                'socket is already bound'))
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18096
diff changeset
   340
            raise
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   341
        self.fileno = self.sock.fileno
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   342
8554
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   343
    def answer_stat_query(self, cs):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   344
        names = cs.read().split('\0')
6287
c86207d41512 Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   345
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   346
        states = names.pop()
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   347
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   348
        self.ui.note(_('answering query for %r\n') % states)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   349
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   350
        visited = set()
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   351
        if not names:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   352
            def genresult(states, tree):
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   353
                for fn, state in tree.walk(states):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   354
                    yield fn
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   355
        else:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   356
            def genresult(states, tree):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   357
                for fn in names:
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   358
                    for f in tree.lookup(states, fn, visited):
9115
b55d44719b47 inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8953
diff changeset
   359
                        yield f
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   360
8554
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   361
        return ['\0'.join(r) for r in [
8335
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   362
            genresult('l', self.repowatcher.statustrees['l']),
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   363
            genresult('m', self.repowatcher.statustrees['m']),
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   364
            genresult('a', self.repowatcher.statustrees['a']),
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   365
            genresult('r', self.repowatcher.statustrees['r']),
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   366
            genresult('!', self.repowatcher.statustrees['!']),
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   367
            '?' in states
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   368
                and genresult('?', self.repowatcher.statustrees['?'])
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   369
                or [],
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   370
            [],
8335
713ec3f9c9de inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8334
diff changeset
   371
            'c' in states and genresult('n', self.repowatcher.tree) or [],
9854
95e1867f765b inotify: mark directories visited during lookup (issue1844)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9514
diff changeset
   372
            visited
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   373
            ]]
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   374
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8554
diff changeset
   375
    def answer_dbug_query(self):
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8554
diff changeset
   376
        return ['\0'.join(self.repowatcher.debug())]
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8554
diff changeset
   377
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   378
    def accept_connection(self):
8554
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   379
        sock, addr = self.sock.accept()
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   380
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   381
        cs = common.recvcs(sock)
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   382
        version = ord(cs.read(1))
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   383
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   384
        if version != common.version:
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   385
            self.ui.warn(_('received query from incompatible client '
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   386
                           'version %d\n') % version)
8952
7c7a672e9db7 inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents: 8794
diff changeset
   387
            try:
7c7a672e9db7 inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents: 8794
diff changeset
   388
                # try to send back our version to the client
7c7a672e9db7 inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents: 8794
diff changeset
   389
                # this way, the client too is informed of the mismatch
7c7a672e9db7 inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents: 8794
diff changeset
   390
                sock.sendall(chr(common.version))
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 15151
diff changeset
   391
            except socket.error:
8952
7c7a672e9db7 inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents: 8794
diff changeset
   392
                pass
8554
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   393
            return
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   394
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   395
        type = cs.read(4)
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   396
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   397
        if type == 'STAT':
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   398
            results = self.answer_stat_query(cs)
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8554
diff changeset
   399
        elif type == 'DBUG':
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8554
diff changeset
   400
            results = self.answer_dbug_query()
8554
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   401
        else:
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   402
            self.ui.warn(_('unrecognized query type: %s\n') % type)
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   403
            return
47d7347484f5 inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8553
diff changeset
   404
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   405
        try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   406
            try:
8386
4aad982111b6 inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8385
diff changeset
   407
                v = chr(common.version)
4aad982111b6 inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8385
diff changeset
   408
8553
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   409
                sock.sendall(v + type + struct.pack(common.resphdrfmts[type],
e387ecd7a6ed inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8386
diff changeset
   410
                                            *map(len, results)))
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   411
                sock.sendall(''.join(results))
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   412
            finally:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   413
                sock.shutdown(socket.SHUT_WR)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   414
        except socket.error, err:
11567
34cc8b84407f removed exception args indexing (not supported by py3k)
Renato Cunha <renatoc@gmail.com>
parents: 11545
diff changeset
   415
            if err.args[0] != errno.EPIPE:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   416
                raise
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   417
15151
0d4f6e843b05 building: build inotify for sys.platform='linux*'
Nikolaj Sjujskij <sterkrig@myopera.com>
parents: 12650
diff changeset
   418
if sys.platform.startswith('linux'):
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   419
    import linuxserver as _server
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   420
else:
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   421
    raise ImportError
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   422
9933
2e7902158af9 inotify: create a common, OS-independent server entry point
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9900
diff changeset
   423
master = _server.master
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   424
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   425
def start(ui, dirstate, root, opts):
10494
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   426
    timeout = opts.get('idle_timeout')
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   427
    if timeout:
10494
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   428
        timeout = float(timeout) * 60000
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   429
    else:
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   430
        timeout = None
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   431
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   432
    class service(object):
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   433
        def init(self):
7451
fca9947652ce inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents: 7420
diff changeset
   434
            try:
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   435
                self.master = master(ui, dirstate, root, timeout)
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   436
            except AlreadyStartedException, inst:
9900
8939900073a8 inotify: improve error messages
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9897
diff changeset
   437
                raise util.Abort("inotify-server: %s" % inst)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   438
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   439
        def run(self):
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   440
            try:
10494
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   441
                try:
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   442
                    self.master.run()
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   443
                except TimeoutException:
08064db9f005 inotify/inserve: implement --timeout-idle option (issue885)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   444
                    pass
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   445
            finally:
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   446
                self.master.shutdown()
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   447
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   448
    if 'inserve' not in sys.argv:
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10090
diff changeset
   449
        runargs = util.hgcmd() + ['inserve', '-R', root]
9897
97eda2133a9b inotify: add a inotify.pidfile configuration possibility
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   450
    else:
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10090
diff changeset
   451
        runargs = util.hgcmd() + sys.argv[1:]
9897
97eda2133a9b inotify: add a inotify.pidfile configuration possibility
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   452
97eda2133a9b inotify: add a inotify.pidfile configuration possibility
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   453
    pidfile = ui.config('inotify', 'pidfile')
19866
993b24488679 inotify: add pidfile to parent options
Siddharth Agarwal <sid0@fb.com>
parents: 18107
diff changeset
   454
    opts.setdefault('pid_file', '')
993b24488679 inotify: add pidfile to parent options
Siddharth Agarwal <sid0@fb.com>
parents: 18107
diff changeset
   455
    if opts['daemon'] and pidfile is not None and not opts['pid_file']:
993b24488679 inotify: add pidfile to parent options
Siddharth Agarwal <sid0@fb.com>
parents: 18107
diff changeset
   456
        opts['pid_file'] = pidfile
9897
97eda2133a9b inotify: add a inotify.pidfile configuration possibility
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9854
diff changeset
   457
        runargs.append("--pid-file=%s" % pidfile)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   458
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   459
    service = service()
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   460
    logfile = ui.config('inotify', 'log')
10013
b2e87fde6806 run-tests: --inotify: use inotify.appendpid to append pids to $DAEMON_PIDS
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9933
diff changeset
   461
b2e87fde6806 run-tests: --inotify: use inotify.appendpid to append pids to $DAEMON_PIDS
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9933
diff changeset
   462
    appendpid = ui.configbool('inotify', 'appendpid', False)
b2e87fde6806 run-tests: --inotify: use inotify.appendpid to append pids to $DAEMON_PIDS
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9933
diff changeset
   463
11943
2d3cbcace897 inotify: show the exact command used to start the server
Greg Ward <greg-hg@gerg.ca>
parents: 11567
diff changeset
   464
    ui.debug('starting inotify server: %s\n' % ' '.join(runargs))
9514
7c01599dd340 inotify: use cmdutil.service instead of local daemonizing code
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9351
diff changeset
   465
    cmdutil.service(opts, initfn=service.init, runfn=service.run,
10013
b2e87fde6806 run-tests: --inotify: use inotify.appendpid to append pids to $DAEMON_PIDS
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9933
diff changeset
   466
                    logfile=logfile, runargs=runargs, appendpid=appendpid)