Path: blob/main/test/lib/python3.9/site-packages/setuptools/_distutils/spawn.py
4799 views
"""distutils.spawn12Provides the 'spawn()' function, a front-end to various platform-3specific functions for launching another program in a sub-process.4Also provides the 'find_executable()' to search the path for a given5executable name.6"""78import sys9import os10import subprocess1112from distutils.errors import DistutilsExecError13from distutils.debug import DEBUG14from distutils import log151617def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):18"""Run another program, specified as a command list 'cmd', in a new process.1920'cmd' is just the argument list for the new process, ie.21cmd[0] is the program to run and cmd[1:] are the rest of its arguments.22There is no way to run a program with a name different from that of its23executable.2425If 'search_path' is true (the default), the system's executable26search path will be used to find the program; otherwise, cmd[0]27must be the exact path to the executable. If 'dry_run' is true,28the command will not actually be run.2930Raise DistutilsExecError if running the program fails in any way; just31return on success.32"""33# cmd is documented as a list, but just in case some code passes a tuple34# in, protect our %-formatting code against horrible death35cmd = list(cmd)3637log.info(subprocess.list2cmdline(cmd))38if dry_run:39return4041if search_path:42executable = find_executable(cmd[0])43if executable is not None:44cmd[0] = executable4546env = env if env is not None else dict(os.environ)4748if sys.platform == 'darwin':49from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver50macosx_target_ver = get_macosx_target_ver()51if macosx_target_ver:52env[MACOSX_VERSION_VAR] = macosx_target_ver5354try:55proc = subprocess.Popen(cmd, env=env)56proc.wait()57exitcode = proc.returncode58except OSError as exc:59if not DEBUG:60cmd = cmd[0]61raise DistutilsExecError(62"command %r failed: %s" % (cmd, exc.args[-1])) from exc6364if exitcode:65if not DEBUG:66cmd = cmd[0]67raise DistutilsExecError(68"command %r failed with exit code %s" % (cmd, exitcode))697071def find_executable(executable, path=None):72"""Tries to find 'executable' in the directories listed in 'path'.7374A string listing directories separated by 'os.pathsep'; defaults to75os.environ['PATH']. Returns the complete filename or None if not found.76"""77_, ext = os.path.splitext(executable)78if (sys.platform == 'win32') and (ext != '.exe'):79executable = executable + '.exe'8081if os.path.isfile(executable):82return executable8384if path is None:85path = os.environ.get('PATH', None)86if path is None:87try:88path = os.confstr("CS_PATH")89except (AttributeError, ValueError):90# os.confstr() or CS_PATH is not available91path = os.defpath92# bpo-35755: Don't use os.defpath if the PATH environment variable is93# set to an empty string9495# PATH='' doesn't match, whereas PATH=':' looks in the current directory96if not path:97return None9899paths = path.split(os.pathsep)100for p in paths:101f = os.path.join(p, executable)102if os.path.isfile(f):103# the file exists, we have a shot at spawn working104return f105return None106107108