blob: c69b42ed0e7bfbb7ac3dc0efc43affb3b376db0b [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001"""distutils.spawn
2
3Provides the 'spawn()' function, a front-end to various platform-
4specific functions for launching another program in a sub-process.
5Also provides the 'find_executable()' to search the path for a given
6executable name.
7"""
8
9# This module should be kept compatible with Python 2.1.
10
11__revision__ = "$Id: spawn.py 37828 2004-11-10 22:23:15Z loewis $"
12
13import sys, os, string
14from distutils.errors import *
15from distutils import log
16
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
23 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.
27
28 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,
31 the command will not actually be run.
32
33 Raise DistutilsExecError if running the program fails in any way; just
34 return on success.
35 """
36 if os.name == 'posix':
37 _spawn_posix(cmd, search_path, dry_run=dry_run)
38 elif os.name == 'nt':
39 _spawn_nt(cmd, search_path, dry_run=dry_run)
40 elif os.name == 'os2':
41 _spawn_os2(cmd, search_path, dry_run=dry_run)
42 elif os.name == 'java':
43 _spawn_java(cmd, search_path, dry_run=dry_run)
44 else:
45 raise DistutilsPlatformError, \
46 "don't know how to spawn programs on platform '%s'" % os.name
47
48# spawn ()
49
50
51def _nt_quote_args (args):
52 """Quote command-line arguments for DOS/Windows conventions: just
53 wraps every argument which contains blanks in double quotes, and
54 returns a new argument list.
55 """
56
57 # XXX this doesn't seem very robust to me -- but if the Windows guys
58 # say it'll work, I guess I'll have to accept it. (What if an arg
59 # contains quotes? What other magic characters, other than spaces,
60 # have to be escaped? Is there an escaping mechanism other than
61 # quoting?)
62
63 for i in range(len(args)):
64 if string.find(args[i], ' ') != -1:
65 args[i] = '"%s"' % args[i]
66 return args
67
68def _spawn_nt (cmd,
69 search_path=1,
70 verbose=0,
71 dry_run=0):
72
73 executable = cmd[0]
74 cmd = _nt_quote_args(cmd)
75 if search_path:
76 # either we find one or it stays the same
77 executable = find_executable(executable) or executable
78 log.info(string.join([executable] + cmd[1:], ' '))
79 if not dry_run:
80 # spawn for NT requires a full path to the .exe
81 try:
82 rc = os.spawnv(os.P_WAIT, executable, cmd)
83 except OSError, exc:
84 # this seems to happen when the command isn't found
85 raise DistutilsExecError, \
86 "command '%s' failed: %s" % (cmd[0], exc[-1])
87 if rc != 0:
88 # and this reflects the command running but failing
89 raise DistutilsExecError, \
90 "command '%s' failed with exit status %d" % (cmd[0], rc)
91
92
93def _spawn_os2 (cmd,
94 search_path=1,
95 verbose=0,
96 dry_run=0):
97
98 executable = cmd[0]
99 #cmd = _nt_quote_args(cmd)
100 if search_path:
101 # either we find one or it stays the same
102 executable = find_executable(executable) or executable
103 log.info(string.join([executable] + cmd[1:], ' '))
104 if not dry_run:
105 # spawnv for OS/2 EMX requires a full path to the .exe
106 try:
107 rc = os.spawnv(os.P_WAIT, executable, cmd)
108 except OSError, exc:
109 # this seems to happen when the command isn't found
110 raise DistutilsExecError, \
111 "command '%s' failed: %s" % (cmd[0], exc[-1])
112 if rc != 0:
113 # and this reflects the command running but failing
114 print "command '%s' failed with exit status %d" % (cmd[0], rc)
115 raise DistutilsExecError, \
116 "command '%s' failed with exit status %d" % (cmd[0], rc)
117
118
119def _spawn_posix (cmd,
120 search_path=1,
121 verbose=0,
122 dry_run=0):
123
124 log.info(string.join(cmd, ' '))
125 if dry_run:
126 return
127 exec_fn = search_path and os.execvp or os.execv
128
129 pid = os.fork()
130
131 if pid == 0: # in the child
132 try:
133 #print "cmd[0] =", cmd[0]
134 #print "cmd =", cmd
135 exec_fn(cmd[0], cmd)
136 except OSError, e:
137 sys.stderr.write("unable to execute %s: %s\n" %
138 (cmd[0], e.strerror))
139 os._exit(1)
140
141 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
142 os._exit(1)
143
144
145 else: # in the parent
146 # Loop until the child either exits or is terminated by a signal
147 # (ie. keep waiting if it's merely stopped)
148 while 1:
149 try:
150 (pid, status) = os.waitpid(pid, 0)
151 except OSError, exc:
152 import errno
153 if exc.errno == errno.EINTR:
154 continue
155 raise DistutilsExecError, \
156 "command '%s' failed: %s" % (cmd[0], exc[-1])
157 if os.WIFSIGNALED(status):
158 raise DistutilsExecError, \
159 "command '%s' terminated by signal %d" % \
160 (cmd[0], os.WTERMSIG(status))
161
162 elif os.WIFEXITED(status):
163 exit_status = os.WEXITSTATUS(status)
164 if exit_status == 0:
165 return # hey, it succeeded!
166 else:
167 raise DistutilsExecError, \
168 "command '%s' failed with exit status %d" % \
169 (cmd[0], exit_status)
170
171 elif os.WIFSTOPPED(status):
172 continue
173
174 else:
175 raise DistutilsExecError, \
176 "unknown error executing '%s': termination status %d" % \
177 (cmd[0], status)
178# _spawn_posix ()
179
180
181def _spawn_java(cmd,
182 search_path=1,
183 verbose=0,
184 dry_run=0):
185 executable = cmd[0]
186 cmd = ' '.join(_nt_quote_args(cmd))
187 log.info(cmd)
188 if not dry_run:
189 try:
190 rc = os.system(cmd) >> 8
191 except OSError, exc:
192 # this seems to happen when the command isn't found
193 raise DistutilsExecError, \
194 "command '%s' failed: %s" % (executable, exc[-1])
195 if rc != 0:
196 # and this reflects the command running but failing
197 print "command '%s' failed with exit status %d" % (executable, rc)
198 raise DistutilsExecError, \
199 "command '%s' failed with exit status %d" % (executable, rc)
200
201
202def find_executable(executable, path=None):
203 """Try to find 'executable' in the directories listed in 'path' (a
204 string listing directories separated by 'os.pathsep'; defaults to
205 os.environ['PATH']). Returns the complete filename or None if not
206 found.
207 """
208 if path is None:
209 path = os.environ['PATH']
210 paths = string.split(path, os.pathsep)
211 (base, ext) = os.path.splitext(executable)
212 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
213 executable = executable + '.exe'
214 if not os.path.isfile(executable):
215 for p in paths:
216 f = os.path.join(p, executable)
217 if os.path.isfile(f):
218 # the file exists, we have a shot at spawn working
219 return f
220 return None
221 else:
222 return executable
223
224# find_executable()