__init__.py
108 lines
| 4.0 KiB
| text/x-python
|
PythonLexer
Bryan O'Sullivan
|
r6239 | # __init__.py - inotify-based status acceleration for Linux | ||
# | ||||
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | ||||
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
'''inotify-based status acceleration for Linux systems | ||||
''' | ||||
# todo: socket permissions | ||||
from mercurial.i18n import gettext as _ | ||||
from mercurial import cmdutil, util | ||||
import client, errno, os, server, socket | ||||
from weakref import proxy | ||||
def serve(ui, repo, **opts): | ||||
'''start an inotify server for this repository''' | ||||
timeout = opts.get('timeout') | ||||
if timeout: | ||||
timeout = float(timeout) * 1e3 | ||||
class service: | ||||
def init(self): | ||||
Benoit Boissinot
|
r6995 | try: | ||
self.master = server.Master(ui, repo, timeout) | ||||
except server.AlreadyStartedException, inst: | ||||
raise util.Abort(str(inst)) | ||||
Bryan O'Sullivan
|
r6239 | |||
def run(self): | ||||
try: | ||||
self.master.run() | ||||
finally: | ||||
self.master.shutdown() | ||||
service = service() | ||||
cmdutil.service(opts, initfn=service.init, runfn=service.run) | ||||
def reposetup(ui, repo): | ||||
if not repo.local(): | ||||
return | ||||
# XXX: weakref until hg stops relying on __del__ | ||||
repo = proxy(repo) | ||||
class inotifydirstate(repo.dirstate.__class__): | ||||
# Set to True if we're the inotify server, so we don't attempt | ||||
# to recurse. | ||||
inotifyserver = False | ||||
Matt Mackall
|
r6753 | def status(self, match, ignored, clean, unknown=True): | ||
Matt Mackall
|
r6603 | files = match.files() | ||
Bryan O'Sullivan
|
r6239 | try: | ||
Matt Mackall
|
r6753 | if not ignored and not self.inotifyserver: | ||
Bryan O'Sullivan
|
r6239 | result = client.query(ui, repo, files, match, False, | ||
Matt Mackall
|
r6753 | clean, unknown) | ||
Bryan O'Sullivan
|
r6239 | if result is not None: | ||
return result | ||||
Benoit Boissinot
|
r6997 | except (OSError, socket.error), err: | ||
Bryan O'Sullivan
|
r6239 | if err[0] == errno.ECONNREFUSED: | ||
ui.warn(_('(found dead inotify server socket; ' | ||||
'removing it)\n')) | ||||
os.unlink(repo.join('inotify.sock')) | ||||
Benoit Boissinot
|
r6996 | if err[0] in (errno.ECONNREFUSED, errno.ENOENT) and \ | ||
ui.configbool('inotify', 'autostart'): | ||||
Bryan O'Sullivan
|
r6239 | query = None | ||
ui.debug(_('(starting inotify server)\n')) | ||||
try: | ||||
server.start(ui, repo) | ||||
query = client.query | ||||
except server.AlreadyStartedException, inst: | ||||
# another process may have started its own | ||||
# inotify server while this one was starting. | ||||
ui.debug(str(inst)) | ||||
query = client.query | ||||
except Exception, inst: | ||||
ui.warn(_('could not start inotify server: ' | ||||
'%s\n') % inst) | ||||
if query: | ||||
try: | ||||
return query(ui, repo, files or [], match, | ||||
Matt Mackall
|
r6753 | ignored, clean, unknown) | ||
Bryan O'Sullivan
|
r6239 | except socket.error, err: | ||
ui.warn(_('could not talk to new inotify ' | ||||
Benoit Boissinot
|
r6996 | 'server: %s\n') % err[-1]) | ||
else: | ||||
ui.warn(_('failed to contact inotify server: %s\n') | ||||
% err[-1]) | ||||
ui.print_exc() | ||||
# replace by old status function | ||||
self.status = super(inotifydirstate, self).status | ||||
Bryan O'Sullivan
|
r6239 | |||
return super(inotifydirstate, self).status( | ||||
Matt Mackall
|
r6753 | match, ignored, clean, unknown) | ||
Bryan O'Sullivan
|
r6239 | |||
repo.dirstate.__class__ = inotifydirstate | ||||
cmdtable = { | ||||
'^inserve': | ||||
(serve, | ||||
[('d', 'daemon', None, _('run server in background')), | ||||
('', 'daemon-pipefds', '', _('used internally by daemon mode')), | ||||
('t', 'idle-timeout', '', _('minutes to sit idle before exiting')), | ||||
('', 'pid-file', '', _('name of file to write process ID to'))], | ||||
_('hg inserve [OPT]...')), | ||||
} | ||||