Skip to content

Commit

Permalink
Extract function to inject macos version.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 21, 2024
1 parent 1288c31 commit 086558b
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions distutils/spawn.py
Expand Up @@ -12,9 +12,8 @@
import pathlib
import platform
import subprocess
import sys

from typing import Iterable
from typing import Iterable, Mapping

from ._log import log
from .debug import DEBUG
Expand All @@ -28,6 +27,21 @@ def _debug(cmd):
return cmd if DEBUG else cmd[0]


def _inject_macos_ver(env: Mapping[str:str] | None) -> Mapping[str:str] | None:
if platform.system() != 'Darwin':
return env

from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver

target_ver = get_macosx_target_ver()
update = {MACOSX_VERSION_VAR: target_ver} if target_ver else {}
return {**_resolve(env), **update}


def _resolve(env: Mapping[str:str] | None) -> Mapping[str:str]:
return os.environ if env is None else env


def spawn(cmd, search_path=True, verbose=False, dry_run=False, env=None):
"""Run another program, specified as a command list 'cmd', in a new process.
Expand All @@ -53,17 +67,8 @@ def spawn(cmd, search_path=True, verbose=False, dry_run=False, env=None):
if executable is not None:
cmd[0] = executable

env = env if env is not None else dict(os.environ)

if sys.platform == 'darwin':
from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver

macosx_target_ver = get_macosx_target_ver()
if macosx_target_ver:
env[MACOSX_VERSION_VAR] = macosx_target_ver

try:
subprocess.check_call(cmd, env=env)
subprocess.check_call(cmd, env=_inject_macos_ver(env))
except OSError as exc:
raise DistutilsExecError(
f"command {_debug(cmd)!r} failed: {exc.args[-1]}"
Expand Down

0 comments on commit 086558b

Please sign in to comment.