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