blob: 8c476dc23f5a87311536b1ddb2ee8549de9ba6ac [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é861d6442009-06-02 16:18:55 +000011import sys
12import os
13
14from distutils.errors import DistutilsPlatformError, DistutilsExecError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000016
Collin Winter5b7e9d72007-08-30 03:52:21 +000017def spawn(cmd, search_path=1, verbose=0, dry_run=0):
Tarek Ziadé861d6442009-06-02 16:18:55 +000018 """Run another program, specified as a command list 'cmd', in a new process.
19
20 'cmd' is just the argument list for the new process, ie.
Greg Warda30f7ac2000-09-26 02:00:51 +000021 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
22 There is no way to run a program with a name different from that of its
23 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000024
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000025 If 'search_path' is true (the default), the system's executable
26 search path will be used to find the program; otherwise, cmd[0]
27 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000028 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000029
Greg Warda30f7ac2000-09-26 02:00:51 +000030 Raise DistutilsExecError if running the program fails in any way; just
31 return on success.
32 """
Greg Wardb4dbfb31999-08-14 23:57:17 +000033 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000034 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000035 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000036 _spawn_nt(cmd, search_path, dry_run=dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000037 elif os.name == 'os2':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000038 _spawn_os2(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000039 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +000040 raise DistutilsPlatformError(
41 "don't know how to spawn programs on platform '%s'" % os.name)
Greg Wardb4dbfb31999-08-14 23:57:17 +000042
Collin Winter5b7e9d72007-08-30 03:52:21 +000043def _nt_quote_args(args):
Tarek Ziadé861d6442009-06-02 16:18:55 +000044 """Quote command-line arguments for DOS/Windows conventions.
45
46 Just wraps every argument which contains blanks in double quotes, and
Greg Warda30f7ac2000-09-26 02:00:51 +000047 returns a new argument list.
48 """
Greg Warde2b44522000-03-07 03:25:20 +000049 # XXX this doesn't seem very robust to me -- but if the Windows guys
50 # say it'll work, I guess I'll have to accept it. (What if an arg
51 # contains quotes? What other magic characters, other than spaces,
52 # have to be escaped? Is there an escaping mechanism other than
53 # quoting?)
Tarek Ziadé861d6442009-06-02 16:18:55 +000054 for i, arg in enumerate(args):
55 if ' ' in arg:
56 args[i] = '"%s"' % arg
Greg Warda3c8bf32000-03-26 21:47:00 +000057 return args
Greg Warde2b44522000-03-07 03:25:20 +000058
Collin Winter5b7e9d72007-08-30 03:52:21 +000059def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000060 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000061 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000062 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000063 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000064 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000065 log.info(' '.join([executable] + cmd[1:]))
Greg Ward69628b01999-08-29 18:20:56 +000066 if not dry_run:
67 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000068 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000069 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000070 except OSError as exc:
Greg Ward3b49c9b2000-01-17 21:57:55 +000071 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000072 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +000073 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Greg Ward69628b01999-08-29 18:20:56 +000074 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000075 # and this reflects the command running but failing
Collin Winter5b7e9d72007-08-30 03:52:21 +000076 raise DistutilsExecError(
77 "command '%s' failed with exit status %d" % (cmd[0], rc))
Greg Wardb4dbfb31999-08-14 23:57:17 +000078
Collin Winter5b7e9d72007-08-30 03:52:21 +000079def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
Marc-André Lemburg2544f512002-01-31 18:56:00 +000080 executable = cmd[0]
Marc-André Lemburg2544f512002-01-31 18:56:00 +000081 if search_path:
82 # either we find one or it stays the same
Tim Peters182b5ac2004-07-18 06:16:08 +000083 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000084 log.info(' '.join([executable] + cmd[1:]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000085 if not dry_run:
86 # spawnv for OS/2 EMX requires a full path to the .exe
87 try:
88 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000089 except OSError as exc:
Marc-André Lemburg2544f512002-01-31 18:56:00 +000090 # this seems to happen when the command isn't found
Collin Winter5b7e9d72007-08-30 03:52:21 +000091 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +000092 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000093 if rc != 0:
94 # and this reflects the command running but failing
Tarek Ziadé861d6442009-06-02 16:18:55 +000095 log.debug("command '%s' failed with exit status %d" % (cmd[0], rc))
Collin Winter5b7e9d72007-08-30 03:52:21 +000096 raise DistutilsExecError(
97 "command '%s' failed with exit status %d" % (cmd[0], rc))
Marc-André Lemburg2544f512002-01-31 18:56:00 +000098
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
Greg Warda30f7ac2000-09-26 02:00:51 +0000104 pid = os.fork()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000105 if pid == 0: # in the child
Greg Wardb4dbfb31999-08-14 23:57:17 +0000106 try:
Greg Warda30f7ac2000-09-26 02:00:51 +0000107 exec_fn(cmd[0], cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +0000108 except OSError as e:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000109 sys.stderr.write("unable to execute %s: %s\n"
110 % (cmd[0], e.strerror))
Greg Warda30f7ac2000-09-26 02:00:51 +0000111 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000112
Greg Warda30f7ac2000-09-26 02:00:51 +0000113 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
114 os._exit(1)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000115 else: # in the parent
Greg Wardb4dbfb31999-08-14 23:57:17 +0000116 # Loop until the child either exits or is terminated by a signal
117 # (ie. keep waiting if it's merely stopped)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000118 while True:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000119 try:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000120 pid, status = os.waitpid(pid, 0)
Guido van Rossumb940e112007-01-10 16:19:56 +0000121 except OSError as exc:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000122 import errno
123 if exc.errno == errno.EINTR:
124 continue
Collin Winter5b7e9d72007-08-30 03:52:21 +0000125 raise DistutilsExecError(
Georg Brandld11b68a2008-01-06 21:13:42 +0000126 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
Greg Warda30f7ac2000-09-26 02:00:51 +0000127 if os.WIFSIGNALED(status):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000128 raise DistutilsExecError(
129 "command '%s' terminated by signal %d"
130 % (cmd[0], os.WTERMSIG(status)))
Greg Warda30f7ac2000-09-26 02:00:51 +0000131 elif os.WIFEXITED(status):
132 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000133 if exit_status == 0:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000134 return # hey, it succeeded!
Greg Wardb4dbfb31999-08-14 23:57:17 +0000135 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000136 raise DistutilsExecError(
137 "command '%s' failed with exit status %d"
138 % (cmd[0], exit_status))
Greg Warda30f7ac2000-09-26 02:00:51 +0000139 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000140 continue
Greg Wardb4dbfb31999-08-14 23:57:17 +0000141 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000142 raise DistutilsExecError(
143 "unknown error executing '%s': termination status %d"
144 % (cmd[0], status))
Greg Ward88608ca2000-08-02 01:08:02 +0000145
Greg Ward88608ca2000-08-02 01:08:02 +0000146def find_executable(executable, path=None):
Tarek Ziadé861d6442009-06-02 16:18:55 +0000147 """Tries to find 'executable' in the directories listed in 'path'.
148
149 A string listing directories separated by 'os.pathsep'; defaults to
150 os.environ['PATH']. Returns the complete filename or None if not found.
Greg Ward88608ca2000-08-02 01:08:02 +0000151 """
152 if path is None:
153 path = os.environ['PATH']
Tarek Ziadé861d6442009-06-02 16:18:55 +0000154
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000155 paths = path.split(os.pathsep)
Tarek Ziadé861d6442009-06-02 16:18:55 +0000156 base, ext = os.path.splitext(executable)
157
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'
Tarek Ziadé861d6442009-06-02 16:18:55 +0000160
Greg Ward88608ca2000-08-02 01:08:02 +0000161 if not os.path.isfile(executable):
162 for p in paths:
163 f = os.path.join(p, executable)
164 if os.path.isfile(f):
165 # the file exists, we have a shot at spawn working
166 return f
167 return None
168 else:
169 return executable