sshserver.py
143 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
/ mercurial / sshserver.py
|
r2399 | # sshserver.py - ssh protocol server support for mercurial | ||
|
r2396 | # | ||
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
|
r2859 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||
|
r2396 | # | ||
|
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. | ||
|
r2396 | |||
|
r12337 | import util, hook, wireproto, changegroup | ||
|
r11596 | import os, sys | ||
|
r2396 | |||
class sshserver(object): | ||||
def __init__(self, ui, repo): | ||||
self.ui = ui | ||||
self.repo = repo | ||||
self.lock = None | ||||
self.fin = sys.stdin | ||||
self.fout = sys.stdout | ||||
|
r5833 | hook.redirect(True) | ||
|
r2396 | sys.stdout = sys.stderr | ||
# Prevent insertion/deletion of CRs | ||||
util.set_binary(self.fin) | ||||
util.set_binary(self.fout) | ||||
|
r11579 | def getargs(self, args): | ||
data = {} | ||||
keys = args.split() | ||||
for n in xrange(len(keys)): | ||||
argline = self.fin.readline()[:-1] | ||||
arg, l = argline.split() | ||||
val = self.fin.read(int(l)) | ||||
if arg not in keys: | ||||
raise util.Abort("unexpected parameter %r" % arg) | ||||
if arg == '*': | ||||
star = {} | ||||
for n in xrange(int(l)): | ||||
arg, l = argline.split() | ||||
val = self.fin.read(int(l)) | ||||
star[arg] = val | ||||
data['*'] = star | ||||
else: | ||||
data[arg] = val | ||||
return [data[k] for k in keys] | ||||
def getarg(self, name): | ||||
return self.getargs(name)[0] | ||||
|
r2396 | |||
|
r11621 | def getfile(self, fpout): | ||
|
r11622 | self.sendresponse('') | ||
|
r11621 | count = int(self.fin.readline()) | ||
while count: | ||||
fpout.write(self.fin.read(count)) | ||||
count = int(self.fin.readline()) | ||||
def redirect(self): | ||||
pass | ||||
|
r11623 | def groupchunks(self, changegroup): | ||
|
r11584 | while True: | ||
d = changegroup.read(4096) | ||||
if not d: | ||||
break | ||||
|
r11623 | yield d | ||
|
r11584 | |||
|
r11623 | def sendresponse(self, v): | ||
self.fout.write("%d\n" % len(v)) | ||||
self.fout.write(v) | ||||
|
r11584 | self.fout.flush() | ||
|
r11585 | def sendstream(self, source): | ||
|
r11625 | for chunk in source.gen: | ||
|
r11585 | self.fout.write(chunk) | ||
self.fout.flush() | ||||
|
r11625 | def sendpushresponse(self, rsp): | ||
|
r11622 | self.sendresponse('') | ||
|
r11625 | self.sendresponse(str(rsp.res)) | ||
|
r11593 | |||
|
r12703 | def sendpusherror(self, rsp): | ||
self.sendresponse(rsp.res) | ||||
|
r2396 | def serve_forever(self): | ||
|
r8109 | try: | ||
|
r10282 | while self.serve_one(): | ||
pass | ||||
|
r8109 | finally: | ||
if self.lock is not None: | ||||
self.lock.release() | ||||
|
r2396 | sys.exit(0) | ||
|
r11625 | handlers = { | ||
str: sendresponse, | ||||
wireproto.streamres: sendstream, | ||||
wireproto.pushres: sendpushresponse, | ||||
|
r12703 | wireproto.pusherr: sendpusherror, | ||
|
r11625 | } | ||
|
r2396 | def serve_one(self): | ||
cmd = self.fin.readline()[:-1] | ||||
|
r11618 | if cmd and cmd in wireproto.commands: | ||
|
r11625 | rsp = wireproto.dispatch(self.repo, self, cmd) | ||
self.handlers[rsp.__class__](self, rsp) | ||||
|
r11618 | elif cmd: | ||
|
r2396 | impl = getattr(self, 'do_' + cmd, None) | ||
|
r10282 | if impl: | ||
|
r11580 | r = impl() | ||
if r is not None: | ||||
|
r11622 | self.sendresponse(r) | ||
else: self.sendresponse("") | ||||
|
r2396 | return cmd != '' | ||
def do_lock(self): | ||||
|
r2439 | '''DEPRECATED - allowing remote client to lock repo is not safe''' | ||
|
r2396 | self.lock = self.repo.lock() | ||
|
r11580 | return "" | ||
|
r2396 | |||
def do_unlock(self): | ||||
|
r2439 | '''DEPRECATED''' | ||
|
r2396 | if self.lock: | ||
self.lock.release() | ||||
self.lock = None | ||||
|
r11580 | return "" | ||
|
r2396 | |||
def do_addchangegroup(self): | ||||
|
r2439 | '''DEPRECATED''' | ||
|
r2396 | if not self.lock: | ||
|
r11622 | self.sendresponse("not locked") | ||
|
r2396 | return | ||
|
r11622 | self.sendresponse("") | ||
|
r12337 | cg = changegroup.unbundle10(self.fin, "UN") | ||
r = self.repo.addchangegroup(cg, 'serve', self._client(), | ||||
|
r11442 | lock=self.lock) | ||
|
r11580 | return str(r) | ||
|
r2439 | |||
|
r11593 | def _client(self): | ||
|
r2673 | client = os.environ.get('SSH_CLIENT', '').split(' ', 1)[0] | ||
return 'remote:ssh:' + client | ||||