jobflow_remote.remote.host package#

Submodules#

Module contents#

class jobflow_remote.remote.host.BaseHost(sanitize: bool = False)[source]#

Bases: MSONable

Base Host class.

Parameters:

sanitize – If True text a string will be prepended and appended to the output of the commands, to ease the parsing and avoid failures due to spurious text coming from the host shell.

abstract close() bool[source]#
abstract connect()[source]#
abstract copy(src, dst)[source]#
abstract execute(command: str | list[str], workdir: str | Path | None = None, timeout: int | None = None) tuple[str, str, int][source]#

Execute the given command on the host.

Parameters:
  • command (str or list of str) – Command to execute, as a str or list of str

  • workdir (str or None) – path where the command will be executed.

  • timeout – Timeout for the execution of the commands.

abstract get(src, dst)[source]#
property interactive_login: bool#

True if the host requires interactive actions upon login. False by default. Subclasses should override the method to customize the value.

abstract property is_connected: bool#
abstract listdir(path: str | Path) list[str][source]#
abstract mkdir(directory: str | Path, recursive: bool = True, exist_ok: bool = True) bool[source]#

Create directory on the host.

abstract put(src, dst)[source]#
abstract remove(path: str | Path)[source]#
abstract rmtree(path: str | Path, raise_on_error: bool = False) bool[source]#

Recursively delete a directory tree on a host.

This method must be implemented by subclasses of BaseHost. It is intended to remove an entire directory tree, including all files and subdirectories, on the host represented by the subclass.

Parameters:
  • path (str or Path) – The path to the directory tree to be removed.

  • raise_on_error (bool) – If set to False (default), errors will be ignored, and the method will attempt to continue removing remaining files and directories. Otherwise, any errors encountered during the removal process will raise an exception.

Returns:

True if the directory tree was successfully removed, False otherwise.

Return type:

bool

sanitize_command(cmd: str) str[source]#

Sanitizes a command by adding a prefix and suffix to the command string if sanitization is enabled. The prefix and suffix are the same and are used to mark the parts of the output that should be sanitized. The prefix and suffix are defined by SANITIZE_KEY.

Parameters:

cmd – The command string to be sanitized

Returns:

The sanitized command string

Return type:

str

sanitize_output(output: str) str[source]#

Sanitizes the output of a command by selecting the section between the SANITIZE_KEY strings. If the second instance of the key is not found, the part of the output after the key is returned. If the key is not present, the entire output is returned.

Parameters:

output – The output of the command to be sanitized

Returns:

The sanitized output

Return type:

str

property sanitize_regex: Pattern#

Regular expression to sanitize sensitive info in command outputs.

test() str | None[source]#
abstract write_text_file(filepath, content)[source]#

Write content to a file on the host.

class jobflow_remote.remote.host.LocalHost(timeout_execute: int = None, sanitize: bool = False)[source]#

Bases: BaseHost

Parameters:

sanitize – If True text a string will be prepended and appended to the output of the commands, to ease the parsing and avoid failures due to spurious text coming from the host shell.

close() bool[source]#
connect() None[source]#
copy(src, dst) None[source]#
execute(command: str | list[str], workdir: str | Path | None = None, timeout: int | None = None)[source]#

Execute the given command on the host.

Note that the command is executed with shell=True, so commands can be exposed to command injection. Consider whether to escape part of the input if it comes from external users.

Parameters:
  • command (str or list of str) – Command to execute, as a str or list of str

  • workdir (str or None) – path where the command will be executed.

  • timeout – Timeout for the execution of the commands.

Returns:

  • stdout (str) – Standard output of the command

  • stderr (str) – Standard error of the command

  • exit_code (int) – Exit code of the command.

get(src, dst) None[source]#
property is_connected: bool#
listdir(path: str | Path) list[str][source]#
mkdir(directory: str | Path, recursive: bool = True, exist_ok: bool = True) bool[source]#

Create directory on the host.

put(src, dst) None[source]#
remove(path: str | Path) None[source]#
rmtree(path: str | Path, raise_on_error: bool = False) bool[source]#

Recursively delete a directory tree on a local host.

It is intended to remove an entire directory tree, including all files and subdirectories, on this local host.

Parameters:
  • path (str or Path) – The path to the directory tree to be removed.

  • raise_on_error (bool) – If set to False (default), errors will be ignored, and the method will attempt to continue removing remaining files and directories. Otherwise, any errors encountered during the removal process will raise an exception.

Returns:

True if the directory tree was successfully removed, False otherwise.

Return type:

bool

write_text_file(filepath, content) None[source]#

Write content to a file on the host.

class jobflow_remote.remote.host.RemoteHost(host, user=None, port=None, config=None, gateway=None, forward_agent=None, connect_timeout=None, connect_kwargs=None, inline_ssh_env=None, timeout_execute=None, keepalive=60, shell_cmd='bash', login_shell=True, retry_on_closed_connection=True, interactive_login=False, sanitize: bool = False)[source]#

Bases: BaseHost

Execute commands on a remote host. For some commands assumes the remote can run unix.

Parameters:

sanitize – If True text a string will be prepended and appended to the output of the commands, to ease the parsing and avoid failures due to spurious text coming from the host shell.

close() bool[source]#
connect() None[source]#
property connection#
copy(src, dst) None[source]#
execute(command: str | list[str], workdir: str | Path | None = None, timeout: int | None = None)[source]#

Execute the given command on the host.

Parameters:
  • command (str or list of str) – Command to execute, as a str or list of str.

  • workdir (str or None) – path where the command will be executed.

Returns:

  • stdout (str) – Standard output of the command

  • stderr (str) – Standard error of the command

  • exit_code (int) – Exit code of the command.

get(src, dst) None[source]#
property interactive_login: bool#

True if the host requires interactive actions upon login. False by default. Subclasses should override the method to customize the value.

property is_connected: bool#
listdir(path: str | Path)[source]#
mkdir(directory: str | Path, recursive: bool = True, exist_ok: bool = True) bool[source]#

Create directory on the host.

put(src, dst) None[source]#
remove(path: str | Path) None[source]#
rmtree(path: str | Path, raise_on_error: bool = False) bool[source]#

Recursively delete a directory tree on a remote host.

It is intended to remove an entire directory tree, including all files and subdirectories, on this remote host.

Parameters:
  • path (str or Path) – The path to the directory tree to be removed.

  • raise_on_error (bool) – If set to False (default), errors will be ignored, and the method will attempt to continue removing remaining files and directories. Otherwise, any errors encountered during the removal process will raise an exception.

Returns:

True if the directory tree was successfully removed, False otherwise.

Return type:

bool

write_text_file(filepath: str | Path, content: str) None[source]#

Write content to a file on the host.