##// END OF EJS Templates
inotify: Simplifying init code...
Nicolas Dumazet -
r8068:389e2820 default
parent child Browse files
Show More
@@ -1,127 +1,125 b''
1 1 # __init__.py - inotify-based status acceleration for Linux
2 2 #
3 3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com>
4 4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 '''inotify-based status acceleration for Linux systems
10 10 '''
11 11
12 12 # todo: socket permissions
13 13
14 14 from mercurial.i18n import _
15 15 from mercurial import cmdutil, util
16 16 import client, errno, os, server, socket
17 17 from weakref import proxy
18 18
19 19 def serve(ui, repo, **opts):
20 20 '''start an inotify server for this repository'''
21 21 timeout = opts.get('timeout')
22 22 if timeout:
23 23 timeout = float(timeout) * 1e3
24 24
25 25 class service:
26 26 def init(self):
27 27 try:
28 28 self.master = server.Master(ui, repo, timeout)
29 29 except server.AlreadyStartedException, inst:
30 30 raise util.Abort(str(inst))
31 31
32 32 def run(self):
33 33 try:
34 34 self.master.run()
35 35 finally:
36 36 self.master.shutdown()
37 37
38 38 service = service()
39 39 cmdutil.service(opts, initfn=service.init, runfn=service.run)
40 40
41 41 def reposetup(ui, repo):
42 42 if not hasattr(repo, 'dirstate'):
43 43 return
44 44
45 45 # XXX: weakref until hg stops relying on __del__
46 46 repo = proxy(repo)
47 47
48 48 class inotifydirstate(repo.dirstate.__class__):
49 49 # Set to True if we're the inotify server, so we don't attempt
50 50 # to recurse.
51 51 inotifyserver = False
52 52
53 53 def status(self, match, ignored, clean, unknown=True):
54 54 files = match.files()
55 55 if '.' in files:
56 56 files = []
57 57 try:
58 58 if not ignored and not self.inotifyserver:
59 59 result = client.query(ui, repo, files, match, False,
60 60 clean, unknown)
61 61 if ui.config('inotify', 'debug'):
62 62 r2 = super(inotifydirstate, self).status(
63 63 match, False, clean, unknown)
64 64 for c,a,b in zip('LMARDUIC', result, r2):
65 65 for f in a:
66 66 if f not in b:
67 67 ui.warn('*** inotify: %s +%s\n' % (c, f))
68 68 for f in b:
69 69 if f not in a:
70 70 ui.warn('*** inotify: %s -%s\n' % (c, f))
71 71 result = r2
72 72
73 73 if result is not None:
74 74 return result
75 75 except (OSError, socket.error), err:
76 76 autostart = ui.configbool('inotify', 'autostart', True)
77 77
78 78 if err[0] == errno.ECONNREFUSED:
79 79 ui.warn(_('(found dead inotify server socket; '
80 80 'removing it)\n'))
81 81 os.unlink(repo.join('inotify.sock'))
82 82 if err[0] in (errno.ECONNREFUSED, errno.ENOENT) and autostart:
83 query = None
84 83 ui.debug(_('(starting inotify server)\n'))
85 84 try:
86 85 try:
87 86 server.start(ui, repo)
88 query = client.query
89 87 except server.AlreadyStartedException, inst:
90 88 # another process may have started its own
91 89 # inotify server while this one was starting.
92 90 ui.debug(str(inst))
93 query = client.query
94 91 except Exception, inst:
95 92 ui.warn(_('could not start inotify server: '
96 93 '%s\n') % inst)
97 if query:
94 else:
95 # server is started, send query again
98 96 try:
99 return query(ui, repo, files, match,
97 return client.query(ui, repo, files, match,
100 98 ignored, clean, unknown)
101 99 except socket.error, err:
102 100 ui.warn(_('could not talk to new inotify '
103 101 'server: %s\n') % err[-1])
104 102 elif err[0] in (errno.ECONNREFUSED, errno.ENOENT):
105 103 # silently ignore normal errors if autostart is False
106 104 ui.debug(_('(inotify server not running)\n'))
107 105 else:
108 106 ui.warn(_('failed to contact inotify server: %s\n')
109 107 % err[-1])
110 108 ui.print_exc()
111 109 # replace by old status function
112 110 self.status = super(inotifydirstate, self).status
113 111
114 112 return super(inotifydirstate, self).status(
115 113 match, ignored, clean, unknown)
116 114
117 115 repo.dirstate.__class__ = inotifydirstate
118 116
119 117 cmdtable = {
120 118 '^inserve':
121 119 (serve,
122 120 [('d', 'daemon', None, _('run server in background')),
123 121 ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
124 122 ('t', 'idle-timeout', '', _('minutes to sit idle before exiting')),
125 123 ('', 'pid-file', '', _('name of file to write process ID to'))],
126 124 _('hg inserve [OPT]...')),
127 125 }
General Comments 0
You need to be logged in to leave comments. Login now