2019-11-01 22:33:28 +03:00
|
|
|
# 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
|
|
|
#
|
2007-06-19 10:51:34 +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
|
|
|
#
|
2009-04-26 03:08:54 +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
|
|
|
|
|
2018-10-28 01:10:26 +03:00
|
|
|
import os
|
|
|
|
import socket
|
2015-08-09 05:55:39 +03:00
|
|
|
import sys
|
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
from . import encoding, error, hook, util, wireproto
|
2016-06-14 12:53:55 +03:00
|
|
|
from .i18n import _
|
2020-02-01 05:36:25 +03:00
|
|
|
from .pycompat import decodeutf8, encodeutf8, 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
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
2014-03-28 22:10:33 +04: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
|
2020-02-04 05:24:33 +03:00
|
|
|
self.fin = ui.fin
|
|
|
|
self.fout = ui.fout
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
self.name = "ssh"
|
2006-06-04 21:26:05 +04:00
|
|
|
|
2008-01-11 22:06:38 +03:00
|
|
|
hook.redirect(True)
|
2011-06-08 02:39:20 +04:00
|
|
|
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
|
|
|
|
2010-07-13 02:28:02 +04:00
|
|
|
def getargs(self, args):
|
|
|
|
data = {}
|
|
|
|
keys = args.split()
|
2019-09-27 01:28:09 +03:00
|
|
|
for n in range(len(keys)):
|
2020-02-01 05:36:25 +03:00
|
|
|
argline = decodeutf8(self.fin.readline()[:-1])
|
2010-07-13 02:28:02 +04:00
|
|
|
arg, l = argline.split()
|
|
|
|
if arg not in keys:
|
2016-06-14 12:53:55 +03:00
|
|
|
raise error.Abort(_("unexpected parameter %r") % arg)
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
if arg == "*":
|
2010-07-13 02:28:02 +04:00
|
|
|
star = {}
|
2019-09-27 01:28:09 +03:00
|
|
|
for k in range(int(l)):
|
2020-02-01 05:36:25 +03:00
|
|
|
argline = decodeutf8(self.fin.readline()[:-1])
|
2010-07-13 02:28:02 +04:00
|
|
|
arg, l = argline.split()
|
2020-02-01 05:36:25 +03:00
|
|
|
val = decodeutf8(self.fin.read(int(l)))
|
2010-07-13 02:28:02 +04:00
|
|
|
star[arg] = val
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
data["*"] = star
|
2010-07-13 02:28:02 +04:00
|
|
|
else:
|
2020-02-01 05:36:25 +03:00
|
|
|
val = decodeutf8(self.fin.read(int(l)))
|
2010-07-13 02:28:02 +04:00
|
|
|
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
|
|
|
|
2010-07-16 20:16:15 +04:00
|
|
|
def getfile(self, fpout):
|
2020-02-01 05:36:25 +03:00
|
|
|
self.sendresponse(b"")
|
2010-07-16 20:16:15 +04:00
|
|
|
count = int(self.fin.readline())
|
|
|
|
while count:
|
|
|
|
fpout.write(self.fin.read(count))
|
|
|
|
count = int(self.fin.readline())
|
|
|
|
|
|
|
|
def redirect(self):
|
|
|
|
pass
|
|
|
|
|
2010-07-17 00:20:10 +04:00
|
|
|
def sendresponse(self, v):
|
2020-02-01 05:36:25 +03:00
|
|
|
self.sendbytesresponse(encodeutf8(v))
|
|
|
|
|
|
|
|
def sendbytesresponse(self, v):
|
|
|
|
self.fout.write(b"%d\n" % len(v))
|
2010-07-17 00:20:10 +04:00
|
|
|
self.fout.write(v)
|
2010-07-15 00:43:20 +04:00
|
|
|
self.fout.flush()
|
|
|
|
|
2010-07-15 01:19:27 +04:00
|
|
|
def sendstream(self, source):
|
2012-09-14 23:09:44 +04:00
|
|
|
write = self.fout.write
|
2016-11-21 00:50:45 +03:00
|
|
|
|
|
|
|
if source.reader:
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
gen = iter(lambda: source.reader.read(4096), "")
|
2016-11-21 00:50:45 +03:00
|
|
|
else:
|
|
|
|
gen = source.gen
|
|
|
|
|
|
|
|
for chunk in gen:
|
2012-09-14 23:09:44 +04:00
|
|
|
write(chunk)
|
2010-07-15 01:19:27 +04:00
|
|
|
self.fout.flush()
|
|
|
|
|
2010-07-20 22:53:33 +04:00
|
|
|
def sendpushresponse(self, rsp):
|
2020-02-01 05:36:25 +03:00
|
|
|
self.sendresponse(b"")
|
|
|
|
self.sendresponse(bytes(rsp.res))
|
2010-07-15 20:24:42 +04:00
|
|
|
|
2010-10-11 21:45:36 +04:00
|
|
|
def sendpusherror(self, rsp):
|
|
|
|
self.sendresponse(rsp.res)
|
|
|
|
|
2011-08-02 23:21:10 +04:00
|
|
|
def sendooberror(self, rsp):
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
self.ui.ferr.write("%s\n-\n" % rsp.message)
|
2011-08-02 23:21:10 +04:00
|
|
|
self.ui.ferr.flush()
|
2020-02-01 05:36:25 +03:00
|
|
|
self.fout.write(b"\n")
|
2011-08-02 23:21:10 +04:00
|
|
|
self.fout.flush()
|
|
|
|
|
2006-06-04 21:26:05 +04:00
|
|
|
def serve_forever(self):
|
2009-04-22 04:01:22 +04:00
|
|
|
try:
|
2010-01-25 09:05:27 +03:00
|
|
|
while self.serve_one():
|
|
|
|
pass
|
2009-04-22 04:01:22 +04:00
|
|
|
finally:
|
|
|
|
if self.lock is not None:
|
|
|
|
self.lock.release()
|
2006-06-04 21:26:05 +04:00
|
|
|
sys.exit(0)
|
|
|
|
|
2010-07-20 22:53:33 +04:00
|
|
|
handlers = {
|
2020-02-01 05:36:25 +03:00
|
|
|
bytes: sendbytesresponse,
|
2010-07-20 22:53:33 +04:00
|
|
|
str: sendresponse,
|
|
|
|
wireproto.streamres: sendstream,
|
|
|
|
wireproto.pushres: sendpushresponse,
|
2010-10-11 21:45:36 +04:00
|
|
|
wireproto.pusherr: sendpusherror,
|
2011-08-02 23:21:10 +04:00
|
|
|
wireproto.ooberror: sendooberror,
|
2010-07-20 22:53:33 +04:00
|
|
|
}
|
|
|
|
|
2006-06-04 21:26:05 +04:00
|
|
|
def serve_one(self):
|
|
|
|
cmd = self.fin.readline()[:-1]
|
2020-02-01 05:36:25 +03:00
|
|
|
cmd = decodeutf8(cmd)
|
2018-10-28 01:10:26 +03:00
|
|
|
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)
|
2017-09-29 18:49:20 +03:00
|
|
|
else:
|
2018-10-28 01:10:26 +03:00
|
|
|
impl = getattr(self, "do_" + cmd, None)
|
|
|
|
if impl:
|
|
|
|
r = impl()
|
|
|
|
if r is not None:
|
|
|
|
self.sendresponse(r)
|
|
|
|
else:
|
2020-02-01 05:36:25 +03:00
|
|
|
self.sendresponse(b"")
|
|
|
|
return cmd != b""
|
2006-06-04 21:26:05 +04:00
|
|
|
|
2010-07-15 20:24:42 +04:00
|
|
|
def _client(self):
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
client = encoding.environ.get("SSH_CLIENT", "").split(" ", 1)[0]
|
|
|
|
return "remote:ssh:" + client
|