Coverage for lintro / utils / env.py: 83%
12 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Environment utilities for subprocess execution."""
3from __future__ import annotations
5import os
6import tempfile
7from pathlib import Path
9from loguru import logger
12def get_subprocess_env() -> dict[str, str]:
13 """Build an environment dict suitable for subprocess calls.
15 Copies the current environment and ensures HOME points to a writable
16 directory. When the real HOME is missing, not a directory, or not
17 writable (e.g., Docker with ``--user "$(id -u):$(id -g)"``), HOME is
18 redirected to the system temp directory so tools that need cache or
19 config directories under ``$HOME`` don't fail with permission errors.
21 Returns:
22 A copy of ``os.environ`` with HOME guaranteed to be writable.
23 """
24 env = os.environ.copy()
25 home = env.get("HOME", "")
26 if not home or not Path(home).is_dir() or not os.access(home, os.W_OK):
27 env["HOME"] = tempfile.gettempdir()
28 logger.debug(
29 "[env] HOME '{}' is unwritable, redirecting to {}",
30 home,
31 env["HOME"],
32 )
33 return env