2026-08-17 16:30:25 +02:00
|
|
|
"""Running external commands without a shell.
|
|
|
|
|
|
|
|
|
|
Every command is an argv list. A database name, a password or a container name
|
|
|
|
|
therefore cannot close a quote and start a second command, which a formatted
|
|
|
|
|
string handed to ``shell=True`` allowed.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
import os
|
2025-12-26 18:13:26 +01:00
|
|
|
import subprocess
|
2026-08-18 01:47:38 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import Mapping, Sequence
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
class BackupError(Exception):
|
2025-12-26 18:13:26 +01:00
|
|
|
"""Generic exception for backup errors."""
|
|
|
|
|
|
|
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
def _child_env(env: Mapping[str, str] | None) -> dict[str, str] | None:
|
|
|
|
|
return None if env is None else {**os.environ, **env}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fail(command: Sequence[str], returncode: int, out: bytes, err: bytes) -> None:
|
2026-08-18 01:47:38 +02:00
|
|
|
raise BackupError(
|
2026-08-17 16:30:25 +02:00
|
|
|
f"Error in command: {' '.join(command)}\n"
|
|
|
|
|
f"Output: {out}\nError: {err}\n"
|
|
|
|
|
f"Exit code: {returncode}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def execute_shell_command(
|
|
|
|
|
command: Sequence[str], *, env: Mapping[str, str] | None = None
|
|
|
|
|
) -> list[str]:
|
|
|
|
|
"""Run *command* and return its stdout lines.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
command: argv, the program first.
|
|
|
|
|
env: variables added to the child's environment, for values that must
|
|
|
|
|
not appear in the argv of a process listing.
|
|
|
|
|
"""
|
|
|
|
|
command = list(command)
|
|
|
|
|
print(" ".join(command), flush=True)
|
2025-12-26 18:13:26 +01:00
|
|
|
process = subprocess.Popen(
|
2026-08-17 16:30:25 +02:00
|
|
|
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=_child_env(env)
|
2025-12-26 18:13:26 +01:00
|
|
|
)
|
|
|
|
|
out, err = process.communicate()
|
|
|
|
|
if process.returncode != 0:
|
2026-08-17 16:30:25 +02:00
|
|
|
_fail(command, process.returncode, out, err)
|
2025-12-26 18:13:26 +01:00
|
|
|
return [line.decode("utf-8") for line in out.splitlines()]
|
2026-08-17 16:30:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def execute_to_file(
|
|
|
|
|
command: Sequence[str], out_file: str, *, env: Mapping[str, str] | None = None
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Run *command*, writing its stdout to *out_file* only once it succeeded.
|
|
|
|
|
|
|
|
|
|
The output goes to a sibling temporary file first, so a partial or empty
|
|
|
|
|
stream from a failing dump never takes the place of a valid backup.
|
|
|
|
|
"""
|
|
|
|
|
command = list(command)
|
|
|
|
|
print(" ".join(command), flush=True)
|
2026-08-18 01:47:38 +02:00
|
|
|
tmp = Path(f"{out_file}.tmp")
|
|
|
|
|
with tmp.open("wb") as handle:
|
2026-08-17 16:30:25 +02:00
|
|
|
process = subprocess.Popen(
|
|
|
|
|
command, stdout=handle, stderr=subprocess.PIPE, env=_child_env(env)
|
|
|
|
|
)
|
|
|
|
|
_, err = process.communicate()
|
|
|
|
|
if process.returncode != 0:
|
2026-08-18 01:47:38 +02:00
|
|
|
tmp.unlink()
|
2026-08-17 16:30:25 +02:00
|
|
|
_fail(command, process.returncode, b"", err)
|
2026-08-18 01:47:38 +02:00
|
|
|
tmp.replace(out_file)
|