blob: f58c55f90241d04ffdc79b9d7ebed38b1068e1bb [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
11
12from distutils.errors import DistutilsPlatformError, DistutilsExecError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000013from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000014
Collin Winter5b7e9d72007-08-30 03:52:21 +000015def spawn(cmd, search_path=1, verbose=0, dry_run=0):
Tarek Ziadé861d6442009-06-02 16:18:55 +000016 """Run another program, specified as a command list 'cmd', in a new process.
17
18 'cmd' is just the argument list for the new process, ie.
Greg Warda30f7ac2000-09-26 02:00:51 +000019 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
20 There is no way to run a program with a name different from that of its
21 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000022
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000023 If 'search_path' is true (the default), the system's executable
24 search path will be used to find the program; otherwise, cmd[0]
25 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000026 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000027
Greg Warda30f7ac2000-09-26 02:00:51 +000028 Raise DistutilsExecError if running the program fails in any way; just
29 return on success.
30 """
Greg Wardb4dbfb31999-08-14 23:57:17 +000031 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000032 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000033 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000034 _spawn_nt(cmd, search_path, dry_run=dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000035 elif os.name == 'os2':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000036 _spawn_os2(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000037 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +000038 raise DistutilsPlatformError(
39 "don't know how to spawn programs on platform '%s'" % os.name)
Greg Wardb4dbfb31999-08-14 23:57:17 +000040
Collin Winter5b7e9d72007-08-30 03:52:21 +000041def _nt_quote_args(args):
Tarek Ziadé861d6442009-06-02 16:18:55 +000042 """Quote command-line arguments for DOS/Windows conventions.
43
44 Just wraps every argument which contains blanks in double quotes, and
Greg Warda30f7ac2000-09-26 02:00:51 +000045 returns a new argument list.
46 """
Greg Warde2b44522000-03-07 03:25:20 +000047 # XXX this doesn't seem very robust to me -- but if the Windows guys
48 # say it'll work, I guess I'll have to accept it. (What if an arg
49 # contains quotes? What other magic characters, other than spaces,
50 # have to be escaped? Is there an escaping mechanism other than
51 # quoting?)
Tarek Ziadé861d6442009-06-02 16:18:55 +000052 for i, arg in enumerate(args):
53 if ' ' in arg:
54 args[i] = '"%s"' % arg
Greg Warda3c8bf32000-03-26 21:47:00 +000055 return args
Greg Warde2b44522000-03-07 03:25:20 +000056
Collin Winter5b7e9d72007-08-30 03:52:21 +000057def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000058 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000059 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000060 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000061 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000062 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000063 log.info(' '.join([executable] + cmd[1:]))
Greg Ward69628b01999-08-29 18:20:56 +000064 if not dry_run:
65 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000066 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000067 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000068 except OSError as exc:
Greg Ward3b49c9b2000-01-17 21:57:55 +000069 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000070 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +000071 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Greg Ward69628b01999-08-29 18:20:56 +000072 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000073 # and this reflects the command running but failing
Collin Winter5b7e9d72007-08-30 03:52:21 +000074 raise DistutilsExecError(
75 "command '%s' failed with exit status %d" % (cmd[0], rc))
Greg Wardb4dbfb31999-08-14 23:57:17 +000076
Collin Winter5b7e9d72007-08-30 03:52:21 +000077def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
Marc-André Lemburg2544f512002-01-31 18:56:00 +000078 executable = cmd[0]
Marc-André Lemburg2544f512002-01-31 18:56:00 +000079 if search_path:
80 # either we find one or it stays the same
Tim Peters182b5ac2004-07-18 06:16:08 +000081 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000082 log.info(' '.join([executable] + cmd[1:]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000083 if not dry_run:
84 # spawnv for OS/2 EMX requires a full path to the .exe
85 try:
86 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000087 except OSError as exc:
Marc-André Lemburg2544f512002-01-31 18:56:00 +000088 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000089 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +000090 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000091 if rc != 0:
92 # and this reflects the command running but failing
Tarek Ziadé861d6442009-06-02 16:18:55 +000093 log.debug("command '%s' failed with exit status %d" % (cmd[0], rc))
Collin Winter5b7e9d72007-08-30 03:52:21 +000094 raise DistutilsExecError(
95 "command '%s' failed with exit status %d" % (cmd[0], rc))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000096
Ned Deilya8f8b502011-06-28 19:44:24 -070097if sys.platform == 'darwin':
98 from distutils import sysconfig
99 _cfg_target = None
100 _cfg_target_split = None
101
Collin Winter5b7e9d72007-08-30 03:52:21 +0000102def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000103 log.info(' '.join(cmd))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000104 if dry_run:
105 return
106 exec_fn = search_path and os.execvp or os.execv
Ned Deilya8f8b502011-06-28 19:44:24 -0700107 exec_args = [cmd[0], cmd]
108 if sys.platform == 'darwin':
109 global _cfg_target, _cfg_target_split
110 if _cfg_target is None:
111 _cfg_target = sysconfig.get_config_var(
112 'MACOSX_DEPLOYMENT_TARGET') or ''
113 if _cfg_target:
114 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
115 if _cfg_target:
116 # ensure that the deployment target of build process is not less
117 # than that used when the interpreter was built. This ensures
118 # extension modules are built with correct compatibility values
119 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
120 if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
121 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
122 'now "%s" but "%s" during configure'
123 % (cur_target, _cfg_target))
124 raise DistutilsPlatformError(my_msg)
125 env = dict(os.environ,
126 MACOSX_DEPLOYMENT_TARGET=cur_target)
127 exec_fn = search_path and os.execvpe or os.execve
128 exec_args.append(env)
Greg Warda30f7ac2000-09-26 02:00:51 +0000129 pid = os.fork()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000130 if pid == 0: # in the child
Greg Wardb4dbfb31999-08-14 23:57:17 +0000131 try:
Ned Deilya8f8b502011-06-28 19:44:24 -0700132 exec_fn(*exec_args)
Guido van Rossumb940e112007-01-10 16:19:56 +0000133 except OSError as e:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000134 sys.stderr.write("unable to execute %s: %s\n"
135 % (cmd[0], e.strerror))
Greg Warda30f7ac2000-09-26 02:00:51 +0000136 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000137
Greg Warda30f7ac2000-09-26 02:00:51 +0000138 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
139 os._exit(1)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000140 else: # in the parent
Greg Wardb4dbfb31999-08-14 23:57:17 +0000141 # Loop until the child either exits or is terminated by a signal
142 # (ie. keep waiting if it's merely stopped)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000143 while True:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000144 try:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000145 pid, status = os.waitpid(pid, 0)
Guido van Rossumb940e112007-01-10 16:19:56 +0000146 except OSError as exc:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000147 import errno
148 if exc.errno == errno.EINTR:
149 continue
Collin Winter5b7e9d72007-08-30 03:52:21 +0000150 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +0000151 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Greg Warda30f7ac2000-09-26 02:00:51 +0000152 if os.WIFSIGNALED(status):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000153 raise DistutilsExecError(
154 "command '%s' terminated by signal %d"
155 % (cmd[0], os.WTERMSIG(status)))
Greg Warda30f7ac2000-09-26 02:00:51 +0000156 elif os.WIFEXITED(status):
157 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000158 if exit_status == 0:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000159 return # hey, it succeeded!
Greg Wardb4dbfb31999-08-14 23:57:17 +0000160 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000161 raise DistutilsExecError(
162 "command '%s' failed with exit status %d"
163 % (cmd[0], exit_status))
Greg Warda30f7ac2000-09-26 02:00:51 +0000164 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000165 continue
Greg Wardb4dbfb31999-08-14 23:57:17 +0000166 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000167 raise DistutilsExecError(
168 "unknown error executing '%s': termination status %d"
169 % (cmd[0], status))
Greg Ward88608ca2000-08-02 01:08:02 +0000170
Greg Ward88608ca2000-08-02 01:08:02 +0000171def find_executable(executable, path=None):
Tarek Ziadé861d6442009-06-02 16:18:55 +0000172 """Tries to find 'executable' in the directories listed in 'path'.
173
174 A string listing directories separated by 'os.pathsep'; defaults to
175 os.environ['PATH']. Returns the complete filename or None if not found.
Greg Ward88608ca2000-08-02 01:08:02 +0000176 """
177 if path is None:
178 path = os.environ['PATH']
Tarek Ziadé861d6442009-06-02 16:18:55 +0000179
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000180 paths = path.split(os.pathsep)
Tarek Ziadé861d6442009-06-02 16:18:55 +0000181 base, ext = os.path.splitext(executable)
182
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000183 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000184 executable = executable + '.exe'
Tarek Ziadé861d6442009-06-02 16:18:55 +0000185
Greg Ward88608ca2000-08-02 01:08:02 +0000186 if not os.path.isfile(executable):
187 for p in paths:
188 f = os.path.join(p, executable)
189 if os.path.isfile(f):
190 # the file exists, we have a shot at spawn working
191 return f
192 return None
193 else:
194 return executable