Path: blob/master/venv/Lib/site-packages/setuptools/_distutils/spawn.py
811 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 DistutilsPlatformError, DistutilsExecError13from distutils.debug import DEBUG14from distutils import log151617if sys.platform == 'darwin':18_cfg_target = None19_cfg_target_split = None202122def spawn(cmd, search_path=1, verbose=0, dry_run=0):23"""Run another program, specified as a command list 'cmd', in a new process.2425'cmd' is just the argument list for the new process, ie.26cmd[0] is the program to run and cmd[1:] are the rest of its arguments.27There is no way to run a program with a name different from that of its28executable.2930If 'search_path' is true (the default), the system's executable31search path will be used to find the program; otherwise, cmd[0]32must be the exact path to the executable. If 'dry_run' is true,33the command will not actually be run.3435Raise DistutilsExecError if running the program fails in any way; just36return on success.37"""38# cmd is documented as a list, but just in case some code passes a tuple39# in, protect our %-formatting code against horrible death40cmd = list(cmd)4142log.info(' '.join(cmd))43if dry_run:44return4546if search_path:47executable = find_executable(cmd[0])48if executable is not None:49cmd[0] = executable5051env = None52if sys.platform == 'darwin':53global _cfg_target, _cfg_target_split54if _cfg_target is None:55from distutils import sysconfig56_cfg_target = sysconfig.get_config_var(57'MACOSX_DEPLOYMENT_TARGET') or ''58if _cfg_target:59_cfg_target_split = [int(x) for x in _cfg_target.split('.')]60if _cfg_target:61# ensure that the deployment target of build process is not less62# than that used when the interpreter was built. This ensures63# extension modules are built with correct compatibility values64cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)65if _cfg_target_split > [int(x) for x in cur_target.split('.')]:66my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '67'now "%s" but "%s" during configure'68% (cur_target, _cfg_target))69raise DistutilsPlatformError(my_msg)70env = dict(os.environ,71MACOSX_DEPLOYMENT_TARGET=cur_target)7273proc = subprocess.Popen(cmd, env=env)74proc.wait()75exitcode = proc.returncode7677if exitcode:78if not DEBUG:79cmd = cmd[0]80raise DistutilsExecError(81"command %r failed with exit code %s" % (cmd, exitcode))828384def find_executable(executable, path=None):85"""Tries to find 'executable' in the directories listed in 'path'.8687A string listing directories separated by 'os.pathsep'; defaults to88os.environ['PATH']. Returns the complete filename or None if not found.89"""90_, ext = os.path.splitext(executable)91if (sys.platform == 'win32') and (ext != '.exe'):92executable = executable + '.exe'9394if os.path.isfile(executable):95return executable9697if path is None:98path = os.environ.get('PATH', None)99if path is None:100try:101path = os.confstr("CS_PATH")102except (AttributeError, ValueError):103# os.confstr() or CS_PATH is not available104path = os.defpath105# bpo-35755: Don't use os.defpath if the PATH environment variable is106# set to an empty string107108# PATH='' doesn't match, whereas PATH=':' looks in the current directory109if not path:110return None111112paths = path.split(os.pathsep)113for p in paths:114f = os.path.join(p, executable)115if os.path.isfile(f):116# the file exists, we have a shot at spawn working117return f118return None119120121