blob: 321344a3a5833f18a97c3596fbe8cbedb5c838c5 [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
Greg Ward3ce77fd2000-03-02 01:49:45 +00009__revision__ = "$Id$"
Greg Wardb4dbfb31999-08-14 23:57:17 +000010
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000011import sys
12import os
13
14from distutils.errors import DistutilsPlatformError, DistutilsExecError
Éric Araujo31fe52d2014-03-12 22:19:39 -040015from distutils.debug import DEBUG
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000016from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000017
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000018def spawn(cmd, search_path=1, verbose=0, dry_run=0):
19 """Run another program, specified as a command list 'cmd', in a new process.
Greg Wardb4dbfb31999-08-14 23:57:17 +000020
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000021 'cmd' is just the argument list for the new process, ie.
Greg Warda30f7ac2000-09-26 02:00:51 +000022 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
23 There is no way to run a program with a name different from that of its
24 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000025
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000026 If 'search_path' is true (the default), the system's executable
27 search path will be used to find the program; otherwise, cmd[0]
28 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000029 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000030
Greg Warda30f7ac2000-09-26 02:00:51 +000031 Raise DistutilsExecError if running the program fails in any way; just
32 return on success.
33 """
Éric Araujo31fe52d2014-03-12 22:19:39 -040034 # cmd is documented as a list, but just in case some code passes a tuple
35 # in, protect our %-formatting code against horrible death
36 cmd = list(cmd)
Greg Wardb4dbfb31999-08-14 23:57:17 +000037 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000038 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000039 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000040 _spawn_nt(cmd, search_path, dry_run=dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000041 elif os.name == 'os2':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000042 _spawn_os2(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000043 else:
44 raise DistutilsPlatformError, \
45 "don't know how to spawn programs on platform '%s'" % os.name
46
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000047def _nt_quote_args(args):
48 """Quote command-line arguments for DOS/Windows conventions.
Greg Wardb4dbfb31999-08-14 23:57:17 +000049
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000050 Just wraps every argument which contains blanks in double quotes, and
Greg Warda30f7ac2000-09-26 02:00:51 +000051 returns a new argument list.
52 """
Greg Warde2b44522000-03-07 03:25:20 +000053 # XXX this doesn't seem very robust to me -- but if the Windows guys
54 # say it'll work, I guess I'll have to accept it. (What if an arg
55 # contains quotes? What other magic characters, other than spaces,
56 # have to be escaped? Is there an escaping mechanism other than
57 # quoting?)
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000058 for i, arg in enumerate(args):
59 if ' ' in arg:
60 args[i] = '"%s"' % arg
Greg Warda3c8bf32000-03-26 21:47:00 +000061 return args
Greg Warde2b44522000-03-07 03:25:20 +000062
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000063def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000064 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000065 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000066 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000067 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000068 executable = find_executable(executable) or executable
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000069 log.info(' '.join([executable] + cmd[1:]))
Greg Ward69628b01999-08-29 18:20:56 +000070 if not dry_run:
71 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000072 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000073 rc = os.spawnv(os.P_WAIT, executable, cmd)
Greg Ward3b49c9b2000-01-17 21:57:55 +000074 except OSError, exc:
75 # this seems to happen when the command isn't found
Éric Araujo31fe52d2014-03-12 22:19:39 -040076 if not DEBUG:
77 cmd = executable
Greg Ward3b49c9b2000-01-17 21:57:55 +000078 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -040079 "command %r failed: %s" % (cmd, exc[-1])
Greg Ward69628b01999-08-29 18:20:56 +000080 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000081 # and this reflects the command running but failing
Éric Araujo31fe52d2014-03-12 22:19:39 -040082 if not DEBUG:
83 cmd = executable
Greg Ward3b49c9b2000-01-17 21:57:55 +000084 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -040085 "command %r failed with exit status %d" % (cmd, rc)
Greg Wardb4dbfb31999-08-14 23:57:17 +000086
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000087def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
Marc-André Lemburg2544f512002-01-31 18:56:00 +000088 executable = cmd[0]
Marc-André Lemburg2544f512002-01-31 18:56:00 +000089 if search_path:
90 # either we find one or it stays the same
Tim Peters182b5ac2004-07-18 06:16:08 +000091 executable = find_executable(executable) or executable
Tarek Ziadéabb4ec62009-06-02 15:58:43 +000092 log.info(' '.join([executable] + cmd[1:]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000093 if not dry_run:
94 # spawnv for OS/2 EMX requires a full path to the .exe
95 try:
96 rc = os.spawnv(os.P_WAIT, executable, cmd)
97 except OSError, exc:
98 # this seems to happen when the command isn't found
Éric Araujo31fe52d2014-03-12 22:19:39 -040099 if not DEBUG:
100 cmd = executable
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000101 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400102 "command %r failed: %s" % (cmd, exc[-1])
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000103 if rc != 0:
104 # and this reflects the command running but failing
Éric Araujo31fe52d2014-03-12 22:19:39 -0400105 if not DEBUG:
106 cmd = executable
107 log.debug("command %r failed with exit status %d" % (cmd, rc))
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000108 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400109 "command %r failed with exit status %d" % (cmd, rc)
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000110
Ned Deily041645a2011-06-28 19:40:39 -0700111if sys.platform == 'darwin':
112 from distutils import sysconfig
113 _cfg_target = None
114 _cfg_target_split = None
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000115
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000116def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
117 log.info(' '.join(cmd))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000118 if dry_run:
119 return
Éric Araujo31fe52d2014-03-12 22:19:39 -0400120 executable = cmd[0]
Greg Wardb4dbfb31999-08-14 23:57:17 +0000121 exec_fn = search_path and os.execvp or os.execv
Éric Araujo31fe52d2014-03-12 22:19:39 -0400122 env = None
Ned Deily041645a2011-06-28 19:40:39 -0700123 if sys.platform == 'darwin':
124 global _cfg_target, _cfg_target_split
125 if _cfg_target is None:
126 _cfg_target = sysconfig.get_config_var(
127 'MACOSX_DEPLOYMENT_TARGET') or ''
128 if _cfg_target:
129 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
130 if _cfg_target:
131 # ensure that the deployment target of build process is not less
132 # than that used when the interpreter was built. This ensures
133 # extension modules are built with correct compatibility values
134 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
135 if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
136 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
137 'now "%s" but "%s" during configure'
138 % (cur_target, _cfg_target))
139 raise DistutilsPlatformError(my_msg)
140 env = dict(os.environ,
141 MACOSX_DEPLOYMENT_TARGET=cur_target)
142 exec_fn = search_path and os.execvpe or os.execve
Greg Warda30f7ac2000-09-26 02:00:51 +0000143 pid = os.fork()
Greg Wardb4dbfb31999-08-14 23:57:17 +0000144
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000145 if pid == 0: # in the child
Greg Wardb4dbfb31999-08-14 23:57:17 +0000146 try:
Éric Araujo31fe52d2014-03-12 22:19:39 -0400147 if env is None:
148 exec_fn(executable, cmd)
149 else:
150 exec_fn(executable, cmd, env)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000151 except OSError, e:
Éric Araujo31fe52d2014-03-12 22:19:39 -0400152 if not DEBUG:
153 cmd = executable
154 sys.stderr.write("unable to execute %r: %s\n" %
155 (cmd, e.strerror))
Greg Warda30f7ac2000-09-26 02:00:51 +0000156 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000157
Éric Araujo31fe52d2014-03-12 22:19:39 -0400158 if not DEBUG:
159 cmd = executable
160 sys.stderr.write("unable to execute %r for unknown reasons" % cmd)
Greg Warda30f7ac2000-09-26 02:00:51 +0000161 os._exit(1)
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000162 else: # in the parent
Greg Wardb4dbfb31999-08-14 23:57:17 +0000163 # Loop until the child either exits or is terminated by a signal
164 # (ie. keep waiting if it's merely stopped)
165 while 1:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000166 try:
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000167 pid, status = os.waitpid(pid, 0)
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000168 except OSError, exc:
169 import errno
170 if exc.errno == errno.EINTR:
171 continue
Éric Araujo31fe52d2014-03-12 22:19:39 -0400172 if not DEBUG:
173 cmd = executable
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000174 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400175 "command %r failed: %s" % (cmd, exc[-1])
Greg Warda30f7ac2000-09-26 02:00:51 +0000176 if os.WIFSIGNALED(status):
Éric Araujo31fe52d2014-03-12 22:19:39 -0400177 if not DEBUG:
178 cmd = executable
Greg Wardb4dbfb31999-08-14 23:57:17 +0000179 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400180 "command %r terminated by signal %d" % \
181 (cmd, os.WTERMSIG(status))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000182
Greg Warda30f7ac2000-09-26 02:00:51 +0000183 elif os.WIFEXITED(status):
184 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000185 if exit_status == 0:
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000186 return # hey, it succeeded!
Greg Wardb4dbfb31999-08-14 23:57:17 +0000187 else:
Éric Araujo31fe52d2014-03-12 22:19:39 -0400188 if not DEBUG:
189 cmd = executable
Greg Wardb4dbfb31999-08-14 23:57:17 +0000190 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400191 "command %r failed with exit status %d" % \
192 (cmd, exit_status)
Fred Drakeb94b8492001-12-06 20:51:35 +0000193
Greg Warda30f7ac2000-09-26 02:00:51 +0000194 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000195 continue
196
197 else:
Éric Araujo31fe52d2014-03-12 22:19:39 -0400198 if not DEBUG:
199 cmd = executable
Greg Wardb4dbfb31999-08-14 23:57:17 +0000200 raise DistutilsExecError, \
Éric Araujo31fe52d2014-03-12 22:19:39 -0400201 "unknown error executing %r: termination status %d" % \
202 (cmd, status)
Greg Ward88608ca2000-08-02 01:08:02 +0000203
204def find_executable(executable, path=None):
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000205 """Tries to find 'executable' in the directories listed in 'path'.
206
207 A string listing directories separated by 'os.pathsep'; defaults to
208 os.environ['PATH']. Returns the complete filename or None if not found.
Greg Ward88608ca2000-08-02 01:08:02 +0000209 """
210 if path is None:
211 path = os.environ['PATH']
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000212 paths = path.split(os.pathsep)
213 base, ext = os.path.splitext(executable)
214
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000215 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000216 executable = executable + '.exe'
Tarek Ziadéabb4ec62009-06-02 15:58:43 +0000217
Greg Ward88608ca2000-08-02 01:08:02 +0000218 if not os.path.isfile(executable):
219 for p in paths:
220 f = os.path.join(p, executable)
221 if os.path.isfile(f):
222 # the file exists, we have a shot at spawn working
223 return f
224 return None
225 else:
226 return executable