blob: 0d1bd0391e6f1134e4472d7e133d8302d564a7dc [file] [log] [blame]
Greg Wardb4dbfb31999-08-14 23:57:17 +00001"""distutils.spawn
2
3Provides the 'spawn()' function, a front-end to various platform-
Greg Ward88608ca2000-08-02 01:08:02 +00004specific functions for launching another program in a sub-process.
5Also provides the 'find_executable()' to search the path for a given
Greg Warda30f7ac2000-09-26 02:00:51 +00006executable name.
7"""
Greg Wardb4dbfb31999-08-14 23:57:17 +00008
Tarek Ziadé861d6442009-06-02 16:18:55 +00009import sys
10import os
Victor Stinner1ec63b62020-03-04 14:50:19 +010011import subprocess
Tarek Ziadé861d6442009-06-02 16:18:55 +000012
13from distutils.errors import DistutilsPlatformError, DistutilsExecError
Éric Araujo45fc8712014-03-13 04:55:35 -040014from distutils.debug import DEBUG
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000016
Victor Stinner1ec63b62020-03-04 14:50:19 +010017
18if sys.platform == 'darwin':
19 _cfg_target = None
20 _cfg_target_split = None
21
22
Collin Winter5b7e9d72007-08-30 03:52:21 +000023def spawn(cmd, search_path=1, verbose=0, dry_run=0):
Tarek Ziadé861d6442009-06-02 16:18:55 +000024 """Run another program, specified as a command list 'cmd', in a new process.
25
26 'cmd' is just the argument list for the new process, ie.
Greg Warda30f7ac2000-09-26 02:00:51 +000027 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
28 There is no way to run a program with a name different from that of its
29 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000030
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000031 If 'search_path' is true (the default), the system's executable
32 search path will be used to find the program; otherwise, cmd[0]
33 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000034 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000035
Greg Warda30f7ac2000-09-26 02:00:51 +000036 Raise DistutilsExecError if running the program fails in any way; just
37 return on success.
38 """
Éric Araujo45fc8712014-03-13 04:55:35 -040039 # cmd is documented as a list, but just in case some code passes a tuple
40 # in, protect our %-formatting code against horrible death
41 cmd = list(cmd)
Greg Wardb4dbfb31999-08-14 23:57:17 +000042
Neal Norwitz9d72bb42007-04-17 08:48:32 +000043 log.info(' '.join(cmd))
Greg Wardb4dbfb31999-08-14 23:57:17 +000044 if dry_run:
45 return
Victor Stinner1ec63b62020-03-04 14:50:19 +010046
47 if search_path:
48 executable = find_executable(cmd[0])
49 if executable is not None:
50 cmd[0] = executable
51
Éric Araujo45fc8712014-03-13 04:55:35 -040052 env = None
Ned Deilya8f8b502011-06-28 19:44:24 -070053 if sys.platform == 'darwin':
54 global _cfg_target, _cfg_target_split
55 if _cfg_target is None:
Paul Monson62dfd7d2019-04-25 11:36:45 -070056 from distutils import sysconfig
Ronald Oussoren49926cf2021-02-01 04:29:44 +010057 _cfg_target = sysconfig.get_config_var(
58 'MACOSX_DEPLOYMENT_TARGET') or ''
Ned Deilya8f8b502011-06-28 19:44:24 -070059 if _cfg_target:
60 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
61 if _cfg_target:
62 # ensure that the deployment target of build process is not less
63 # than that used when the interpreter was built. This ensures
64 # extension modules are built with correct compatibility values
65 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
66 if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
67 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
68 'now "%s" but "%s" during configure'
69 % (cur_target, _cfg_target))
70 raise DistutilsPlatformError(my_msg)
71 env = dict(os.environ,
72 MACOSX_DEPLOYMENT_TARGET=cur_target)
Fred Drakeb94b8492001-12-06 20:51:35 +000073
Jason R. Coombs6ae27802020-07-07 07:11:28 -040074 try:
75 proc = subprocess.Popen(cmd, env=env)
76 proc.wait()
77 exitcode = proc.returncode
78 except OSError as exc:
79 if not DEBUG:
80 cmd = cmd[0]
81 raise DistutilsExecError(
82 "command %r failed: %s" % (cmd, exc.args[-1])) from exc
Victor Stinner1ec63b62020-03-04 14:50:19 +010083
84 if exitcode:
Éric Araujo45fc8712014-03-13 04:55:35 -040085 if not DEBUG:
Victor Stinner1ec63b62020-03-04 14:50:19 +010086 cmd = cmd[0]
87 raise DistutilsExecError(
88 "command %r failed with exit code %s" % (cmd, exitcode))
89
Greg Ward88608ca2000-08-02 01:08:02 +000090
Greg Ward88608ca2000-08-02 01:08:02 +000091def find_executable(executable, path=None):
Tarek Ziadé861d6442009-06-02 16:18:55 +000092 """Tries to find 'executable' in the directories listed in 'path'.
93
94 A string listing directories separated by 'os.pathsep'; defaults to
95 os.environ['PATH']. Returns the complete filename or None if not found.
Greg Ward88608ca2000-08-02 01:08:02 +000096 """
Victor Stinner228a3c92019-04-17 16:26:36 +020097 _, ext = os.path.splitext(executable)
Jesus Cead17833d2012-10-11 01:20:12 +020098 if (sys.platform == 'win32') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +000099 executable = executable + '.exe'
Tarek Ziadé861d6442009-06-02 16:18:55 +0000100
Victor Stinner228a3c92019-04-17 16:26:36 +0200101 if os.path.isfile(executable):
Greg Ward88608ca2000-08-02 01:08:02 +0000102 return executable
Victor Stinner228a3c92019-04-17 16:26:36 +0200103
104 if path is None:
105 path = os.environ.get('PATH', None)
106 if path is None:
107 try:
108 path = os.confstr("CS_PATH")
109 except (AttributeError, ValueError):
110 # os.confstr() or CS_PATH is not available
111 path = os.defpath
112 # bpo-35755: Don't use os.defpath if the PATH environment variable is
Victor Stinner197f0442019-04-17 17:44:06 +0200113 # set to an empty string
Victor Stinner228a3c92019-04-17 16:26:36 +0200114
115 # PATH='' doesn't match, whereas PATH=':' looks in the current directory
116 if not path:
117 return None
118
119 paths = path.split(os.pathsep)
120 for p in paths:
121 f = os.path.join(p, executable)
122 if os.path.isfile(f):
123 # the file exists, we have a shot at spawn working
124 return f
125 return None