Skip to main content
pyinfra provides several command types for different operations: shell commands, file transfers, Python functions, and rsync operations.

StringCommand

Execute shell commands with automatic escaping and formatting.

Constructor

str | QuoteString | MaskString | StringCommand
required
Command parts to join together.
str
default:" "
String to use between parts.
ConnectorArguments
Execution arguments like _sudo, _su_user, etc.

Methods

str
Get the raw command string (including masked values).
str
Get the command string with masked values replaced by ***.

Usage in Operations

QuoteString Helper

Automatically quote and escape arguments:

MaskString Helper

Hide sensitive values in logs:

Formatted Commands

Use make_formatted_string_command() for complex formatting:

FileUploadCommand

Upload files or file-like objects to remote hosts.

Constructor

str | IO
required
Source file path or file-like object to upload.
str
required
Destination path on remote host.
str
Temporary filename to use on remote host during upload.

Usage in Operations

FileDownloadCommand

Download files from remote hosts.

Constructor

str
required
Source path on remote host.
str | IO
required
Local destination path or file-like object.

Usage in Operations

FunctionCommand

Execute Python functions on remote hosts.

Constructor

Callable
required
Python function to execute.
tuple
required
Positional arguments to pass to the function.
dict
required
Keyword arguments to pass to the function.

Function Signatures

Functions can optionally accept state and host parameters:

Usage in Operations

RsyncCommand

Sync files using rsync (if supported by connector).

Constructor

str
required
Source directory path.
str
required
Destination directory path.
list[str]
required
List of rsync flags (e.g., ["-avz", "--delete"]).

Usage in Operations

Complete Example

Here’s a complete operation using various command types:

Source Reference

Location: src/pyinfra/api/command.py

Key Classes

  • PyinfraCommand - Base command class (line 66)
  • StringCommand - Shell commands (line 82)
  • FileUploadCommand - File uploads (line 150)
  • FileDownloadCommand - File downloads (line 181)
  • FunctionCommand - Python functions (line 212)
  • RsyncCommand - Rsync operations (line 260)
  • QuoteString - Quote helper (line 55)
  • MaskString - Mask helper (line 51)

Helper Functions

  • make_formatted_string_command() - Create formatted commands (line 20)