"""SSH connection.
Connect to a remote host via SSH and execute a command on the host.
"""
-import os
-import sys
-import re
-import subprocess
+import sys, os, re, subprocess
+from bup import helpers, path
-from bup import helpers
-
-def connect(rhost, subcmd):
+def connect(rhost, port, subcmd, stderr=None):
"""Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
assert(not re.search(r'[^\w-]', subcmd))
- main_exe = os.environ.get('BUP_MAIN_EXE') or sys.argv[0]
- nicedir = os.path.split(os.path.abspath(main_exe))[0]
- nicedir = re.sub(r':', "_", nicedir)
+ nicedir = re.sub(r':', "_", path.exedir())
if rhost == '-':
rhost = None
if not rhost:
cmd = r"""
sh -c PATH=%s:'$PATH BUP_DEBUG=%s BUP_FORCE_TTY=%s bup %s'
""" % (escapedir, buglvl, force_tty, subcmd)
- argv = ['ssh', rhost, '--', cmd.strip()]
+ argv = ['ssh']
+ if port:
+ argv.extend(('-p', port))
+ argv.extend((rhost, '--', cmd.strip()))
#helpers.log('argv is: %r\n' % argv)
def setup():
# runs in the child process
os.environ.get('PATH', '')])
os.setsid()
return subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=stderr,
preexec_fn=setup)