2022-08-24 15:01:07 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class InteractiveInput:
|
|
|
|
"""
|
|
|
|
Interactive input for a shell command.
|
|
|
|
|
|
|
|
:attr str prompt_pattern: regular expression that defines expected prompt from the command.
|
|
|
|
:attr str input: user input that should be supplied to the command in response to the prompt.
|
|
|
|
"""
|
2022-08-24 11:41:11 +00:00
|
|
|
|
2022-08-24 15:01:07 +00:00
|
|
|
prompt_pattern: str
|
|
|
|
input: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class CommandOptions:
|
|
|
|
"""
|
|
|
|
Options that control command execution.
|
|
|
|
|
|
|
|
:attr list interactive_inputs: user inputs that should be interactively supplied to
|
2022-08-24 11:41:11 +00:00
|
|
|
the command during execution.
|
2022-08-24 15:01:07 +00:00
|
|
|
:attr int timeout: timeout for command execution (in seconds).
|
|
|
|
:attr bool check: controls whether to check return code of the command. Set to False to
|
|
|
|
ignore non-zero return codes.
|
|
|
|
"""
|
2022-08-24 11:41:11 +00:00
|
|
|
|
2022-08-24 15:01:07 +00:00
|
|
|
interactive_inputs: Optional[list[InteractiveInput]] = None
|
|
|
|
timeout: int = 30
|
|
|
|
check: bool = True
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class CommandResult:
|
|
|
|
"""
|
|
|
|
Represents a result of a command executed via shell.
|
|
|
|
"""
|
2022-08-24 11:41:11 +00:00
|
|
|
|
2022-08-24 15:01:07 +00:00
|
|
|
stdout: str
|
|
|
|
stderr: str
|
|
|
|
return_code: int
|
|
|
|
|
|
|
|
|
|
|
|
class Shell(ABC):
|
|
|
|
"""
|
|
|
|
Interface of a command shell on some system (local or remote).
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def exec(self, command: str, options: Optional[CommandOptions] = None) -> CommandResult:
|
|
|
|
"""
|
|
|
|
Executes specified command on this shell. To execute interactive command, user inputs
|
|
|
|
should be specified in *options*.
|
|
|
|
|
|
|
|
:param str command: command to execute on the shell.
|
|
|
|
:param CommandOptions options: options that control command execution.
|
|
|
|
:return command result.
|
|
|
|
"""
|