sshrepo.py
214 lines
| 6.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / sshrepo.py
|
r1096 | # sshrepo.py - ssh repository proxy class for mercurial | ||
|
r1089 | # | ||
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
|
r1089 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r1089 | |||
|
r3891 | from i18n import _ | ||
|
r14076 | import util, error, wireproto | ||
|
r1089 | |||
|
r6313 | class remotelock(object): | ||
def __init__(self, repo): | ||||
self.repo = repo | ||||
def release(self): | ||||
self.repo.unlock() | ||||
self.repo = None | ||||
def __del__(self): | ||||
if self.repo: | ||||
self.release() | ||||
|
r11586 | class sshrepository(wireproto.wirerepository): | ||
|
r2549 | def __init__(self, ui, path, create=0): | ||
|
r2673 | self._url = path | ||
|
r1089 | self.ui = ui | ||
|
r14076 | u = util.url(path, parsequery=False, parsefragment=False) | ||
|
r13819 | if u.scheme != 'ssh' or not u.host or u.path is None: | ||
|
r11590 | self._abort(error.RepoError(_("couldn't parse location %s") % path)) | ||
|
r1089 | |||
|
r13819 | self.user = u.user | ||
if u.passwd is not None: | ||||
|
r13464 | self._abort(error.RepoError(_("password in URL not supported"))) | ||
|
r13819 | self.host = u.host | ||
self.port = u.port | ||||
self.path = u.path or "." | ||||
|
r1089 | |||
sshcmd = self.ui.config("ui", "ssh", "ssh") | ||||
remotecmd = self.ui.config("ui", "remotecmd", "hg") | ||||
|
r2549 | |||
|
r5644 | args = util.sshargs(sshcmd, self.host, self.user, self.port) | ||
|
r2549 | if create: | ||
cmd = '%s %s "%s init %s"' | ||||
cmd = cmd % (sshcmd, args, remotecmd, self.path) | ||||
|
r6953 | ui.note(_('running %s\n') % cmd) | ||
|
r5292 | res = util.system(cmd) | ||
|
r2549 | if res != 0: | ||
|
r11590 | self._abort(error.RepoError(_("could not create remote repo"))) | ||
|
r2549 | |||
self.validate_repo(ui, sshcmd, args, remotecmd) | ||||
|
r2673 | def url(self): | ||
return self._url | ||||
|
r2549 | def validate_repo(self, ui, sshcmd, args, remotecmd): | ||
|
r3034 | # cleanup up previous run | ||
self.cleanup() | ||||
|
r1330 | cmd = '%s %s "%s -R %s serve --stdio"' | ||
|
r1089 | cmd = cmd % (sshcmd, args, remotecmd, self.path) | ||
|
r5292 | cmd = util.quotecommand(cmd) | ||
|
r6953 | ui.note(_('running %s\n') % cmd) | ||
|
r8339 | self.pipeo, self.pipei, self.pipee = util.popen3(cmd) | ||
|
r1089 | |||
|
r2028 | # skip any noise generated by remote shell | ||
|
r11589 | self._callstream("hello") | ||
r = self._callstream("between", pairs=("%s-%s" % ("0"*40, "0"*40))) | ||||
|
r2420 | lines = ["", "dummy"] | ||
|
r2046 | max_noise = 500 | ||
|
r2420 | while lines[-1] and max_noise: | ||
l = r.readline() | ||||
|
r2028 | self.readerr() | ||
|
r2420 | if lines[-1] == "1\n" and l == "\n": | ||
|
r2028 | break | ||
|
r2420 | if l: | ||
|
r9467 | ui.debug("remote: ", l) | ||
|
r2420 | lines.append(l) | ||
|
r2040 | max_noise -= 1 | ||
else: | ||||
|
r11590 | self._abort(error.RepoError(_("no suitable response from remote hg"))) | ||
|
r2028 | |||
|
r8150 | self.capabilities = set() | ||
|
r8210 | for l in reversed(lines): | ||
|
r2421 | if l.startswith("capabilities:"): | ||
|
r5258 | self.capabilities.update(l[:-1].split(":")[1].split()) | ||
|
r2421 | break | ||
|
r1089 | def readerr(self): | ||
while 1: | ||||
|
r2176 | size = util.fstat(self.pipee).st_size | ||
|
r10282 | if size == 0: | ||
break | ||||
|
r13084 | s = self.pipee.read(size) | ||
if not s: | ||||
|
r10282 | break | ||
|
r13084 | for l in s.splitlines(): | ||
self.ui.status(_("remote: "), l, '\n') | ||||
|
r1089 | |||
|
r11586 | def _abort(self, exception): | ||
self.cleanup() | ||||
raise exception | ||||
|
r3034 | def cleanup(self): | ||
|
r1089 | try: | ||
self.pipeo.close() | ||||
self.pipei.close() | ||||
|
r1358 | # read the error descriptor until EOF | ||
for l in self.pipee: | ||||
|
r1402 | self.ui.status(_("remote: "), l) | ||
|
r1089 | self.pipee.close() | ||
except: | ||||
pass | ||||
|
r3034 | __del__ = cleanup | ||
|
r11589 | def _callstream(self, cmd, **args): | ||
|
r9467 | self.ui.debug("sending %s command\n" % cmd) | ||
|
r1089 | self.pipeo.write("%s\n" % cmd) | ||
|
r13721 | _func, names = wireproto.commands[cmd] | ||
keys = names.split() | ||||
wireargs = {} | ||||
for k in keys: | ||||
if k == '*': | ||||
wireargs['*'] = args | ||||
break | ||||
else: | ||||
wireargs[k] = args[k] | ||||
del args[k] | ||||
for k, v in sorted(wireargs.iteritems()): | ||||
|
r1089 | self.pipeo.write("%s %d\n" % (k, len(v))) | ||
|
r13721 | if isinstance(v, dict): | ||
for dk, dv in v.iteritems(): | ||||
self.pipeo.write("%s %d\n" % (dk, len(dv))) | ||||
self.pipeo.write(dv) | ||||
else: | ||||
self.pipeo.write(v) | ||||
|
r1089 | self.pipeo.flush() | ||
return self.pipei | ||||
|
r11589 | def _call(self, cmd, **args): | ||
self._callstream(cmd, **args) | ||||
|
r5978 | return self._recv() | ||
|
r11592 | def _callpush(self, cmd, fp, **args): | ||
r = self._call(cmd, **args) | ||||
if r: | ||||
return '', r | ||||
while 1: | ||||
d = fp.read(4096) | ||||
if not d: | ||||
break | ||||
self._send(d) | ||||
self._send("", flush=True) | ||||
r = self._recv() | ||||
if r: | ||||
return '', r | ||||
return self._recv(), '' | ||||
|
r11591 | def _decompress(self, stream): | ||
return stream | ||||
|
r5978 | def _recv(self): | ||
l = self.pipei.readline() | ||||
|
r1089 | self.readerr() | ||
try: | ||||
l = int(l) | ||||
|
r14004 | except ValueError: | ||
|
r11590 | self._abort(error.ResponseError(_("unexpected response:"), l)) | ||
|
r5978 | return self.pipei.read(l) | ||
def _send(self, data, flush=False): | ||||
self.pipeo.write("%d\n" % len(data)) | ||||
if data: | ||||
self.pipeo.write(data) | ||||
if flush: | ||||
self.pipeo.flush() | ||||
self.readerr() | ||||
|
r1089 | |||
def lock(self): | ||||
|
r11589 | self._call("lock") | ||
|
r1089 | return remotelock(self) | ||
def unlock(self): | ||||
|
r11589 | self._call("unlock") | ||
|
r1089 | |||
|
r2673 | def addchangegroup(self, cg, source, url): | ||
|
r11153 | '''Send a changegroup to the remote server. Return an integer | ||
similar to unbundle(). DEPRECATED, since it requires locking the | ||||
remote.''' | ||||
|
r11589 | d = self._call("addchangegroup") | ||
|
r1089 | if d: | ||
|
r11590 | self._abort(error.RepoError(_("push refused: %s") % d)) | ||
|
r1089 | while 1: | ||
d = cg.read(4096) | ||||
|
r5978 | if not d: | ||
break | ||||
|
r1089 | self.pipeo.write(d) | ||
self.readerr() | ||||
self.pipeo.flush() | ||||
self.readerr() | ||||
|
r5978 | r = self._recv() | ||
|
r2019 | if not r: | ||
return 1 | ||||
|
r5978 | try: | ||
return int(r) | ||||
|
r14004 | except ValueError: | ||
|
r11590 | self._abort(error.ResponseError(_("unexpected response:"), r)) | ||
|
r2612 | |||
|
r2740 | instance = sshrepository | ||