blob: 0aee2bc3eb4cd4f74629daa1cbdd32087060093c [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
Neal Norwitz9d72bb42007-04-17 08:48:32 +000011import sys, os
Greg Wardb4dbfb31999-08-14 23:57:17 +000012from distutils.errors import *
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):
Greg Wardb4dbfb31999-08-14 23:57:17 +000016 """Run another program, specified as a command list 'cmd', in a new
Greg Warda30f7ac2000-09-26 02:00:51 +000017 process. 'cmd' is just the argument list for the new process, ie.
18 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
19 There is no way to run a program with a name different from that of its
20 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000021
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000022 If 'search_path' is true (the default), the system's executable
23 search path will be used to find the program; otherwise, cmd[0]
24 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000025 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000026
Greg Warda30f7ac2000-09-26 02:00:51 +000027 Raise DistutilsExecError if running the program fails in any way; just
28 return on success.
29 """
Greg Wardb4dbfb31999-08-14 23:57:17 +000030 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000031 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000032 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000033 _spawn_nt(cmd, search_path, dry_run=dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000034 elif os.name == 'os2':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000035 _spawn_os2(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000036 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +000037 raise DistutilsPlatformError(
38 "don't know how to spawn programs on platform '%s'" % os.name)
Greg Wardb4dbfb31999-08-14 23:57:17 +000039
Greg Warda4d132a1999-09-08 02:23:28 +000040
Collin Winter5b7e9d72007-08-30 03:52:21 +000041def _nt_quote_args(args):
Greg Warda30f7ac2000-09-26 02:00:51 +000042 """Quote command-line arguments for DOS/Windows conventions: just
43 wraps every argument which contains blanks in double quotes, and
44 returns a new argument list.
45 """
Greg Warde2b44522000-03-07 03:25:20 +000046 # XXX this doesn't seem very robust to me -- but if the Windows guys
47 # say it'll work, I guess I'll have to accept it. (What if an arg
48 # contains quotes? What other magic characters, other than spaces,
49 # have to be escaped? Is there an escaping mechanism other than
50 # quoting?)
Greg Warda30f7ac2000-09-26 02:00:51 +000051 for i in range(len(args)):
Neal Norwitz9d72bb42007-04-17 08:48:32 +000052 if args[i].find(' ') != -1:
Greg Warde2b44522000-03-07 03:25:20 +000053 args[i] = '"%s"' % args[i]
Greg Warda3c8bf32000-03-26 21:47:00 +000054 return args
Greg Warde2b44522000-03-07 03:25:20 +000055
Collin Winter5b7e9d72007-08-30 03:52:21 +000056def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000057 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000058 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000059 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000060 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000061 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000062 log.info(' '.join([executable] + cmd[1:]))
Greg Ward69628b01999-08-29 18:20:56 +000063 if not dry_run:
64 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000065 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000066 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000067 except OSError as exc:
Greg Ward3b49c9b2000-01-17 21:57:55 +000068 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000069 raise DistutilsExecError(
70 "command '%s' failed: %s" % (cmd[0], exc[-1]))
Greg Ward69628b01999-08-29 18:20:56 +000071 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000072 # and this reflects the command running but failing
Collin Winter5b7e9d72007-08-30 03:52:21 +000073 raise DistutilsExecError(
74 "command '%s' failed with exit status %d" % (cmd[0], rc))
Greg Wardb4dbfb31999-08-14 23:57:17 +000075
Fred Drakeb94b8492001-12-06 20:51:35 +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]
79 #cmd = _nt_quote_args(cmd)
80 if search_path:
81 # either we find one or it stays the same
Tim Peters182b5ac2004-07-18 06:16:08 +000082 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000083 log.info(' '.join([executable] + cmd[1:]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000084 if not dry_run:
85 # spawnv for OS/2 EMX requires a full path to the .exe
86 try:
87 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000088 except OSError as exc:
Marc-André Lemburg2544f512002-01-31 18:56:00 +000089 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000090 raise DistutilsExecError(
91 "command '%s' failed: %s" % (cmd[0], exc[-1]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000092 if rc != 0:
93 # and this reflects the command running but failing
Guido van Rossumbe19ed72007-02-09 05:37:30 +000094 print("command '%s' failed with exit status %d" % (cmd[0], rc))
Collin Winter5b7e9d72007-08-30 03:52:21 +000095 raise DistutilsExecError(
96 "command '%s' failed with exit status %d" % (cmd[0], rc))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000097
98
Collin Winter5b7e9d72007-08-30 03:52:21 +000099def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000100 log.info(' '.join(cmd))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000101 if dry_run:
102 return
103 exec_fn = search_path and os.execvp or os.execv
104
Greg Warda30f7ac2000-09-26 02:00:51 +0000105 pid = os.fork()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000106 if pid == 0: # in the child
Greg Wardb4dbfb31999-08-14 23:57:17 +0000107 try:
Greg Warda30f7ac2000-09-26 02:00:51 +0000108 exec_fn(cmd[0], cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +0000109 except OSError as e:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000110 sys.stderr.write("unable to execute %s: %s\n"
111 % (cmd[0], e.strerror))
Greg Warda30f7ac2000-09-26 02:00:51 +0000112 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000113
Greg Warda30f7ac2000-09-26 02:00:51 +0000114 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
115 os._exit(1)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000116 else: # in the parent
Greg Wardb4dbfb31999-08-14 23:57:17 +0000117 # Loop until the child either exits or is terminated by a signal
118 # (ie. keep waiting if it's merely stopped)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000119 while True:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000120 try:
121 (pid, status) = os.waitpid(pid, 0)
Guido van Rossumb940e112007-01-10 16:19:56 +0000122 except OSError as exc:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000123 import errno
124 if exc.errno == errno.EINTR:
125 continue
Collin Winter5b7e9d72007-08-30 03:52:21 +0000126 raise DistutilsExecError(
127 "command '%s' failed: %s" % (cmd[0], exc[-1]))
Greg Warda30f7ac2000-09-26 02:00:51 +0000128 if os.WIFSIGNALED(status):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000129 raise DistutilsExecError(
130 "command '%s' terminated by signal %d"
131 % (cmd[0], os.WTERMSIG(status)))
Greg Warda30f7ac2000-09-26 02:00:51 +0000132 elif os.WIFEXITED(status):
133 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000134 if exit_status == 0:
135 return # hey, it succeeded!
136 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000137 raise DistutilsExecError(
138 "command '%s' failed with exit status %d"
139 % (cmd[0], exit_status))
Greg Warda30f7ac2000-09-26 02:00:51 +0000140 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000141 continue
Greg Wardb4dbfb31999-08-14 23:57:17 +0000142 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000143 raise DistutilsExecError(
144 "unknown error executing '%s': termination status %d"
145 % (cmd[0], status))
Greg Ward88608ca2000-08-02 01:08:02 +0000146
147
148def find_executable(executable, path=None):
149 """Try to find 'executable' in the directories listed in 'path' (a
150 string listing directories separated by 'os.pathsep'; defaults to
151 os.environ['PATH']). Returns the complete filename or None if not
152 found.
153 """
154 if path is None:
155 path = os.environ['PATH']
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000156 paths = path.split(os.pathsep)
Greg Ward88608ca2000-08-02 01:08:02 +0000157 (base, ext) = os.path.splitext(executable)
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000158 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000159 executable = executable + '.exe'
160 if not os.path.isfile(executable):
161 for p in paths:
162 f = os.path.join(p, executable)
163 if os.path.isfile(f):
164 # the file exists, we have a shot at spawn working
165 return f
166 return None
167 else:
168 return executable