mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
lib.ec.ssh.AsyncSSH: Code beautification
Apply some style changes: - Replace double by single quotes for consistency - Add spaces around equal signs in long parameter lists Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
1359719f04
commit
f1898941e7
1 changed files with 85 additions and 85 deletions
|
|
@ -25,28 +25,28 @@ class AsyncSSH(Base):
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
uri,
|
uri,
|
||||||
caps = self.Caps.LogOutput | self.Caps.Wd | self.Caps.Interactive | self.Caps.ModEnv,
|
caps = self.Caps.LogOutput | self.Caps.Wd | self.Caps.Interactive | self.Caps.Env,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
self.client_keys = client_keys
|
self.client_keys = client_keys
|
||||||
self.known_hosts = known_hosts
|
self.known_hosts = known_hosts
|
||||||
self.term_type = term_type or os.environ.get("TERM", "xterm")
|
self.term_type = term_type or os.environ.get('TERM', 'xterm')
|
||||||
self.connect_timeout = connect_timeout
|
self.connect_timeout = connect_timeout
|
||||||
|
|
||||||
def _connect_kwargs(self, hide_secrets: bool=False) -> dict:
|
def _connect_kwargs(self, hide_secrets: bool=False) -> dict:
|
||||||
|
|
||||||
kwargs: dict = {
|
kwargs: dict = {
|
||||||
"host": self.hostname,
|
'host': self.hostname,
|
||||||
"port": self.port,
|
'port': self.port,
|
||||||
"username": self.username,
|
'username': self.username,
|
||||||
"password": self.password,
|
'password': self.password,
|
||||||
"client_keys": self.client_keys,
|
'client_keys': self.client_keys,
|
||||||
"connect_timeout": self.connect_timeout,
|
'connect_timeout': self.connect_timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.known_hosts is not _USE_DEFAULT_KNOWN_HOSTS:
|
if self.known_hosts is not _USE_DEFAULT_KNOWN_HOSTS:
|
||||||
kwargs["known_hosts"] = self.known_hosts
|
kwargs['known_hosts'] = self.known_hosts
|
||||||
|
|
||||||
ret = {k: v for k, v in kwargs.items() if v is not None}
|
ret = {k: v for k, v in kwargs.items() if v is not None}
|
||||||
if hide_secrets and 'password' in kwargs:
|
if hide_secrets and 'password' in kwargs:
|
||||||
|
|
@ -57,13 +57,13 @@ class AsyncSSH(Base):
|
||||||
def _build_remote_command(cmd: list[str], wd: str | None) -> str:
|
def _build_remote_command(cmd: list[str], wd: str | None) -> str:
|
||||||
|
|
||||||
if not cmd:
|
if not cmd:
|
||||||
raise ValueError("cmd must not be empty")
|
raise ValueError('cmd must not be empty')
|
||||||
|
|
||||||
inner = f"exec {join_cmd(cmd)}"
|
inner = f'exec {join_cmd(cmd)}'
|
||||||
if wd is not None:
|
if wd is not None:
|
||||||
inner = f"cd {shlex.quote(wd)} && {inner}"
|
inner = f'cd {shlex.quote(wd)} && {inner}'
|
||||||
|
|
||||||
return f"/bin/sh -lc {shlex.quote(inner)}"
|
return f'/bin/sh -lc {shlex.quote(inner)}'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _has_local_tty() -> bool:
|
def _has_local_tty() -> bool:
|
||||||
|
|
@ -80,8 +80,8 @@ class AsyncSSH(Base):
|
||||||
try:
|
try:
|
||||||
import fcntl, termios, struct
|
import fcntl, termios, struct
|
||||||
|
|
||||||
packed = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, b"\0" * 8)
|
packed = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, b'\0' * 8)
|
||||||
rows2, cols2, xpixel, ypixel = struct.unpack("HHHH", packed)
|
rows2, cols2, xpixel, ypixel = struct.unpack('HHHH', packed)
|
||||||
|
|
||||||
if cols2 > 0 and rows2 > 0:
|
if cols2 > 0 and rows2 > 0:
|
||||||
cols, rows = cols2, rows2
|
cols, rows = cols2, rows2
|
||||||
|
|
@ -100,7 +100,7 @@ class AsyncSSH(Base):
|
||||||
log_prefix: str,
|
log_prefix: str,
|
||||||
log_enc: str,
|
log_enc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
buf = b""
|
buf = b''
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
chunk = await stream.read(4096)
|
chunk = await stream.read(4096)
|
||||||
|
|
@ -111,12 +111,12 @@ class AsyncSSH(Base):
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
buf += chunk
|
buf += chunk
|
||||||
while b"\n" in buf:
|
while b'\n' in buf:
|
||||||
line, buf = buf.split(b"\n", 1)
|
line, buf = buf.split(b'\n', 1)
|
||||||
log(prio, log_prefix, line.decode(log_enc, errors="replace"))
|
log(prio, log_prefix, line.decode(log_enc, errors='replace'))
|
||||||
|
|
||||||
if verbose and buf:
|
if verbose and buf:
|
||||||
log(prio, log_prefix, buf.decode(log_enc, errors="replace"))
|
log(prio, log_prefix, buf.decode(log_enc, errors='replace'))
|
||||||
|
|
||||||
async def _run_interactive_on_conn(
|
async def _run_interactive_on_conn(
|
||||||
self,
|
self,
|
||||||
|
|
@ -137,7 +137,7 @@ class AsyncSSH(Base):
|
||||||
stdout = asyncssh.PIPE,
|
stdout = asyncssh.PIPE,
|
||||||
stderr = asyncssh.STDOUT,
|
stderr = asyncssh.STDOUT,
|
||||||
encoding = None,
|
encoding = None,
|
||||||
request_pty="force",
|
request_pty = 'force',
|
||||||
term_type = self.term_type,
|
term_type = self.term_type,
|
||||||
term_size = self._get_local_term_size(),
|
term_size = self._get_local_term_size(),
|
||||||
)
|
)
|
||||||
|
|
@ -161,7 +161,7 @@ class AsyncSSH(Base):
|
||||||
try:
|
try:
|
||||||
data = os.read(stdin_fd, 4096)
|
data = os.read(stdin_fd, 4096)
|
||||||
except OSError:
|
except OSError:
|
||||||
data = b""
|
data = b''
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
stdin_queue.put_nowait(data)
|
stdin_queue.put_nowait(data)
|
||||||
|
|
@ -223,7 +223,7 @@ class AsyncSSH(Base):
|
||||||
except (NotImplementedError, RuntimeError):
|
except (NotImplementedError, RuntimeError):
|
||||||
stdin_queue.put_nowait(None)
|
stdin_queue.put_nowait(None)
|
||||||
|
|
||||||
if hasattr(signal, "SIGWINCH"):
|
if hasattr(signal, 'SIGWINCH'):
|
||||||
try:
|
try:
|
||||||
old_winch_handler = signal.getsignal(signal.SIGWINCH)
|
old_winch_handler = signal.getsignal(signal.SIGWINCH)
|
||||||
signal.signal(signal.SIGWINCH, _on_winch)
|
signal.signal(signal.SIGWINCH, _on_winch)
|
||||||
|
|
@ -247,7 +247,7 @@ class AsyncSSH(Base):
|
||||||
if exit_code is None:
|
if exit_code is None:
|
||||||
exit_code = completed.returncode if completed.returncode is not None else -1
|
exit_code = completed.returncode if completed.returncode is not None else -1
|
||||||
|
|
||||||
stdout = b"".join(stdout_parts) if stdout_parts else None
|
stdout = b''.join(stdout_parts) if stdout_parts else None
|
||||||
return Result(stdout, None, exit_code)
|
return Result(stdout, None, exit_code)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -257,7 +257,7 @@ class AsyncSSH(Base):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if old_winch_handler is not None and hasattr(signal, "SIGWINCH"):
|
if old_winch_handler is not None and hasattr(signal, 'SIGWINCH'):
|
||||||
try:
|
try:
|
||||||
signal.signal(signal.SIGWINCH, old_winch_handler)
|
signal.signal(signal.SIGWINCH, old_winch_handler)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -289,7 +289,7 @@ class AsyncSSH(Base):
|
||||||
command = self._build_remote_command(cmd, wd)
|
command = self._build_remote_command(cmd, wd)
|
||||||
|
|
||||||
stdout_parts: list[bytes] = []
|
stdout_parts: list[bytes] = []
|
||||||
stdout_log_enc = sys.stdout.encoding or "utf-8"
|
stdout_log_enc = sys.stdout.encoding or 'utf-8'
|
||||||
|
|
||||||
proc = await conn.create_process(
|
proc = await conn.create_process(
|
||||||
command = command,
|
command = command,
|
||||||
|
|
@ -298,7 +298,7 @@ class AsyncSSH(Base):
|
||||||
stdout = asyncssh.PIPE,
|
stdout = asyncssh.PIPE,
|
||||||
stderr = asyncssh.STDOUT,
|
stderr = asyncssh.STDOUT,
|
||||||
encoding = None,
|
encoding = None,
|
||||||
request_pty="force",
|
request_pty = 'force',
|
||||||
term_type = self.term_type,
|
term_type = self.term_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -325,7 +325,7 @@ class AsyncSSH(Base):
|
||||||
if exit_code is None:
|
if exit_code is None:
|
||||||
exit_code = completed.returncode if completed.returncode is not None else -1
|
exit_code = completed.returncode if completed.returncode is not None else -1
|
||||||
|
|
||||||
stdout = b"".join(stdout_parts) if stdout_parts else None
|
stdout = b''.join(stdout_parts) if stdout_parts else None
|
||||||
return Result(stdout, None, exit_code)
|
return Result(stdout, None, exit_code)
|
||||||
|
|
||||||
async def _run_on_conn(
|
async def _run_on_conn(
|
||||||
|
|
@ -364,8 +364,8 @@ class AsyncSSH(Base):
|
||||||
stdout_parts: list[bytes] = []
|
stdout_parts: list[bytes] = []
|
||||||
stderr_parts: list[bytes] = []
|
stderr_parts: list[bytes] = []
|
||||||
|
|
||||||
stdout_log_enc = sys.stdout.encoding or "utf-8"
|
stdout_log_enc = sys.stdout.encoding or 'utf-8'
|
||||||
stderr_log_enc = sys.stderr.encoding or "utf-8"
|
stderr_log_enc = sys.stderr.encoding or 'utf-8'
|
||||||
|
|
||||||
stdin_mode = asyncssh.PIPE if cmd_input is not None else asyncssh.DEVNULL
|
stdin_mode = asyncssh.PIPE if cmd_input is not None else asyncssh.DEVNULL
|
||||||
|
|
||||||
|
|
@ -410,8 +410,8 @@ class AsyncSSH(Base):
|
||||||
completed = await proc.wait(check=False)
|
completed = await proc.wait(check=False)
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
stdout = b"".join(stdout_parts) if stdout_parts else None
|
stdout = b''.join(stdout_parts) if stdout_parts else None
|
||||||
stderr = b"".join(stderr_parts) if stderr_parts else None
|
stderr = b''.join(stderr_parts) if stderr_parts else None
|
||||||
|
|
||||||
exit_code = completed.exit_status
|
exit_code = completed.exit_status
|
||||||
if exit_code is None:
|
if exit_code is None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue