client.py
171 lines
| 5.8 KiB
| text/x-python
|
PythonLexer
|
r6239 | # client.py - inotify status client | ||
# | ||||
# Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | ||||
# Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | ||||
|
r8551 | # Copyright 2009 Nicolas Dumazet <nicdumz@gmail.com> | ||
|
r6239 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
# GNU General Public License version 2, incorporated herein by reference. | ||||
|
r6239 | |||
|
r7225 | from mercurial.i18n import _ | ||
|
r8552 | import common, server | ||
import errno, os, socket, struct | ||||
class QueryFailed(Exception): pass | ||||
def start_server(function): | ||||
""" | ||||
Decorator. | ||||
Tries to call function, if it fails, try to (re)start inotify server. | ||||
Raise QueryFailed if something went wrong | ||||
""" | ||||
def decorated_function(self, *args): | ||||
result = None | ||||
try: | ||||
return function(self, *args) | ||||
except (OSError, socket.error), err: | ||||
autostart = self.ui.configbool('inotify', 'autostart', True) | ||||
if err[0] == errno.ECONNREFUSED: | ||||
|
r9900 | self.ui.warn(_('inotify-client: found dead inotify server ' | ||
'socket; removing it\n')) | ||||
|
r9351 | os.unlink(os.path.join(self.root, '.hg', 'inotify.sock')) | ||
|
r8552 | if err[0] in (errno.ECONNREFUSED, errno.ENOENT) and autostart: | ||
|
r9467 | self.ui.debug('(starting inotify server)\n') | ||
|
r8552 | try: | ||
try: | ||||
|
r9514 | server.start(self.ui, self.dirstate, self.root, | ||
dict(daemon=True, daemon_pipefds='')) | ||||
|
r8552 | except server.AlreadyStartedException, inst: | ||
# another process may have started its own | ||||
# inotify server while this one was starting. | ||||
self.ui.debug(str(inst)) | ||||
except Exception, inst: | ||||
|
r9900 | self.ui.warn(_('inotify-client: could not start inotify ' | ||
'server: %s\n') % inst) | ||||
|
r8552 | else: | ||
try: | ||||
return function(self, *args) | ||||
except socket.error, err: | ||||
|
r9900 | self.ui.warn(_('inotify-client: could not talk to new ' | ||
'inotify server: %s\n') % err[-1]) | ||||
|
r8552 | elif err[0] in (errno.ECONNREFUSED, errno.ENOENT): | ||
# silently ignore normal errors if autostart is False | ||||
|
r9467 | self.ui.debug('(inotify server not running)\n') | ||
|
r8552 | else: | ||
|
r9900 | self.ui.warn(_('inotify-client: failed to contact inotify ' | ||
'server: %s\n') % err[-1]) | ||||
|
r8552 | |||
self.ui.traceback() | ||||
raise QueryFailed('inotify query failed') | ||||
return decorated_function | ||||
|
r6239 | |||
|
r8551 | class client(object): | ||
def __init__(self, ui, repo): | ||||
self.ui = ui | ||||
|
r9351 | self.dirstate = repo.dirstate | ||
self.root = repo.root | ||||
|
r8551 | self.sock = socket.socket(socket.AF_UNIX) | ||
def _connect(self): | ||||
|
r9351 | sockpath = os.path.join(self.root, '.hg', 'inotify.sock') | ||
|
r8551 | try: | ||
self.sock.connect(sockpath) | ||||
except socket.error, err: | ||||
if err[0] == "AF_UNIX path too long": | ||||
sockpath = os.readlink(sockpath) | ||||
self.sock.connect(sockpath) | ||||
else: | ||||
raise | ||||
|
r6239 | |||
|
r8553 | def _send(self, type, data): | ||
|
r8551 | """Sends protocol version number, and the data""" | ||
|
r8553 | self.sock.sendall(chr(common.version) + type + data) | ||
|
r8551 | |||
self.sock.shutdown(socket.SHUT_WR) | ||||
|
r6239 | |||
|
r8553 | def _receive(self, type): | ||
|
r8551 | """ | ||
Read data, check version number, extract headers, | ||||
and returns a tuple (data descriptor, header) | ||||
|
r8552 | Raises QueryFailed on error | ||
|
r8551 | """ | ||
cs = common.recvcs(self.sock) | ||||
|
r8788 | try: | ||
version = ord(cs.read(1)) | ||||
except TypeError: | ||||
# empty answer, assume the server crashed | ||||
|
r9900 | self.ui.warn(_('inotify-client: received empty answer from inotify ' | ||
'server')) | ||||
|
r8788 | raise QueryFailed('server crashed') | ||
|
r8551 | if version != common.version: | ||
self.ui.warn(_('(inotify: received response from incompatible ' | ||||
'server version %d)\n') % version) | ||||
|
r8552 | raise QueryFailed('incompatible server version') | ||
|
r6239 | |||
|
r8553 | readtype = cs.read(4) | ||
if readtype != type: | ||||
self.ui.warn(_('(inotify: received \'%s\' response when expecting' | ||||
' \'%s\')\n') % (readtype, type)) | ||||
raise QueryFailed('wrong response type') | ||||
|
r8551 | hdrfmt = common.resphdrfmts[type] | ||
hdrsize = common.resphdrsizes[type] | ||||
try: | ||||
resphdr = struct.unpack(hdrfmt, cs.read(hdrsize)) | ||||
except struct.error: | ||||
|
r8552 | raise QueryFailed('unable to retrieve query response headers') | ||
|
r6239 | |||
|
r8551 | return cs, resphdr | ||
|
r8553 | def query(self, type, req): | ||
|
r8551 | self._connect() | ||
|
r6239 | |||
|
r8553 | self._send(type, req) | ||
|
r8551 | |||
|
r8553 | return self._receive(type) | ||
|
r8551 | |||
|
r8552 | @start_server | ||
|
r8551 | def statusquery(self, names, match, ignored, clean, unknown=True): | ||
|
r6239 | |||
|
r8551 | def genquery(): | ||
for n in names: | ||||
yield n | ||||
states = 'almrx!' | ||||
if ignored: | ||||
raise ValueError('this is insanity') | ||||
if clean: states += 'c' | ||||
if unknown: states += '?' | ||||
yield states | ||||
req = '\0'.join(genquery()) | ||||
|
r6239 | |||
|
r8553 | cs, resphdr = self.query('STAT', req) | ||
|
r8551 | |||
def readnames(nbytes): | ||||
if nbytes: | ||||
names = cs.read(nbytes) | ||||
if names: | ||||
return filter(match, names.split('\0')) | ||||
return [] | ||||
|
r9854 | results = map(readnames, resphdr[:-1]) | ||
if names: | ||||
nbytes = resphdr[-1] | ||||
vdirs = cs.read(nbytes) | ||||
if vdirs: | ||||
for vdir in vdirs.split('\0'): | ||||
match.dir(vdir) | ||||
return results | ||||
|
r8551 | |||
|
r8555 | @start_server | ||
def debugquery(self): | ||||
cs, resphdr = self.query('DBUG', '') | ||||
nbytes = resphdr[0] | ||||
names = cs.read(nbytes) | ||||
return names.split('\0') | ||||