hgext/inotify/__init__.py
author Martin Geisler <mg@lazybytes.net>
Sat, 29 Aug 2009 00:29:16 +0200
changeset 9410 1c83938b6a8e
parent 9355 f7c334e810c2
child 9514 7c01599dd340
permissions -rw-r--r--
extensions: load and configure extensions in well-defined phases Extensions are now loaded with a call-graph like this: dispatch._dispatch extensions.loadall extensions.load # add foo module to extensions._extensions extensions.load # add bar module to extensions._extensions foo.uisetup(ui) bar.uisetup(ui) foo.extsetup() bar.extsetup() commands.table.update(foo.cmdtable) commands.table.update(bar.cmdtable) hg.repository foo.reposetup(ui, repo) bar.reposetup(ui, repo) The uisetup calls could easily be moved out to dispatch._dispatch, but have been kept in extensions.loadall since at least TortoiseHg calls extensions.loadall and expects it to call uisetup. The extensions.load function called uisetup. It now has an unused ui argument which has been kept for backwards compatibility.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
# __init__.py - inotify-based status acceleration for Linux
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
#
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8206
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: 8206
diff changeset
     7
# GNU General Public License version 2, incorporated herein by reference.
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
8932
f87884329419 extensions: fix up description lines some more
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8894
diff changeset
     9
'''accelerate status report using Linux's inotify service'''
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
# todo: socket permissions
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
7225
59b4ae211584 i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents: 7219
diff changeset
    13
from mercurial.i18n import _
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
from mercurial import cmdutil, util
8656
284fda4cd093 removed unused imports
Martin Geisler <mg@lazybytes.net>
parents: 8557
diff changeset
    15
import server
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    16
from client import client, QueryFailed
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
def serve(ui, repo, **opts):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
    '''start an inotify server for this repository'''
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    20
    timeout = opts.get('timeout')
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    21
    if timeout:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    22
        timeout = float(timeout) * 1e3
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    23
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8656
diff changeset
    24
    class service(object):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    25
        def init(self):
6995
25619b72f86a inotify: fix traceback when the server has been already started
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    26
            try:
9351
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8947
diff changeset
    27
                self.master = server.master(ui, repo.dirstate,
206f7f4c5c2a inotify: client: no repo use
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8947
diff changeset
    28
                                            repo.root, timeout)
6995
25619b72f86a inotify: fix traceback when the server has been already started
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    29
            except server.AlreadyStartedException, inst:
25619b72f86a inotify: fix traceback when the server has been already started
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6239
diff changeset
    30
                raise util.Abort(str(inst))
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    31
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    32
        def run(self):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    33
            try:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    34
                self.master.run()
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    35
            finally:
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    36
                self.master.shutdown()
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    37
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
    service = service()
8790
72af80052bd9 inotify: add log config option redirect inotify server output to a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
    39
    logfile = ui.config('inotify', 'log')
72af80052bd9 inotify: add log config option redirect inotify server output to a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
    40
    cmdutil.service(opts, initfn=service.init, runfn=service.run,
72af80052bd9 inotify: add log config option redirect inotify server output to a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
    41
                    logfile=logfile)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    42
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    43
def debuginotify(ui, repo, **opts):
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    44
    '''debugging information for inotify extension
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    45
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    46
    Prints the list of directories being watched by the inotify server.
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    47
    '''
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    48
    cli = client(ui, repo)
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    49
    response = cli.debugquery()
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    50
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    51
    ui.write(_('directories being watched:\n'))
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    52
    for path in response:
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    53
        ui.write(('  %s/\n') % path)
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    54
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
def reposetup(ui, repo):
7522
2f4a399a8787 inotify: do not attempt to monkeypatch bundlerepos
Brendan Cully <brendan@kublai.com>
parents: 7452
diff changeset
    56
    if not hasattr(repo, 'dirstate'):
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
        return
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
    class inotifydirstate(repo.dirstate.__class__):
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
8556
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    61
        # We'll set this to false after an unsuccessful attempt so that
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    62
        # next calls of status() within the same instance don't try again
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    63
        # to start an inotify server if it won't start.
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    64
        _inotifyon = True
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    65
6753
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6603
diff changeset
    66
        def status(self, match, ignored, clean, unknown=True):
6603
41eb20cc1c02 match: remove files arg from repo.status and friends
Matt Mackall <mpm@selenic.com>
parents: 6239
diff changeset
    67
            files = match.files()
7393
92c952c4470c inotify: fix status . in repo.root
Brendan Cully <brendan@kublai.com>
parents: 7329
diff changeset
    68
            if '.' in files:
7434
cf7741aa1e96 kill some trailing spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7393
diff changeset
    69
                files = []
8557
67f76a4463ef inotify: Removing the unnecessary "inotifyserver" class variable.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8556
diff changeset
    70
            if self._inotifyon and not ignored:
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    71
                cli = client(ui, repo)
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    72
                try:
8551
7089d9727867 inotify: modular architecture for inotify clients
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8385
diff changeset
    73
                    result = cli.statusquery(files, match, False,
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    74
                                            clean, unknown)
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    75
                except QueryFailed, instr:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    76
                    ui.debug(str(instr))
8556
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    77
                    # don't retry within the same hg instance
f5fae700cc00 inotify: set a flag so a failed inotify query doesn't get repeated.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8555
diff changeset
    78
                    inotifydirstate._inotifyon = False
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    79
                    pass
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    80
                else:
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    81
                    if ui.config('inotify', 'debug'):
7219
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    82
                        r2 = super(inotifydirstate, self).status(
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    83
                            match, False, clean, unknown)
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    84
                        for c,a,b in zip('LMARDUIC', result, r2):
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    85
                            for f in a:
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    86
                                if f not in b:
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    87
                                    ui.warn('*** inotify: %s +%s\n' % (c, f))
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    88
                            for f in b:
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    89
                                if f not in a:
7304
68374f1c8c87 inotify: fix bug in formatting
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7225
diff changeset
    90
                                    ui.warn('*** inotify: %s -%s\n' % (c, f))
7219
1f6d2e487135 inotify: add debugging mode to inotify
Matt Mackall <mpm@selenic.com>
parents: 7218
diff changeset
    91
                        result = r2
8552
06561793778e inotify: Separate query sending logic from Server starting.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8551
diff changeset
    92
                    return result
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    93
            return super(inotifydirstate, self).status(
6753
ed5ffb2c12f3 repo.status: eliminate list_
Matt Mackall <mpm@selenic.com>
parents: 6603
diff changeset
    94
                match, ignored, clean, unknown)
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    95
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    96
    repo.dirstate.__class__ = inotifydirstate
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    97
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    98
cmdtable = {
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
    99
    'debuginotify':
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   100
        (debuginotify, [], ('hg debuginotify')),
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   101
    '^inserve':
8555
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   102
        (serve,
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   103
         [('d', 'daemon', None, _('run server in background')),
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   104
          ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   105
          ('t', 'idle-timeout', '', _('minutes to sit idle before exiting')),
3e09bc5fee12 inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8552
diff changeset
   106
          ('', 'pid-file', '', _('name of file to write process ID to'))],
8947
9cda78218ab3 inotify: OPT -> OPTION in cmdline help string
Martin Geisler <mg@lazybytes.net>
parents: 8932
diff changeset
   107
         _('hg inserve [OPTION]...')),
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   108
    }