Dont import all of the ssh kitten just to detect commandlines
This commit is contained in:
parent
5efcb35cfb
commit
bba1455e28
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
import atexit
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import glob
|
import glob
|
||||||
import io
|
import io
|
||||||
@ -47,6 +46,7 @@ from .config import init_config
|
|||||||
from .copy import CopyInstruction
|
from .copy import CopyInstruction
|
||||||
from .options.types import Options as SSHOptions
|
from .options.types import Options as SSHOptions
|
||||||
from .options.utils import DELETE_ENV_VAR
|
from .options.utils import DELETE_ENV_VAR
|
||||||
|
from .utils import create_shared_memory
|
||||||
|
|
||||||
|
|
||||||
@run_once
|
@run_once
|
||||||
@ -54,40 +54,6 @@ def ssh_exe() -> str:
|
|||||||
return shutil.which('ssh') or 'ssh'
|
return shutil.which('ssh') or 'ssh'
|
||||||
|
|
||||||
|
|
||||||
def is_kitten_cmdline(q: List[str]) -> bool:
|
|
||||||
if len(q) < 4:
|
|
||||||
return False
|
|
||||||
if os.path.basename(q[0]).lower() != 'kitty':
|
|
||||||
return False
|
|
||||||
return q[1:3] == ['+kitten', 'ssh'] or q[1:4] == ['+', 'kitten', 'ssh']
|
|
||||||
|
|
||||||
|
|
||||||
def patch_cmdline(key: str, val: str, argv: List[str]) -> None:
|
|
||||||
for i, arg in enumerate(tuple(argv)):
|
|
||||||
if arg.startswith(f'--kitten={key}='):
|
|
||||||
argv[i] = f'--kitten={key}={val}'
|
|
||||||
return
|
|
||||||
elif i > 0 and argv[i-1] == '--kitten' and (arg.startswith(f'{key}=') or arg.startswith(f'{key} ')):
|
|
||||||
argv[i] = val
|
|
||||||
return
|
|
||||||
idx = argv.index('ssh')
|
|
||||||
argv.insert(idx + 1, f'--kitten={key}={val}')
|
|
||||||
|
|
||||||
|
|
||||||
def set_cwd_in_cmdline(cwd: str, argv: List[str]) -> None:
|
|
||||||
patch_cmdline('cwd', cwd, argv)
|
|
||||||
|
|
||||||
|
|
||||||
def create_shared_memory(data: Any, prefix: str) -> str:
|
|
||||||
from kitty.shm import SharedMemory
|
|
||||||
db = json.dumps(data).encode('utf-8')
|
|
||||||
with SharedMemory(size=len(db) + SharedMemory.num_bytes_for_size, mode=stat.S_IREAD, prefix=prefix) as shm:
|
|
||||||
shm.write_data_with_size(db)
|
|
||||||
shm.flush()
|
|
||||||
atexit.register(shm.unlink)
|
|
||||||
return shm.name
|
|
||||||
|
|
||||||
|
|
||||||
def read_data_from_shared_memory(shm_name: str) -> Any:
|
def read_data_from_shared_memory(shm_name: str) -> Any:
|
||||||
with SharedMemory(shm_name, readonly=True) as shm:
|
with SharedMemory(shm_name, readonly=True) as shm:
|
||||||
shm.unlink()
|
shm.unlink()
|
||||||
@ -99,10 +65,6 @@ def read_data_from_shared_memory(shm_name: str) -> Any:
|
|||||||
return json.loads(shm.read_data_with_size())
|
return json.loads(shm.read_data_with_size())
|
||||||
|
|
||||||
|
|
||||||
def set_env_in_cmdline(env: Dict[str, str], argv: List[str]) -> None:
|
|
||||||
patch_cmdline('clone_env', create_shared_memory(env, 'ksse-'), argv)
|
|
||||||
|
|
||||||
|
|
||||||
# See https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
|
# See https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
|
||||||
quote_pat = re.compile('([\\`"])')
|
quote_pat = re.compile('([\\`"])')
|
||||||
|
|
||||||
|
|||||||
48
kittens/ssh/utils.py
Normal file
48
kittens/ssh/utils.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
|
|
||||||
|
def is_kitten_cmdline(q: List[str]) -> bool:
|
||||||
|
if len(q) < 4:
|
||||||
|
return False
|
||||||
|
if os.path.basename(q[0]).lower() != 'kitty':
|
||||||
|
return False
|
||||||
|
return q[1:3] == ['+kitten', 'ssh'] or q[1:4] == ['+', 'kitten', 'ssh']
|
||||||
|
|
||||||
|
|
||||||
|
def patch_cmdline(key: str, val: str, argv: List[str]) -> None:
|
||||||
|
for i, arg in enumerate(tuple(argv)):
|
||||||
|
if arg.startswith(f'--kitten={key}='):
|
||||||
|
argv[i] = f'--kitten={key}={val}'
|
||||||
|
return
|
||||||
|
elif i > 0 and argv[i-1] == '--kitten' and (arg.startswith(f'{key}=') or arg.startswith(f'{key} ')):
|
||||||
|
argv[i] = val
|
||||||
|
return
|
||||||
|
idx = argv.index('ssh')
|
||||||
|
argv.insert(idx + 1, f'--kitten={key}={val}')
|
||||||
|
|
||||||
|
|
||||||
|
def set_cwd_in_cmdline(cwd: str, argv: List[str]) -> None:
|
||||||
|
patch_cmdline('cwd', cwd, argv)
|
||||||
|
|
||||||
|
|
||||||
|
def create_shared_memory(data: Any, prefix: str) -> str:
|
||||||
|
import atexit
|
||||||
|
import json
|
||||||
|
import stat
|
||||||
|
|
||||||
|
from kitty.shm import SharedMemory
|
||||||
|
db = json.dumps(data).encode('utf-8')
|
||||||
|
with SharedMemory(size=len(db) + SharedMemory.num_bytes_for_size, mode=stat.S_IREAD, prefix=prefix) as shm:
|
||||||
|
shm.write_data_with_size(db)
|
||||||
|
shm.flush()
|
||||||
|
atexit.register(shm.unlink)
|
||||||
|
return shm.name
|
||||||
|
|
||||||
|
|
||||||
|
def set_env_in_cmdline(env: Dict[str, str], argv: List[str]) -> None:
|
||||||
|
patch_cmdline('clone_env', create_shared_memory(env, 'ksse-'), argv)
|
||||||
@ -613,7 +613,7 @@ def clone_and_launch(msg: str, window: Window) -> None:
|
|||||||
is_clone_launch = serialize_env(c.shell, env_to_serialize)
|
is_clone_launch = serialize_env(c.shell, env_to_serialize)
|
||||||
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
|
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
|
||||||
if ssh_kitten_cmdline:
|
if ssh_kitten_cmdline:
|
||||||
from kittens.ssh.main import (
|
from kittens.ssh.utils import (
|
||||||
patch_cmdline, set_cwd_in_cmdline, set_env_in_cmdline
|
patch_cmdline, set_cwd_in_cmdline, set_env_in_cmdline
|
||||||
)
|
)
|
||||||
cmdline = ssh_kitten_cmdline
|
cmdline = ssh_kitten_cmdline
|
||||||
|
|||||||
@ -97,7 +97,7 @@ class CwdRequest:
|
|||||||
if argv[0] == resolved_shell(get_options())[0]:
|
if argv[0] == resolved_shell(get_options())[0]:
|
||||||
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
|
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
|
||||||
if ssh_kitten_cmdline:
|
if ssh_kitten_cmdline:
|
||||||
from kittens.ssh.main import set_cwd_in_cmdline
|
from kittens.ssh.utils import set_cwd_in_cmdline
|
||||||
argv[:] = ssh_kitten_cmdline
|
argv[:] = ssh_kitten_cmdline
|
||||||
set_cwd_in_cmdline(reported_cwd, argv)
|
set_cwd_in_cmdline(reported_cwd, argv)
|
||||||
return ''
|
return ''
|
||||||
@ -1319,7 +1319,7 @@ class Window:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def ssh_kitten_cmdline(self) -> List[str]:
|
def ssh_kitten_cmdline(self) -> List[str]:
|
||||||
from kittens.ssh.main import is_kitten_cmdline
|
from kittens.ssh.utils import is_kitten_cmdline
|
||||||
for p in self.child.foreground_processes:
|
for p in self.child.foreground_processes:
|
||||||
q = list(p['cmdline'] or ())
|
q = list(p['cmdline'] or ())
|
||||||
if is_kitten_cmdline(q):
|
if is_kitten_cmdline(q):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user