author | Nicolas Dumazet <nicdumz.commits@gmail.com> |
Sun, 28 Jun 2009 19:46:36 +0900 | |
changeset 9348 | 954f7a879495 |
parent 9117 | a87bc6e2a907 |
child 9349 | 56fb15ad8fb1 |
permissions | -rw-r--r-- |
6239 | 1 |
# server.py - inotify status server |
2 |
# |
|
3 |
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> |
|
4 |
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> |
|
5 |
# |
|
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
6 |
# This software may be used and distributed according to the terms of the |
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
7 |
# GNU General Public License version 2, incorporated herein by reference. |
6239 | 8 |
|
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
9 |
from mercurial.i18n import _ |
7420
b4ac1e2cd38c
inotify: remove unused imports (thanks pyflakes)
Brendan Cully <brendan@kublai.com>
parents:
7351
diff
changeset
|
10 |
from mercurial import osutil, util |
6239 | 11 |
import common |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
12 |
import errno, os, select, socket, stat, struct, sys, tempfile, time |
6239 | 13 |
|
14 |
try: |
|
6994
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
15 |
import linux as inotify |
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
16 |
from linux import watcher |
6239 | 17 |
except ImportError: |
18 |
raise |
|
19 |
||
20 |
class AlreadyStartedException(Exception): pass |
|
21 |
||
22 |
def join(a, b): |
|
23 |
if a: |
|
24 |
if a[-1] == '/': |
|
25 |
return a + b |
|
26 |
return a + '/' + b |
|
27 |
return b |
|
28 |
||
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
29 |
def split(path): |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
30 |
c = path.rfind('/') |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
31 |
if c == -1: |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
32 |
return '', path |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
33 |
return path[:c], path[c+1:] |
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
34 |
|
6239 | 35 |
walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) |
36 |
||
37 |
def walkrepodirs(repo): |
|
38 |
'''Iterate over all subdirectories of this repo. |
|
39 |
Exclude the .hg directory, any nested repos, and ignored dirs.''' |
|
40 |
rootslash = repo.root + os.sep |
|
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
41 |
|
6239 | 42 |
def walkit(dirname, top): |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
43 |
fullpath = rootslash + dirname |
6239 | 44 |
try: |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
45 |
for name, kind in osutil.listdir(fullpath): |
6239 | 46 |
if kind == stat.S_IFDIR: |
47 |
if name == '.hg': |
|
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
48 |
if not top: |
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
49 |
return |
6239 | 50 |
else: |
51 |
d = join(dirname, name) |
|
52 |
if repo.dirstate._ignore(d): |
|
53 |
continue |
|
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
54 |
for subdir in walkit(d, False): |
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
55 |
yield subdir |
6239 | 56 |
except OSError, err: |
57 |
if err.errno not in walk_ignored_errors: |
|
58 |
raise |
|
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
59 |
yield fullpath |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
60 |
|
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
61 |
return walkit('', True) |
6239 | 62 |
|
63 |
def walk(repo, root): |
|
64 |
'''Like os.walk, but only yields regular files.''' |
|
65 |
||
66 |
# This function is critical to performance during startup. |
|
67 |
||
68 |
rootslash = repo.root + os.sep |
|
69 |
||
70 |
def walkit(root, reporoot): |
|
71 |
files, dirs = [], [] |
|
72 |
||
73 |
try: |
|
74 |
fullpath = rootslash + root |
|
75 |
for name, kind in osutil.listdir(fullpath): |
|
76 |
if kind == stat.S_IFDIR: |
|
77 |
if name == '.hg': |
|
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
78 |
if not reporoot: |
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
79 |
return |
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
80 |
else: |
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
81 |
dirs.append(name) |
8381
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
82 |
path = join(root, name) |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
83 |
if repo.dirstate._ignore(path): |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
84 |
continue |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
85 |
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
|
86 |
yield result |
6239 | 87 |
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
|
88 |
files.append(name) |
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
89 |
yield fullpath, dirs, files |
6239 | 90 |
|
91 |
except OSError, err: |
|
9116
f90bbf1ea09f
inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9115
diff
changeset
|
92 |
if err.errno == errno.ENOTDIR: |
f90bbf1ea09f
inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9115
diff
changeset
|
93 |
# 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
|
94 |
# by a file. |
f90bbf1ea09f
inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9115
diff
changeset
|
95 |
yield fullpath, dirs, files |
f90bbf1ea09f
inotify: fix issue1375, add a test.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9115
diff
changeset
|
96 |
elif err.errno not in walk_ignored_errors: |
6239 | 97 |
raise |
8320
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
98 |
|
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
99 |
return walkit(root, root == '') |
6239 | 100 |
|
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
101 |
def _explain_watch_limit(ui, repo): |
6239 | 102 |
path = '/proc/sys/fs/inotify/max_user_watches' |
103 |
try: |
|
104 |
limit = int(file(path).read()) |
|
105 |
except IOError, err: |
|
106 |
if err.errno != errno.ENOENT: |
|
107 |
raise |
|
108 |
raise util.Abort(_('this system does not seem to ' |
|
109 |
'support inotify')) |
|
110 |
ui.warn(_('*** the current per-user limit on the number ' |
|
111 |
'of inotify watches is %s\n') % limit) |
|
112 |
ui.warn(_('*** this limit is too low to watch every ' |
|
113 |
'directory in this repository\n')) |
|
114 |
ui.warn(_('*** counting directories: ')) |
|
115 |
ndirs = len(list(walkrepodirs(repo))) |
|
116 |
ui.warn(_('found %d\n') % ndirs) |
|
117 |
newlimit = min(limit, 1024) |
|
118 |
while newlimit < ((limit + ndirs) * 1.1): |
|
119 |
newlimit *= 2 |
|
120 |
ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % |
|
121 |
(limit, newlimit)) |
|
122 |
ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) |
|
123 |
raise util.Abort(_('cannot watch %s until inotify watch limit is raised') |
|
124 |
% repo.root) |
|
125 |
||
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
126 |
class pollable(object): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
127 |
""" |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
128 |
Interface to support polling. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
129 |
The file descriptor returned by fileno() is registered to a polling |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
130 |
object. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
131 |
Usage: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
132 |
Every tick, check if an event has happened since the last tick: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
133 |
* If yes, call handle_events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
134 |
* If no, call handle_timeout |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
135 |
""" |
6239 | 136 |
poll_events = select.POLLIN |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
137 |
instances = {} |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
138 |
poll = select.poll() |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
139 |
|
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
140 |
def fileno(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
141 |
raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
142 |
|
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
143 |
def handle_events(self, events): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
144 |
raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
145 |
|
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
146 |
def handle_timeout(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
147 |
raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
148 |
|
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
149 |
def shutdown(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
150 |
raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
151 |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
152 |
def register(self, timeout): |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
153 |
fd = self.fileno() |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
154 |
|
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
155 |
pollable.poll.register(fd, pollable.poll_events) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
156 |
pollable.instances[fd] = self |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
157 |
|
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
158 |
self.registered = True |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
159 |
self.timeout = timeout |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
160 |
|
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
161 |
def unregister(self): |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
162 |
pollable.poll.unregister(self) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
163 |
self.registered = False |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
164 |
|
8793
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
165 |
@classmethod |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
166 |
def run(cls): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
167 |
while True: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
168 |
timeout = None |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
169 |
timeobj = None |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
170 |
for obj in cls.instances.itervalues(): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
171 |
if obj.timeout is not None and (timeout is None or obj.timeout < timeout): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
172 |
timeout, timeobj = obj.timeout, obj |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
173 |
try: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
174 |
events = cls.poll.poll(timeout) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
175 |
except select.error, err: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
176 |
if err[0] == errno.EINTR: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
177 |
continue |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
178 |
raise |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
179 |
if events: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
180 |
by_fd = {} |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
181 |
for fd, event in events: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
182 |
by_fd.setdefault(fd, []).append(event) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
183 |
|
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
184 |
for fd, events in by_fd.iteritems(): |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
185 |
cls.instances[fd].handle_pollevents(events) |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
186 |
|
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
187 |
elif timeobj: |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
188 |
timeobj.handle_timeout() |
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
189 |
|
8791
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
190 |
def eventaction(code): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
191 |
""" |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
192 |
Decorator to help handle events in repowatcher |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
193 |
""" |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
194 |
def decorator(f): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
195 |
def wrapper(self, wpath): |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
196 |
if code == 'm' and wpath in self.lastevent and \ |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
197 |
self.lastevent[wpath] in 'cm': |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
198 |
return |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
199 |
self.lastevent[wpath] = code |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
200 |
self.timeout = 250 |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
201 |
|
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
202 |
f(self, wpath) |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
203 |
|
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
204 |
wrapper.func_name = f.func_name |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
205 |
return wrapper |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
206 |
return decorator |
23730a475363
inotify.server: the decorator eventaction() shouldn't be a method of repowatcher
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8787
diff
changeset
|
207 |
|
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
208 |
class directory(object): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
209 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
210 |
Representing a directory |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
211 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
212 |
* 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
|
213 |
* 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
|
214 |
- 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
|
215 |
- 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
|
216 |
* 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
|
217 |
- 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
|
218 |
- 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
|
219 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
220 |
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
|
221 |
self.path = relpath |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
222 |
self.files = {} |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
223 |
self.dirs = {} |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
224 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
225 |
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
|
226 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
230 |
if not relpath: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
231 |
return self |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
232 |
l = relpath.split('/') |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
233 |
ret = self |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
234 |
while l: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
235 |
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
|
236 |
try: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
237 |
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
|
238 |
except KeyError: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
239 |
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
|
240 |
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
|
241 |
ret = d |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
242 |
return ret |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
243 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
244 |
def walk(self, states): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
245 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
for dir in self.dirs.itervalues(): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
254 |
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
|
255 |
yield e |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
256 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
257 |
def lookup(self, states, path): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
258 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
* 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
|
262 |
* 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
|
263 |
* 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
|
264 |
""" |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
265 |
if path[-1] == '/': |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
266 |
path = path[:-1] |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
267 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
268 |
paths = path.split('/') |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
269 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
270 |
# 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
|
271 |
last = paths.pop() |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
272 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
273 |
tree = self |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
274 |
try: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
275 |
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
|
276 |
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
|
277 |
except KeyError: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
278 |
# 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
|
279 |
return |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
280 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
281 |
try: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
282 |
# if path is a directory, walk it |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
283 |
for file, st in tree.dirs[last].walk(states): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
284 |
yield file |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
285 |
except KeyError: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
286 |
try: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
287 |
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
|
288 |
# path is a file |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
289 |
yield path |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
290 |
except KeyError: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
291 |
# 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
|
292 |
pass |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
293 |
|
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
294 |
class repowatcher(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
295 |
""" |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
296 |
Watches inotify events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
297 |
""" |
6239 | 298 |
statuskeys = 'almr!?' |
8383
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
299 |
mask = ( |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
300 |
inotify.IN_ATTRIB | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
301 |
inotify.IN_CREATE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
302 |
inotify.IN_DELETE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
303 |
inotify.IN_DELETE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
304 |
inotify.IN_MODIFY | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
305 |
inotify.IN_MOVED_FROM | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
306 |
inotify.IN_MOVED_TO | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
307 |
inotify.IN_MOVE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
308 |
inotify.IN_ONLYDIR | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
309 |
inotify.IN_UNMOUNT | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
310 |
0) |
6239 | 311 |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
312 |
def __init__(self, ui, repo): |
6239 | 313 |
self.ui = ui |
314 |
self.repo = repo |
|
315 |
self.wprefix = self.repo.wjoin('') |
|
316 |
try: |
|
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
317 |
self.watcher = watcher.watcher() |
6239 | 318 |
except OSError, err: |
319 |
raise util.Abort(_('inotify service not available: %s') % |
|
320 |
err.strerror) |
|
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
321 |
self.threshold = watcher.threshold(self.watcher) |
6239 | 322 |
self.fileno = self.watcher.fileno |
323 |
||
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
324 |
self.tree = directory() |
6239 | 325 |
self.statcache = {} |
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
326 |
self.statustrees = dict([(s, directory()) for s in self.statuskeys]) |
6239 | 327 |
|
328 |
self.last_event = None |
|
329 |
||
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
330 |
self.lastevent = {} |
6239 | 331 |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
332 |
self.register(timeout=None) |
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
333 |
|
6239 | 334 |
self.ds_info = self.dirstate_info() |
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
|
335 |
self.handle_timeout() |
6239 | 336 |
self.scan() |
337 |
||
338 |
def event_time(self): |
|
339 |
last = self.last_event |
|
340 |
now = time.time() |
|
341 |
self.last_event = now |
|
342 |
||
343 |
if last is None: |
|
344 |
return 'start' |
|
345 |
delta = now - last |
|
346 |
if delta < 5: |
|
347 |
return '+%.3f' % delta |
|
348 |
if delta < 50: |
|
349 |
return '+%.2f' % delta |
|
350 |
return '+%.1f' % delta |
|
351 |
||
352 |
def dirstate_info(self): |
|
353 |
try: |
|
354 |
st = os.lstat(self.repo.join('dirstate')) |
|
355 |
return st.st_mtime, st.st_ino |
|
356 |
except OSError, err: |
|
357 |
if err.errno != errno.ENOENT: |
|
358 |
raise |
|
359 |
return 0, 0 |
|
360 |
||
361 |
def add_watch(self, path, mask): |
|
362 |
if not path: |
|
363 |
return |
|
364 |
if self.watcher.path(path) is None: |
|
365 |
if self.ui.debugflag: |
|
366 |
self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) |
|
367 |
try: |
|
368 |
self.watcher.add(path, mask) |
|
369 |
except OSError, err: |
|
370 |
if err.errno in (errno.ENOENT, errno.ENOTDIR): |
|
371 |
return |
|
372 |
if err.errno != errno.ENOSPC: |
|
373 |
raise |
|
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
374 |
_explain_watch_limit(self.ui, self.repo) |
6239 | 375 |
|
376 |
def setup(self): |
|
377 |
self.ui.note(_('watching directories under %r\n') % self.repo.root) |
|
378 |
self.add_watch(self.repo.path, inotify.IN_DELETE) |
|
379 |
self.check_dirstate() |
|
380 |
||
381 |
def filestatus(self, fn, st): |
|
382 |
try: |
|
383 |
type_, mode, size, time = self.repo.dirstate._map[fn][:4] |
|
384 |
except KeyError: |
|
385 |
type_ = '?' |
|
386 |
if type_ == 'n': |
|
387 |
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
|
388 |
if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
389 |
return 'l' |
6239 | 390 |
if size and (size != st_size or (mode ^ st_mode) & 0100): |
391 |
return 'm' |
|
392 |
if time != int(st_mtime): |
|
393 |
return 'l' |
|
394 |
return 'n' |
|
395 |
if type_ == '?' and self.repo.dirstate._ignore(fn): |
|
396 |
return 'i' |
|
397 |
return type_ |
|
398 |
||
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
399 |
def updatefile(self, wfn, osstat): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
400 |
''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
401 |
update the file entry of an existing file. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
402 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
403 |
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
|
404 |
''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
405 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
406 |
self._updatestatus(wfn, self.filestatus(wfn, osstat)) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
407 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
408 |
def deletefile(self, wfn, oldstatus): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
409 |
''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
410 |
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
|
411 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
412 |
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
|
413 |
''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
414 |
if oldstatus == 'r': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
415 |
newstatus = 'r' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
416 |
elif oldstatus in 'almn': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
417 |
newstatus = '!' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
418 |
else: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
419 |
newstatus = None |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
420 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
421 |
self.statcache.pop(wfn, None) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
422 |
self._updatestatus(wfn, newstatus) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
423 |
|
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
424 |
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
|
425 |
''' |
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
426 |
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
|
427 |
|
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
428 |
newstatus: - char in (statuskeys + 'ni'), new status to apply. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
429 |
- 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
|
430 |
''' |
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
431 |
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
|
432 |
d = self.tree.dir(root) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
433 |
|
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
434 |
oldstatus = d.files.get(fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
435 |
# oldstatus can be either: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
436 |
# - None : fn is new |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
437 |
# - a char in statuskeys: fn is a (tracked) file |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
438 |
|
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
439 |
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
|
440 |
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
|
441 |
(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
|
442 |
|
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
443 |
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
|
444 |
and oldstatus != newstatus: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
445 |
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
|
446 |
|
954f7a879495
inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9117
diff
changeset
|
447 |
if newstatus in (None, 'i'): |
954f7a879495
inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9117
diff
changeset
|
448 |
d.files.pop(fn, None) |
954f7a879495
inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9117
diff
changeset
|
449 |
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
|
450 |
d.files[fn] = newstatus |
9348
954f7a879495
inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9117
diff
changeset
|
451 |
if newstatus != 'n': |
954f7a879495
inotify: server._updatestatus: simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9117
diff
changeset
|
452 |
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
|
453 |
|
6239 | 454 |
|
455 |
def check_deleted(self, key): |
|
456 |
# Files that had been deleted but were present in the dirstate |
|
457 |
# may have vanished from the dirstate; we must clean them up. |
|
458 |
nuke = [] |
|
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
459 |
for wfn, ignore in self.statustrees[key].walk(key): |
6239 | 460 |
if wfn not in self.repo.dirstate: |
461 |
nuke.append(wfn) |
|
462 |
for wfn in nuke: |
|
8787
9aca76502280
inotify: server: move split() out of server
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8786
diff
changeset
|
463 |
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
|
464 |
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
|
465 |
del self.tree.dir(root).files[fn] |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
466 |
|
6239 | 467 |
def scan(self, topdir=''): |
468 |
ds = self.repo.dirstate._map.copy() |
|
469 |
self.add_watch(join(self.repo.root, topdir), self.mask) |
|
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
470 |
for root, dirs, files in walk(self.repo, topdir): |
6239 | 471 |
for d in dirs: |
472 |
self.add_watch(join(root, d), self.mask) |
|
473 |
wroot = root[len(self.wprefix):] |
|
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
474 |
for fn in files: |
6239 | 475 |
wfn = join(wroot, fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
476 |
self.updatefile(wfn, self.getstat(wfn)) |
6239 | 477 |
ds.pop(wfn, None) |
478 |
wtopdir = topdir |
|
479 |
if wtopdir and wtopdir[-1] != '/': |
|
480 |
wtopdir += '/' |
|
481 |
for wfn, state in ds.iteritems(): |
|
482 |
if not wfn.startswith(wtopdir): |
|
483 |
continue |
|
7302
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
484 |
try: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
485 |
st = self.stat(wfn) |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
486 |
except OSError: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
487 |
status = state[0] |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
488 |
self.deletefile(wfn, status) |
6239 | 489 |
else: |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
490 |
self.updatefile(wfn, st) |
6239 | 491 |
self.check_deleted('!') |
492 |
self.check_deleted('r') |
|
493 |
||
494 |
def check_dirstate(self): |
|
495 |
ds_info = self.dirstate_info() |
|
496 |
if ds_info == self.ds_info: |
|
497 |
return |
|
498 |
self.ds_info = ds_info |
|
499 |
if not self.ui.debugflag: |
|
500 |
self.last_event = None |
|
501 |
self.ui.note(_('%s dirstate reload\n') % self.event_time()) |
|
502 |
self.repo.dirstate.invalidate() |
|
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
|
503 |
self.handle_timeout() |
6239 | 504 |
self.scan() |
505 |
self.ui.note(_('%s end dirstate reload\n') % self.event_time()) |
|
506 |
||
507 |
def update_hgignore(self): |
|
508 |
# An update of the ignore file can potentially change the |
|
509 |
# states of all unknown and ignored files. |
|
510 |
||
511 |
# XXX If the user has other ignore files outside the repo, or |
|
512 |
# changes their list of ignore files at run time, we'll |
|
513 |
# potentially never see changes to them. We could get the |
|
514 |
# client to report to us what ignore data they're using. |
|
515 |
# But it's easier to do nothing than to open that can of |
|
516 |
# worms. |
|
517 |
||
7085
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
518 |
if '_ignore' in self.repo.dirstate.__dict__: |
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
519 |
delattr(self.repo.dirstate, '_ignore') |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
520 |
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
|
521 |
self.handle_timeout() |
6239 | 522 |
self.scan() |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
523 |
|
6239 | 524 |
def getstat(self, wpath): |
525 |
try: |
|
526 |
return self.statcache[wpath] |
|
527 |
except KeyError: |
|
528 |
try: |
|
529 |
return self.stat(wpath) |
|
530 |
except OSError, err: |
|
531 |
if err.errno != errno.ENOENT: |
|
532 |
raise |
|
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
533 |
|
6239 | 534 |
def stat(self, wpath): |
535 |
try: |
|
536 |
st = os.lstat(join(self.wprefix, wpath)) |
|
537 |
ret = st.st_mode, st.st_size, st.st_mtime |
|
538 |
self.statcache[wpath] = ret |
|
539 |
return ret |
|
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
540 |
except OSError: |
6239 | 541 |
self.statcache.pop(wpath, None) |
542 |
raise |
|
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
543 |
|
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
544 |
@eventaction('c') |
6239 | 545 |
def created(self, wpath): |
546 |
if wpath == '.hgignore': |
|
547 |
self.update_hgignore() |
|
548 |
try: |
|
549 |
st = self.stat(wpath) |
|
550 |
if stat.S_ISREG(st[0]): |
|
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
551 |
self.updatefile(wpath, st) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
552 |
except OSError: |
6239 | 553 |
pass |
554 |
||
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
555 |
@eventaction('m') |
6239 | 556 |
def modified(self, wpath): |
557 |
if wpath == '.hgignore': |
|
558 |
self.update_hgignore() |
|
559 |
try: |
|
560 |
st = self.stat(wpath) |
|
561 |
if stat.S_ISREG(st[0]): |
|
562 |
if self.repo.dirstate[wpath] in 'lmn': |
|
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
563 |
self.updatefile(wpath, st) |
6239 | 564 |
except OSError: |
565 |
pass |
|
566 |
||
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
567 |
@eventaction('d') |
6239 | 568 |
def deleted(self, wpath): |
569 |
if wpath == '.hgignore': |
|
570 |
self.update_hgignore() |
|
571 |
elif wpath.startswith('.hg/'): |
|
572 |
if wpath == '.hg/wlock': |
|
573 |
self.check_dirstate() |
|
574 |
return |
|
575 |
||
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
576 |
self.deletefile(wpath, self.repo.dirstate[wpath]) |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
577 |
|
6239 | 578 |
def process_create(self, wpath, evt): |
579 |
if self.ui.debugflag: |
|
580 |
self.ui.note(_('%s event: created %s\n') % |
|
581 |
(self.event_time(), wpath)) |
|
582 |
||
583 |
if evt.mask & inotify.IN_ISDIR: |
|
584 |
self.scan(wpath) |
|
585 |
else: |
|
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
586 |
self.created(wpath) |
6239 | 587 |
|
588 |
def process_delete(self, wpath, evt): |
|
589 |
if self.ui.debugflag: |
|
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
590 |
self.ui.note(_('%s event: deleted %s\n') % |
6239 | 591 |
(self.event_time(), wpath)) |
592 |
||
593 |
if evt.mask & inotify.IN_ISDIR: |
|
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
594 |
tree = self.tree.dir(wpath) |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
595 |
todelete = [wfn for wfn, ignore in tree.walk('?')] |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
596 |
for fn in todelete: |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
597 |
self.deletefile(fn, '?') |
6239 | 598 |
self.scan(wpath) |
8600
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
599 |
else: |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
600 |
self.deleted(wpath) |
6239 | 601 |
|
602 |
def process_modify(self, wpath, evt): |
|
603 |
if self.ui.debugflag: |
|
604 |
self.ui.note(_('%s event: modified %s\n') % |
|
605 |
(self.event_time(), wpath)) |
|
606 |
||
607 |
if not (evt.mask & inotify.IN_ISDIR): |
|
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
608 |
self.modified(wpath) |
6239 | 609 |
|
610 |
def process_unmount(self, evt): |
|
611 |
self.ui.warn(_('filesystem containing %s was unmounted\n') % |
|
612 |
evt.fullpath) |
|
613 |
sys.exit(0) |
|
614 |
||
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
615 |
def handle_pollevents(self, events): |
6239 | 616 |
if self.ui.debugflag: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
617 |
self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 618 |
(self.event_time(), self.threshold.readable())) |
619 |
if not self.threshold(): |
|
620 |
if self.registered: |
|
621 |
if self.ui.debugflag: |
|
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
622 |
self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 623 |
(self.event_time())) |
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
624 |
self.unregister() |
6239 | 625 |
self.timeout = 250 |
626 |
else: |
|
627 |
self.read_events() |
|
628 |
||
629 |
def read_events(self, bufsize=None): |
|
630 |
events = self.watcher.read(bufsize) |
|
631 |
if self.ui.debugflag: |
|
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
632 |
self.ui.note(_('%s reading %d events\n') % |
6239 | 633 |
(self.event_time(), len(events))) |
634 |
for evt in events: |
|
8953
e1d119f450f0
inotify: server: remove wpath method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8952
diff
changeset
|
635 |
assert evt.fullpath.startswith(self.wprefix) |
e1d119f450f0
inotify: server: remove wpath method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8952
diff
changeset
|
636 |
wpath = evt.fullpath[len(self.wprefix):] |
e1d119f450f0
inotify: server: remove wpath method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8952
diff
changeset
|
637 |
|
9117
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
638 |
# paths have been normalized, wpath never ends with a '/' |
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
639 |
|
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
640 |
if wpath.startswith('.hg/') and evt.mask & inotify.IN_ISDIR: |
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
641 |
# ignore subdirectories of .hg/ (merge, patches...) |
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
642 |
continue |
a87bc6e2a907
inotify: server: explicitely ignore events in subdirs of .hg/ (issue1735)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9116
diff
changeset
|
643 |
|
6239 | 644 |
if evt.mask & inotify.IN_UNMOUNT: |
645 |
self.process_unmount(wpath, evt) |
|
646 |
elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): |
|
647 |
self.process_modify(wpath, evt) |
|
648 |
elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | |
|
649 |
inotify.IN_MOVED_FROM): |
|
650 |
self.process_delete(wpath, evt) |
|
651 |
elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): |
|
652 |
self.process_create(wpath, evt) |
|
653 |
||
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
654 |
self.lastevent.clear() |
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
655 |
|
6239 | 656 |
def handle_timeout(self): |
657 |
if not self.registered: |
|
658 |
if self.ui.debugflag: |
|
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
659 |
self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 660 |
(self.event_time(), self.threshold.readable())) |
661 |
self.read_events(0) |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
662 |
self.register(timeout=None) |
6239 | 663 |
|
664 |
self.timeout = None |
|
665 |
||
666 |
def shutdown(self): |
|
667 |
self.watcher.close() |
|
668 |
||
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
669 |
def debug(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
670 |
""" |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
671 |
Returns a sorted list of relatives paths currently watched, |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
672 |
for debugging purposes. |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
673 |
""" |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
674 |
return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher) |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
675 |
|
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
676 |
class server(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
677 |
""" |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
678 |
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
|
679 |
""" |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
680 |
def __init__(self, ui, repo, repowatcher, timeout): |
6239 | 681 |
self.ui = ui |
682 |
self.repo = repo |
|
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
683 |
self.repowatcher = repowatcher |
6239 | 684 |
self.sock = socket.socket(socket.AF_UNIX) |
685 |
self.sockpath = self.repo.join('inotify.sock') |
|
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
686 |
self.realsockpath = None |
6239 | 687 |
try: |
688 |
self.sock.bind(self.sockpath) |
|
689 |
except socket.error, err: |
|
690 |
if err[0] == errno.EADDRINUSE: |
|
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
691 |
raise AlreadyStartedException(_('could not start server: %s') |
6239 | 692 |
% err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
693 |
if err[0] == "AF_UNIX path too long": |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
694 |
tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
695 |
self.realsockpath = os.path.join(tempdir, "inotify.sock") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
696 |
try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
697 |
self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
698 |
os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
699 |
except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
700 |
try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
701 |
os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
702 |
except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
703 |
pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
704 |
os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
705 |
if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
706 |
raise AlreadyStartedException(_('could not start server: %s') |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
707 |
% inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
708 |
raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
709 |
else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
710 |
raise |
6239 | 711 |
self.sock.listen(5) |
712 |
self.fileno = self.sock.fileno |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
713 |
self.register(timeout=timeout) |
6239 | 714 |
|
715 |
def handle_timeout(self): |
|
716 |
pass |
|
717 |
||
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
718 |
def answer_stat_query(self, cs): |
6239 | 719 |
names = cs.read().split('\0') |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
720 |
|
6239 | 721 |
states = names.pop() |
722 |
||
723 |
self.ui.note(_('answering query for %r\n') % states) |
|
724 |
||
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
725 |
if self.repowatcher.timeout: |
6239 | 726 |
# We got a query while a rescan is pending. Make sure we |
727 |
# rescan before responding, or we could give back a wrong |
|
728 |
# answer. |
|
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
729 |
self.repowatcher.handle_timeout() |
6239 | 730 |
|
731 |
if not names: |
|
732 |
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
|
733 |
for fn, state in tree.walk(states): |
6239 | 734 |
yield fn |
735 |
else: |
|
736 |
def genresult(states, tree): |
|
737 |
for fn in names: |
|
9115
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
738 |
for f in tree.lookup(states, fn): |
b55d44719b47
inotify: server: new data structure to keep track of changes.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8953
diff
changeset
|
739 |
yield f |
6239 | 740 |
|
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
741 |
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
|
742 |
genresult('l', self.repowatcher.statustrees['l']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
743 |
genresult('m', self.repowatcher.statustrees['m']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
744 |
genresult('a', self.repowatcher.statustrees['a']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
745 |
genresult('r', self.repowatcher.statustrees['r']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
746 |
genresult('!', self.repowatcher.statustrees['!']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
747 |
'?' in states |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
748 |
and genresult('?', self.repowatcher.statustrees['?']) |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
749 |
or [], |
6239 | 750 |
[], |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
751 |
'c' in states and genresult('n', self.repowatcher.tree) or [], |
6239 | 752 |
]] |
753 |
||
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
754 |
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
|
755 |
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
|
756 |
|
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
757 |
def handle_pollevents(self, events): |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
758 |
for e in events: |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
759 |
self.handle_pollevent() |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
760 |
|
8608
228db070bfc4
inotify: rename handle_event to handle_pollevent to avoid confusion
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8607
diff
changeset
|
761 |
def handle_pollevent(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
|
762 |
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
|
763 |
|
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
764 |
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
|
765 |
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
|
766 |
|
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
767 |
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
|
768 |
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
|
769 |
'version %d\n') % version) |
8952
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
770 |
try: |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
771 |
# 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
|
772 |
# 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
|
773 |
sock.sendall(chr(common.version)) |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
774 |
except: |
7c7a672e9db7
inotify: return version to client even when not matching
Simon Heimberg <simohe@besonet.ch>
parents:
8794
diff
changeset
|
775 |
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
|
776 |
return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
777 |
|
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
778 |
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
|
779 |
|
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
780 |
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
|
781 |
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
|
782 |
elif type == 'DBUG': |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
783 |
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
|
784 |
else: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
785 |
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
|
786 |
return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
787 |
|
6239 | 788 |
try: |
789 |
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
|
790 |
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
|
791 |
|
8553
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
792 |
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
|
793 |
*map(len, results))) |
6239 | 794 |
sock.sendall(''.join(results)) |
795 |
finally: |
|
796 |
sock.shutdown(socket.SHUT_WR) |
|
797 |
except socket.error, err: |
|
798 |
if err[0] != errno.EPIPE: |
|
799 |
raise |
|
800 |
||
801 |
def shutdown(self): |
|
802 |
self.sock.close() |
|
803 |
try: |
|
804 |
os.unlink(self.sockpath) |
|
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
805 |
if self.realsockpath: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
806 |
os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
807 |
os.rmdir(os.path.dirname(self.realsockpath)) |
6239 | 808 |
except OSError, err: |
809 |
if err.errno != errno.ENOENT: |
|
810 |
raise |
|
811 |
||
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
812 |
class master(object): |
6239 | 813 |
def __init__(self, ui, repo, timeout=None): |
814 |
self.ui = ui |
|
815 |
self.repo = repo |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
816 |
self.repowatcher = repowatcher(ui, repo) |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
817 |
self.server = server(ui, repo, self.repowatcher, timeout) |
6239 | 818 |
|
819 |
def shutdown(self): |
|
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
820 |
for obj in pollable.instances.itervalues(): |
6239 | 821 |
obj.shutdown() |
822 |
||
823 |
def run(self): |
|
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
824 |
self.repowatcher.setup() |
6239 | 825 |
self.ui.note(_('finished setup\n')) |
826 |
if os.getenv('TIME_STARTUP'): |
|
827 |
sys.exit(0) |
|
8793
9d0c521bce0e
inotify: put the "while True: poll()" loop in pollable class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8792
diff
changeset
|
828 |
pollable.run() |
6239 | 829 |
|
830 |
def start(ui, repo): |
|
7451
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
831 |
def closefds(ignore): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
832 |
# (from python bug #1177468) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
833 |
# close all inherited file descriptors |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
834 |
# Python 2.4.1 and later use /dev/urandom to seed the random module's RNG |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
835 |
# a file descriptor is kept internally as os._urandomfd (created on demand |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
836 |
# the first time os.urandom() is called), and should not be closed |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
837 |
try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
838 |
os.urandom(4) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
839 |
urandom_fd = getattr(os, '_urandomfd', None) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
840 |
except AttributeError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
841 |
urandom_fd = None |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
842 |
ignore.append(urandom_fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
843 |
for fd in range(3, 256): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
844 |
if fd in ignore: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
845 |
continue |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
846 |
try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
847 |
os.close(fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
848 |
except OSError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
849 |
pass |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
850 |
|
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
851 |
m = master(ui, repo) |
6239 | 852 |
sys.stdout.flush() |
853 |
sys.stderr.flush() |
|
854 |
||
855 |
pid = os.fork() |
|
856 |
if pid: |
|
857 |
return pid |
|
858 |
||
8792
3e23b1d20837
inotify: refactor (un)register methods into pollable object
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8791
diff
changeset
|
859 |
closefds(pollable.instances.keys()) |
6239 | 860 |
os.setsid() |
861 |
||
862 |
fd = os.open('/dev/null', os.O_RDONLY) |
|
863 |
os.dup2(fd, 0) |
|
864 |
if fd > 0: |
|
865 |
os.close(fd) |
|
866 |
||
867 |
fd = os.open(ui.config('inotify', 'log', '/dev/null'), |
|
868 |
os.O_RDWR | os.O_CREAT | os.O_TRUNC) |
|
869 |
os.dup2(fd, 1) |
|
870 |
os.dup2(fd, 2) |
|
871 |
if fd > 2: |
|
872 |
os.close(fd) |
|
873 |
||
874 |
try: |
|
875 |
m.run() |
|
876 |
finally: |
|
877 |
m.shutdown() |
|
878 |
os._exit(0) |