]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/python/hgext/inotify/__init__.py
added factotum support for python and hg
[plan9front.git] / sys / lib / python / hgext / inotify / __init__.py
1 # __init__.py - inotify-based status acceleration for Linux
2 #
3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com>
4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com>
5 #
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2, incorporated herein by reference.
8
9 '''accelerate status report using Linux's inotify service'''
10
11 # todo: socket permissions
12
13 from mercurial.i18n import _
14 from mercurial import cmdutil, util
15 import server
16 from weakref import proxy
17 from client import client, QueryFailed
18
19 def serve(ui, repo, **opts):
20     '''start an inotify server for this repository'''
21     timeout = opts.get('timeout')
22     if timeout:
23         timeout = float(timeout) * 1e3
24
25     class service(object):
26         def init(self):
27             try:
28                 self.master = server.master(ui, repo.dirstate,
29                                             repo.root, timeout)
30             except server.AlreadyStartedException, inst:
31                 raise util.Abort(str(inst))
32
33         def run(self):
34             try:
35                 self.master.run()
36             finally:
37                 self.master.shutdown()
38
39     service = service()
40     logfile = ui.config('inotify', 'log')
41     cmdutil.service(opts, initfn=service.init, runfn=service.run,
42                     logfile=logfile)
43
44 def debuginotify(ui, repo, **opts):
45     '''debugging information for inotify extension
46
47     Prints the list of directories being watched by the inotify server.
48     '''
49     cli = client(ui, repo)
50     response = cli.debugquery()
51
52     ui.write(_('directories being watched:\n'))
53     for path in response:
54         ui.write(('  %s/\n') % path)
55
56 def reposetup(ui, repo):
57     if not hasattr(repo, 'dirstate'):
58         return
59
60     class inotifydirstate(repo.dirstate.__class__):
61
62         # We'll set this to false after an unsuccessful attempt so that
63         # next calls of status() within the same instance don't try again
64         # to start an inotify server if it won't start.
65         _inotifyon = True
66
67         def status(self, match, ignored, clean, unknown=True):
68             files = match.files()
69             if '.' in files:
70                 files = []
71             if self._inotifyon and not ignored:
72                 cli = client(ui, repo)
73                 try:
74                     result = cli.statusquery(files, match, False,
75                                             clean, unknown)
76                 except QueryFailed, instr:
77                     ui.debug(str(instr))
78                     # don't retry within the same hg instance
79                     inotifydirstate._inotifyon = False
80                     pass
81                 else:
82                     if ui.config('inotify', 'debug'):
83                         r2 = super(inotifydirstate, self).status(
84                             match, False, clean, unknown)
85                         for c,a,b in zip('LMARDUIC', result, r2):
86                             for f in a:
87                                 if f not in b:
88                                     ui.warn('*** inotify: %s +%s\n' % (c, f))
89                             for f in b:
90                                 if f not in a:
91                                     ui.warn('*** inotify: %s -%s\n' % (c, f))
92                         result = r2
93                     return result
94             return super(inotifydirstate, self).status(
95                 match, ignored, clean, unknown)
96
97     repo.dirstate.__class__ = inotifydirstate
98
99 cmdtable = {
100     'debuginotify':
101         (debuginotify, [], ('hg debuginotify')),
102     '^inserve':
103         (serve,
104          [('d', 'daemon', None, _('run server in background')),
105           ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
106           ('t', 'idle-timeout', '', _('minutes to sit idle before exiting')),
107           ('', 'pid-file', '', _('name of file to write process ID to'))],
108          _('hg inserve [OPTION]...')),
109     }