Introduce a subprocess_run utility function

This is done to avoid forgetting windows specific arguments when calling `subprocess.run`.
This commit is contained in:
Alexis Métaireau 2025-02-26 16:09:58 +01:00
parent c313c6d1d7
commit 53a7028110
No known key found for this signature in database
GPG key ID: C65C7A89A8FFC56E

View file

@ -14,6 +14,11 @@ CONTAINER_NAME = "ghcr.io/freedomofpress/dangerzone/dangerzone"
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def subprocess_run(*args, **kwargs) -> subprocess.CompletedProcess:
"""subprocess.run with the correct startupinfo for Windows."""
return subprocess.run(*args, startupinfo=get_subprocess_startupinfo(), **kwargs)
def get_runtime_name() -> str: def get_runtime_name() -> str:
if platform.system() == "Linux": if platform.system() == "Linux":
return "podman" return "podman"
@ -39,9 +44,8 @@ def get_runtime_version() -> Tuple[int, int]:
cmd = [runtime, "version", "-f", query] cmd = [runtime, "version", "-f", query]
try: try:
version = subprocess.run( version = subprocess_run(
cmd, cmd,
startupinfo=get_subprocess_startupinfo(),
capture_output=True, capture_output=True,
check=True, check=True,
).stdout.decode() ).stdout.decode()
@ -144,8 +148,7 @@ def load_image_tarball_from_gzip() -> None:
def load_image_tarball_from_tar(tarball_path: str) -> None: def load_image_tarball_from_tar(tarball_path: str) -> None:
cmd = [get_runtime(), "load", "-i", tarball_path] cmd = [get_runtime(), "load", "-i", tarball_path]
subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) subprocess_run(cmd, check=True)
log.info("Successfully installed container image from %s", tarball_path) log.info("Successfully installed container image from %s", tarball_path)
@ -156,7 +159,7 @@ def tag_image_by_digest(digest: str, tag: str) -> None:
image_id = get_image_id_by_digest(digest) image_id = get_image_id_by_digest(digest)
cmd = [get_runtime(), "tag", image_id, tag] cmd = [get_runtime(), "tag", image_id, tag]
log.debug(" ".join(cmd)) log.debug(" ".join(cmd))
subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) subprocess_run(cmd, check=True)
def get_image_id_by_digest(digest: str) -> str: def get_image_id_by_digest(digest: str) -> str:
@ -172,9 +175,7 @@ def get_image_id_by_digest(digest: str) -> str:
"{{.Id}}", "{{.Id}}",
] ]
log.debug(" ".join(cmd)) log.debug(" ".join(cmd))
process = subprocess.run( process = subprocess_run(cmd, check=True, capture_output=True)
cmd, startupinfo=get_subprocess_startupinfo(), check=True, capture_output=True
)
# In case we have multiple lines, we only want the first one. # In case we have multiple lines, we only want the first one.
return process.stdout.decode().strip().split("\n")[0] return process.stdout.decode().strip().split("\n")[0]
@ -183,7 +184,7 @@ def container_pull(image: str, manifest_digest: str):
"""Pull a container image from a registry.""" """Pull a container image from a registry."""
cmd = [get_runtime_name(), "pull", f"{image}@sha256:{manifest_digest}"] cmd = [get_runtime_name(), "pull", f"{image}@sha256:{manifest_digest}"]
try: try:
subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) subprocess_run(cmd, check=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise errors.ContainerPullException( raise errors.ContainerPullException(
f"Could not pull the container image: {e}" f"Could not pull the container image: {e}"
@ -199,7 +200,11 @@ def get_local_image_digest(image: str) -> str:
cmd = [get_runtime_name(), "images", image, "--format", "{{.Digest}}"] cmd = [get_runtime_name(), "images", image, "--format", "{{.Digest}}"]
log.debug(" ".join(cmd)) log.debug(" ".join(cmd))
try: try:
result = subprocess.run(cmd, capture_output=True, check=True) result = subprocess_run(
cmd,
capture_output=True,
check=True,
)
lines = result.stdout.decode().strip().split("\n") lines = result.stdout.decode().strip().split("\n")
if len(lines) != 1: if len(lines) != 1:
raise errors.MultipleImagesFoundException( raise errors.MultipleImagesFoundException(