craft_providers.executor module
Executor module.
- class craft_providers.executor.Executor[source]
Bases:
abc.ABC
Interfaces to execute commands and move data in/out of an environment.
- abstract execute_popen(command, *, cwd=None, env=None, **kwargs)[source]
Execute a command in instance, using subprocess.Popen().
The process’ environment will inherit the execution environment’s default environment (PATH, etc.), but can be additionally configured via env parameter.
- Parameters
command (
List
[str
]) – Command to execute.env (
Optional
[Dict
[str
,Optional
[str
]]]) – Additional environment to set for process.kwargs – Additional keyword arguments to pass.
cwd (
Optional
[Path
]) –
- Return type
Popen
- Returns
Popen instance.
- abstract execute_run(command, *, cwd=None, env=None, **kwargs)[source]
Execute a command using subprocess.run().
The process’ environment will inherit the execution environment’s default environment (PATH, etc.), but can be additionally configured via env parameter.
- Parameters
command (
List
[str
]) – Command to execute.env (
Optional
[Dict
[str
,Optional
[str
]]]) – Additional environment to set for process.kwargs – Keyword args to pass to subprocess.run().
cwd (
Optional
[Path
]) –
- Return type
CompletedProcess
- Returns
Completed process.
- Raises
subprocess.CalledProcessError – if command fails and check is True.
- abstract exists()[source]
Check if instance exists.
- Return type
bool
- Returns
True if instance exists.
- abstract is_running()[source]
Check if instance is running.
- Return type
bool
- Returns
True if instance is running.
- abstract mount(*, host_source, target)[source]
Mount host source directory to target mount point.
- Parameters
host_source (
Path
) –target (
Path
) –
- Return type
None
- abstract pull_file(*, source, destination)[source]
Copy a file from the environment to host.
- Parameters
source (
PurePath
) – Environment file to copy.destination (
Path
) – Host file path to copy to. Parent directory (destination.parent) must exist.
- Raises
FileNotFoundError – If source file or destination’s parent directory does not exist.
ProviderError – On error copying file.
- Return type
None
- abstract push_file(*, source, destination)[source]
Copy a file from the host into the environment.
The destination file is overwritten if it exists.
- Parameters
source (
Path
) – Host file to copy.destination (
PurePath
) – Target environment file path to copy to. Parent directory (destination.parent) must exist.
- Raises
FileNotFoundError – If source file or destination’s parent directory does not exist.
ProviderError – On error copying file.
- Return type
None
- abstract push_file_io(*, destination, content, file_mode, group='root', user='root')[source]
Create or replace a file with specified content and file mode.
- Parameters
destination (
PurePath
) – Path to file.content (
BytesIO
) – Contents of file.file_mode (
str
) – File mode string (e.g. ‘0644’).group (
str
) – File owner group.user (
str
) – File owner user.
- Return type
None
- temporarily_pull_file(*, source, missing_ok=False)[source]
Copy a file from the environment to a temporary file in the host.
This is mainly a layer above pull_file that pulls the file into a temporary path which is cleaned later.
Works as a context manager, provides the file path in the host as target.
The temporary file is stored in the home directory where Multipass has access.
- Parameters
source (
Path
) – Environment file to copy.missing_ok (
bool
) – Do not raise an error if the file does not exist in the environment; in this case the target will be None.
- Raises
FileNotFoundError – If source file or destination’s parent directory does not exist (and missing_ok is False).
ProviderError – On error copying file content.
- Return type
Generator
[Optional
[Path
],None
,None
]