mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 10:41:58 +02:00
Refactor launch sequence in preparation for optionising it
This commit is contained in:
@@ -437,14 +437,14 @@ def wrap_bootstrap_script(sh_script: str, interpreter: str) -> List[str]:
|
|||||||
def get_remote_command(
|
def get_remote_command(
|
||||||
remote_args: List[str], ssh_opts: SSHOptions,
|
remote_args: List[str], ssh_opts: SSHOptions,
|
||||||
hostname: str = 'localhost', cli_hostname: str = '', cli_uname: str = '', echo_on: bool = True,
|
hostname: str = 'localhost', cli_hostname: str = '', cli_uname: str = '', echo_on: bool = True,
|
||||||
) -> Tuple[List[str], Dict[str, str]]:
|
) -> Tuple[List[str], Dict[str, str], str]:
|
||||||
interpreter = ssh_opts.interpreter
|
interpreter = ssh_opts.interpreter
|
||||||
q = os.path.basename(interpreter).lower()
|
q = os.path.basename(interpreter).lower()
|
||||||
is_python = 'python' in q
|
is_python = 'python' in q
|
||||||
sh_script, replacements, shm = bootstrap_script(
|
sh_script, replacements, shm = bootstrap_script(
|
||||||
ssh_opts, script_type='py' if is_python else 'sh', remote_args=remote_args,
|
ssh_opts, script_type='py' if is_python else 'sh', remote_args=remote_args,
|
||||||
cli_hostname=cli_hostname, cli_uname=cli_uname, echo_on=echo_on)
|
cli_hostname=cli_hostname, cli_uname=cli_uname, echo_on=echo_on)
|
||||||
return wrap_bootstrap_script(sh_script, interpreter), replacements
|
return wrap_bootstrap_script(sh_script, interpreter), replacements, shm.name
|
||||||
|
|
||||||
|
|
||||||
def connection_sharing_args(opts: SSHOptions, kitty_pid: int) -> List[str]:
|
def connection_sharing_args(opts: SSHOptions, kitty_pid: int) -> List[str]:
|
||||||
@@ -525,7 +525,7 @@ def drain_potential_tty_garbage(p: 'subprocess.Popen[bytes]', data_request: str)
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple[str, ...], echo_on: bool) -> NoReturn:
|
def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple[str, ...]) -> NoReturn:
|
||||||
cmd = ['ssh'] + ssh_args
|
cmd = ['ssh'] + ssh_args
|
||||||
hostname, remote_args = server_args[0], server_args[1:]
|
hostname, remote_args = server_args[0], server_args[1:]
|
||||||
if not remote_args:
|
if not remote_args:
|
||||||
@@ -554,25 +554,27 @@ def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple
|
|||||||
so = init_config(overrides)
|
so = init_config(overrides)
|
||||||
host_opts = options_for_host(hostname_for_match, uname, so)
|
host_opts = options_for_host(hostname_for_match, uname, so)
|
||||||
use_control_master = host_opts.share_connections
|
use_control_master = host_opts.share_connections
|
||||||
rcmd, replacements = get_remote_command(remote_args, host_opts, hostname, hostname_for_match, uname, echo_on)
|
|
||||||
cmd += rcmd
|
|
||||||
if use_control_master:
|
if use_control_master:
|
||||||
cmd[insertion_point:insertion_point] = connection_sharing_args(host_opts, int(os.environ['KITTY_PID']))
|
cmd[insertion_point:insertion_point] = connection_sharing_args(host_opts, int(os.environ['KITTY_PID']))
|
||||||
# We force use of askpass so that OpenSSH does not use the tty leaving
|
with restore_terminal_state() as echo_on:
|
||||||
# it free for us to use
|
rcmd, replacements, shm_name = get_remote_command(remote_args, host_opts, hostname, hostname_for_match, uname, echo_on)
|
||||||
os.environ['SSH_ASKPASS_REQUIRE'] = 'force'
|
cmd += rcmd
|
||||||
if not os.environ.get('SSH_ASKPASS'):
|
# We force use of askpass so that OpenSSH does not use the tty leaving
|
||||||
os.environ['SSH_ASKPASS'] = os.path.join(shell_integration_dir, 'ssh', 'askpass.py')
|
# it free for us to use
|
||||||
try:
|
os.environ['SSH_ASKPASS_REQUIRE'] = 'force'
|
||||||
p = subprocess.Popen(cmd)
|
if not os.environ.get('SSH_ASKPASS'):
|
||||||
except FileNotFoundError:
|
os.environ['SSH_ASKPASS'] = os.path.join(shell_integration_dir, 'ssh', 'askpass.py')
|
||||||
raise SystemExit('Could not find the ssh executable, is it in your PATH?')
|
with no_echo(sys.stdin.fileno()):
|
||||||
else:
|
|
||||||
with drain_potential_tty_garbage(p, 'id={REQUEST_ID}:pwfile={PASSWORD_FILENAME}:pw={DATA_PASSWORD}'.format(**replacements)):
|
|
||||||
try:
|
try:
|
||||||
raise SystemExit(p.wait())
|
p = subprocess.Popen(cmd)
|
||||||
except KeyboardInterrupt:
|
except FileNotFoundError:
|
||||||
raise SystemExit(1)
|
raise SystemExit('Could not find the ssh executable, is it in your PATH?')
|
||||||
|
else:
|
||||||
|
with drain_potential_tty_garbage(p, 'id={REQUEST_ID}:pwfile={PASSWORD_FILENAME}:pw={DATA_PASSWORD}'.format(**replacements)):
|
||||||
|
try:
|
||||||
|
raise SystemExit(p.wait())
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
def main(args: List[str]) -> NoReturn:
|
def main(args: List[str]) -> NoReturn:
|
||||||
@@ -589,8 +591,7 @@ def main(args: List[str]) -> NoReturn:
|
|||||||
raise SystemExit('The SSH kitten is meant for interactive use via SSH only')
|
raise SystemExit('The SSH kitten is meant for interactive use via SSH only')
|
||||||
if not sys.stdin.isatty():
|
if not sys.stdin.isatty():
|
||||||
raise SystemExit('The SSH kitten is meant for interactive use only, STDIN must be a terminal')
|
raise SystemExit('The SSH kitten is meant for interactive use only, STDIN must be a terminal')
|
||||||
with restore_terminal_state() as echo_on, no_echo(sys.stdin.fileno()):
|
run_ssh(ssh_args, server_args, found_extra_args)
|
||||||
run_ssh(ssh_args, server_args, found_extra_args, echo_on)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user