blob: e5654ff0096b07917241825b130e130225bf318f [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
Martin v. Löwis5a6601c2004-11-10 22:23:15 +00009# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +000010
Greg Ward3ce77fd2000-03-02 01:49:45 +000011__revision__ = "$Id$"
Greg Wardb4dbfb31999-08-14 23:57:17 +000012
13import sys, os, string
14from distutils.errors import *
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000016
17def spawn (cmd,
18 search_path=1,
19 verbose=0,
20 dry_run=0):
21
22 """Run another program, specified as a command list 'cmd', in a new
Greg Warda30f7ac2000-09-26 02:00:51 +000023 process. 'cmd' is just the argument list for the new process, ie.
24 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
25 There is no way to run a program with a name different from that of its
26 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000027
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000028 If 'search_path' is true (the default), the system's executable
29 search path will be used to find the program; otherwise, cmd[0]
30 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000031 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000032
Greg Warda30f7ac2000-09-26 02:00:51 +000033 Raise DistutilsExecError if running the program fails in any way; just
34 return on success.
35 """
Greg Wardb4dbfb31999-08-14 23:57:17 +000036 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000037 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000038 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000039 _spawn_nt(cmd, search_path, dry_run=dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000040 elif os.name == 'os2':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000041 _spawn_os2(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000042 else:
43 raise DistutilsPlatformError, \
44 "don't know how to spawn programs on platform '%s'" % os.name
45
46# spawn ()
47
Greg Warda4d132a1999-09-08 02:23:28 +000048
Greg Warde2b44522000-03-07 03:25:20 +000049def _nt_quote_args (args):
Greg Warda30f7ac2000-09-26 02:00:51 +000050 """Quote command-line arguments for DOS/Windows conventions: just
51 wraps every argument which contains blanks in double quotes, and
52 returns a new argument list.
53 """
Greg Warde2b44522000-03-07 03:25:20 +000054
55 # XXX this doesn't seem very robust to me -- but if the Windows guys
56 # say it'll work, I guess I'll have to accept it. (What if an arg
57 # contains quotes? What other magic characters, other than spaces,
58 # have to be escaped? Is there an escaping mechanism other than
59 # quoting?)
60
Greg Warda30f7ac2000-09-26 02:00:51 +000061 for i in range(len(args)):
62 if string.find(args[i], ' ') != -1:
Greg Warde2b44522000-03-07 03:25:20 +000063 args[i] = '"%s"' % args[i]
Greg Warda3c8bf32000-03-26 21:47:00 +000064 return args
Greg Warde2b44522000-03-07 03:25:20 +000065
66def _spawn_nt (cmd,
67 search_path=1,
68 verbose=0,
69 dry_run=0):
70
Greg Ward69628b01999-08-29 18:20:56 +000071 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000072 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000073 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000074 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000075 executable = find_executable(executable) or executable
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000076 log.info(string.join([executable] + cmd[1:], ' '))
Greg Ward69628b01999-08-29 18:20:56 +000077 if not dry_run:
78 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000079 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000080 rc = os.spawnv(os.P_WAIT, executable, cmd)
Greg Ward3b49c9b2000-01-17 21:57:55 +000081 except OSError, exc:
82 # this seems to happen when the command isn't found
83 raise DistutilsExecError, \
84 "command '%s' failed: %s" % (cmd[0], exc[-1])
Greg Ward69628b01999-08-29 18:20:56 +000085 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000086 # and this reflects the command running but failing
87 raise DistutilsExecError, \
88 "command '%s' failed with exit status %d" % (cmd[0], rc)
Greg Wardb4dbfb31999-08-14 23:57:17 +000089
Fred Drakeb94b8492001-12-06 20:51:35 +000090
Marc-André Lemburg2544f512002-01-31 18:56:00 +000091def _spawn_os2 (cmd,
92 search_path=1,
93 verbose=0,
94 dry_run=0):
95
96 executable = cmd[0]
97 #cmd = _nt_quote_args(cmd)
98 if search_path:
99 # either we find one or it stays the same
Tim Peters182b5ac2004-07-18 06:16:08 +0000100 executable = find_executable(executable) or executable
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000101 log.info(string.join([executable] + cmd[1:], ' '))
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000102 if not dry_run:
103 # spawnv for OS/2 EMX requires a full path to the .exe
104 try:
105 rc = os.spawnv(os.P_WAIT, executable, cmd)
106 except OSError, exc:
107 # this seems to happen when the command isn't found
108 raise DistutilsExecError, \
109 "command '%s' failed: %s" % (cmd[0], exc[-1])
110 if rc != 0:
111 # and this reflects the command running but failing
112 print "command '%s' failed with exit status %d" % (cmd[0], rc)
113 raise DistutilsExecError, \
114 "command '%s' failed with exit status %d" % (cmd[0], rc)
115
116
Greg Wardb4dbfb31999-08-14 23:57:17 +0000117def _spawn_posix (cmd,
118 search_path=1,
119 verbose=0,
120 dry_run=0):
121
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000122 log.info(string.join(cmd, ' '))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000123 if dry_run:
124 return
125 exec_fn = search_path and os.execvp or os.execv
126
Greg Warda30f7ac2000-09-26 02:00:51 +0000127 pid = os.fork()
Greg Wardb4dbfb31999-08-14 23:57:17 +0000128
129 if pid == 0: # in the child
130 try:
131 #print "cmd[0] =", cmd[0]
132 #print "cmd =", cmd
Greg Warda30f7ac2000-09-26 02:00:51 +0000133 exec_fn(cmd[0], cmd)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000134 except OSError, e:
Greg Warda30f7ac2000-09-26 02:00:51 +0000135 sys.stderr.write("unable to execute %s: %s\n" %
136 (cmd[0], e.strerror))
137 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000138
Greg Warda30f7ac2000-09-26 02:00:51 +0000139 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
140 os._exit(1)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000141
Fred Drakeb94b8492001-12-06 20:51:35 +0000142
Greg Wardb4dbfb31999-08-14 23:57:17 +0000143 else: # in the parent
144 # Loop until the child either exits or is terminated by a signal
145 # (ie. keep waiting if it's merely stopped)
146 while 1:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000147 try:
148 (pid, status) = os.waitpid(pid, 0)
149 except OSError, exc:
150 import errno
151 if exc.errno == errno.EINTR:
152 continue
153 raise DistutilsExecError, \
154 "command '%s' failed: %s" % (cmd[0], exc[-1])
Greg Warda30f7ac2000-09-26 02:00:51 +0000155 if os.WIFSIGNALED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000156 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000157 "command '%s' terminated by signal %d" % \
Greg Warda30f7ac2000-09-26 02:00:51 +0000158 (cmd[0], os.WTERMSIG(status))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000159
Greg Warda30f7ac2000-09-26 02:00:51 +0000160 elif os.WIFEXITED(status):
161 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000162 if exit_status == 0:
163 return # hey, it succeeded!
164 else:
165 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000166 "command '%s' failed with exit status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000167 (cmd[0], exit_status)
Fred Drakeb94b8492001-12-06 20:51:35 +0000168
Greg Warda30f7ac2000-09-26 02:00:51 +0000169 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000170 continue
171
172 else:
173 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000174 "unknown error executing '%s': termination status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000175 (cmd[0], status)
176# _spawn_posix ()
Greg Ward88608ca2000-08-02 01:08:02 +0000177
178
179def find_executable(executable, path=None):
180 """Try to find 'executable' in the directories listed in 'path' (a
181 string listing directories separated by 'os.pathsep'; defaults to
182 os.environ['PATH']). Returns the complete filename or None if not
183 found.
184 """
185 if path is None:
186 path = os.environ['PATH']
187 paths = string.split(path, os.pathsep)
188 (base, ext) = os.path.splitext(executable)
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000189 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000190 executable = executable + '.exe'
191 if not os.path.isfile(executable):
192 for p in paths:
193 f = os.path.join(p, executable)
194 if os.path.isfile(f):
195 # the file exists, we have a shot at spawn working
196 return f
197 return None
198 else:
199 return executable
200
Fred Drakeb94b8492001-12-06 20:51:35 +0000201# find_executable()