sapling/eden/scm/edenscm/mercurial/sshserver.py

153 lines
4.5 KiB
Python
Raw Normal View History

# Portions Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
2006-06-04 21:32:13 +04:00
# sshserver.py - ssh protocol server support for mercurial
2006-06-04 21:26:05 +04:00
#
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
2006-08-12 23:30:02 +04:00
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
2006-06-04 21:26:05 +04:00
#
# This software may be used and distributed according to the terms of the
2010-01-20 07:20:08 +03:00
# GNU General Public License version 2 or any later version.
2006-06-04 21:26:05 +04:00
2015-08-09 05:55:39 +03:00
from __future__ import absolute_import
import os
import socket
2015-08-09 05:55:39 +03:00
import sys
from . import encoding, error, hook, util, wireproto
from .i18n import _
from .pycompat import decodeutf8, range
flake8: enable F821 check Summary: This check is useful and detects real errors (ex. fbconduit). Unfortunately `arc lint` will run it with both py2 and py3 so a lot of py2 builtins will still be warned. I didn't find a clean way to disable py3 check. So this diff tries to fix them. For `xrange`, the change was done by a script: ``` import sys import redbaron headertypes = {'comment', 'endl', 'from_import', 'import', 'string', 'assignment', 'atomtrailers'} xrangefix = '''try: xrange(0) except NameError: xrange = range ''' def isxrange(x): try: return x[0].value == 'xrange' except Exception: return False def main(argv): for i, path in enumerate(argv): print('(%d/%d) scanning %s' % (i + 1, len(argv), path)) content = open(path).read() try: red = redbaron.RedBaron(content) except Exception: print(' warning: failed to parse') continue hasxrange = red.find('atomtrailersnode', value=isxrange) hasxrangefix = 'xrange = range' in content if hasxrangefix or not hasxrange: print(' no need to change') continue # find a place to insert the compatibility statement changed = False for node in red: if node.type in headertypes: continue # node.insert_before is an easier API, but it has bugs changing # other "finally" and "except" positions. So do the insert # manually. # # node.insert_before(xrangefix) line = node.absolute_bounding_box.top_left.line - 1 lines = content.splitlines(1) content = ''.join(lines[:line]) + xrangefix + ''.join(lines[line:]) changed = True break if changed: # "content" is faster than "red.dumps()" open(path, 'w').write(content) print(' updated') if __name__ == "__main__": sys.exit(main(sys.argv[1:])) ``` For other py2 builtins that do not have a py3 equivalent, some `# noqa` were added as a workaround for now. Reviewed By: DurhamG Differential Revision: D6934535 fbshipit-source-id: 546b62830af144bc8b46788d2e0fd00496838939
2018-02-10 04:31:44 +03:00
class sshserver(wireproto.abstractserverproto):
2006-06-04 21:26:05 +04:00
def __init__(self, ui, repo):
self.ui = ui
self.repo = repo
self.lock = None
self.fin = ui.fin
self.fout = ui.fout
self.name = "ssh"
2006-06-04 21:26:05 +04:00
hook.redirect(True)
ui.fout = repo.ui.fout = ui.ferr
2006-06-04 21:26:05 +04:00
# Prevent insertion/deletion of CRs
2011-05-06 17:25:35 +04:00
util.setbinary(self.fin)
util.setbinary(self.fout)
2006-06-04 21:26:05 +04:00
def getargs(self, args):
data = {}
keys = args.split()
for n in range(len(keys)):
argline = decodeutf8(self.fin.readline()[:-1])
arg, l = argline.split()
if arg not in keys:
raise error.Abort(_("unexpected parameter %r") % arg)
if arg == "*":
star = {}
for k in range(int(l)):
argline = decodeutf8(self.fin.readline()[:-1])
arg, l = argline.split()
val = decodeutf8(self.fin.read(int(l)))
star[arg] = val
data["*"] = star
else:
val = decodeutf8(self.fin.read(int(l)))
data[arg] = val
return [data[k] for k in keys]
def getarg(self, name):
return self.getargs(name)[0]
2006-06-04 21:26:05 +04:00
def getfile(self, fpout):
self.sendresponse("")
count = int(self.fin.readline())
while count:
fpout.write(self.fin.read(count))
count = int(self.fin.readline())
def redirect(self):
pass
def sendresponse(self, v):
self.fout.write("%d\n" % len(v))
self.fout.write(v)
self.fout.flush()
2010-07-15 01:19:27 +04:00
def sendstream(self, source):
write = self.fout.write
if source.reader:
gen = iter(lambda: source.reader.read(4096), "")
else:
gen = source.gen
for chunk in gen:
write(chunk)
2010-07-15 01:19:27 +04:00
self.fout.flush()
def sendpushresponse(self, rsp):
self.sendresponse("")
self.sendresponse(str(rsp.res))
def sendpusherror(self, rsp):
self.sendresponse(rsp.res)
def sendooberror(self, rsp):
self.ui.ferr.write("%s\n-\n" % rsp.message)
self.ui.ferr.flush()
self.fout.write("\n")
self.fout.flush()
2006-06-04 21:26:05 +04:00
def serve_forever(self):
try:
2010-01-25 09:05:27 +03:00
while self.serve_one():
pass
finally:
if self.lock is not None:
self.lock.release()
2006-06-04 21:26:05 +04:00
sys.exit(0)
handlers = {
str: sendresponse,
wireproto.streamres: sendstream,
wireproto.pushres: sendpushresponse,
wireproto.pusherr: sendpusherror,
wireproto.ooberror: sendooberror,
}
2006-06-04 21:26:05 +04:00
def serve_one(self):
cmd = self.fin.readline()[:-1]
if cmd:
if util.safehasattr(util, "setprocname"):
client = encoding.environ.get("SSH_CLIENT", "").split(" ")[0]
# Resolve IP to hostname
try:
client = socket.gethostbyaddr(client)[0]
except (socket.error, IndexError):
pass
reponame = os.path.basename(self.repo.root)
title = "hg serve (%s)" % " ".join(
filter(None, [reponame, cmd, client])
)
util.setprocname(title)
if cmd in wireproto.commands:
rsp = wireproto.dispatch(self.repo, self, cmd)
self.handlers[rsp.__class__](self, rsp)
else:
impl = getattr(self, "do_" + cmd, None)
if impl:
r = impl()
if r is not None:
self.sendresponse(r)
else:
self.sendresponse("")
return cmd != ""
2006-06-04 21:26:05 +04:00
def _client(self):
client = encoding.environ.get("SSH_CLIENT", "").split(" ", 1)[0]
return "remote:ssh:" + client