Command
The scherbelberg.Command class describes shell commands and allows to manipulate and chain them quickly via pipes - very similar to what Python’s pathlib does to paths. It uses the scherbelberg.Process class to actually run the commands asynchronously.
The Command Class
- class scherbelberg.Command(cmd)
Representing a chain of commands, connected via pipes. Immutable.
- Parameters
cmd (
List[List[str]]) – List of list of strings. Each inner list represents one command compatible tosubprocess.Popen.
- __len__()
Number of chained commmands
- Return type
int
- __or__(other)
Pipe
- Parameters
other (
CommandABC) –- Return type
CommandABC
- __repr__()
Interactive string representation
- Return type
str
- __str__()
String conversion
- Return type
str
- property cmd: List[List[str]]
List of list of strings. Each inner list represents one command compatible to
subprocess.Popen. This interface can not be used to change the command.- Return type
List[List[str]]
- classmethod from_list(cmd)
Generates a
scherbelberg.Commandobject from a list of strings compatible tosubprocess.Popen.- Parameters
cmd (
List[str]) – A list of strings compatible tosubprocess.Popen.- Return type
CommandABC- Returns
New command object.
- classmethod from_scp(*source, target, host)
Generates a
scherbelberg.Commandobject representing anscpcommand. Only supports copy operations from the local system to the remote host.- Parameters
source (
str) – An arbitrary number of paths on the local system.target (
str) – Target path on the remote system.host (
SSHConfigABC) – SSH configuration.
- Return type
CommandABC- Returns
New command object.
- classmethod from_str(cmd)
Generates a
scherbelberg.Commandobject from a single string containing a (shell) command.- Parameters
cmd (
str) – Single string containing a (shell) command.- Return type
CommandABC- Returns
New command object.
- on_host(host)
Adds a
sshprefix to the command so it can be executed on a remote host. Does not change the current command but returns a new one.- Parameters
host (
SSHConfigABC) – SSH configuration- Return type
CommandABC- Returns
A new
scherbelberg.Commandobject which can be executed on the remote host.
- async run(returncode=False, timeout=None, wait=1.0)
Run command or chain of commands in a
scherbelberg.Processobject.- Parameters
returncode (
bool) – If set toTrue, returns actual return code and does not raise an exception if the command(s) failed. If set toFalse, a failed command raises an exception and only data from standard output and standard error streams is returned.timeout (
Union[float,int,None]) – Total timeout in seconds.wait (
float) – Interval defining every how many seconds the status of the running process is being observed.
- Return type
Union[Tuple[List[str],List[str],List[int],Exception],Tuple[List[str],List[str]]]- Returns
A tuple, the first two elements containing data from standard output and standard error streams. If
returncodeis set toTrue, the tuple has two additional entries, a list of return codes and an exception object that can be raised by the caller.