1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-30 17:50:46 +00:00

utils/ssh: Remove _check_env()

Replace _check_env() by lazily initialized global var.
This commit is contained in:
Douglas Raillard 2023-06-26 15:19:47 +01:00 committed by Marc Bonnici
parent 926aee1833
commit c39d40c6f8

View File

@ -67,9 +67,17 @@ from devlib.connection import (ConnectionBase, ParamikoBackgroundCommand, PopenB
DEFAULT_SSH_SUDO_COMMAND = "sudo -k -p ' ' -S -- sh -c {}"
ssh = None
scp = None
sshpass = None
# Lazy init of some globals
def __getattr__(attr):
if attr in {'ssh', 'scp', 'sshpass'}:
path = which(attr)
if path:
globals()[attr] = path
return path
else:
raise HostError(f'OpenSSH must be installed on the host: could not find {attr} command')
else:
raise AttributeError(f"Module '{__name__}' has no attribute '{attr}'")
logger = logging.getLogger('ssh')
@ -176,7 +184,6 @@ def telnet_get_shell(host,
port=None,
timeout=10,
original_prompt=None):
_check_env()
start_time = time.time()
while True:
conn = TelnetPxssh(original_prompt=original_prompt)
@ -792,7 +799,6 @@ class TelnetConnection(SshConnectionBase):
strict_host_check=strict_host_check,
)
_check_env()
self.options = self._get_default_options()
self.lock = threading.Lock()
@ -1607,16 +1613,6 @@ def _give_password(password, command):
return (pass_string + command, redacted_string + command)
def _check_env():
global ssh, scp, sshpass # pylint: disable=global-statement
if not ssh:
ssh = which('ssh')
scp = which('scp')
sshpass = which('sshpass')
if not (ssh and scp):
raise HostError('OpenSSH must be installed on the host.')
def process_backspaces(text):
chars = []
for c in text: