blob: 5b6016e0d8edc6f8126def31bafa504ae27289f1 [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
9# created 1999/07/24, Greg Ward
10
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 *
15
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
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
Greg Warda30f7ac2000-09-26 02:00:51 +000028 If 'search_path' is true (the default), the system's executable search
29 path will be used to find the program; otherwise, cmd[0] must be the
30 exact path to the executable. If 'verbose' is true, a one-line summary
31 of the command will be printed before it is run. If 'dry_run' is true,
32 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000033
Greg Warda30f7ac2000-09-26 02:00:51 +000034 Raise DistutilsExecError if running the program fails in any way; just
35 return on success.
36 """
Greg Wardb4dbfb31999-08-14 23:57:17 +000037 if os.name == 'posix':
Greg Warda30f7ac2000-09-26 02:00:51 +000038 _spawn_posix(cmd, search_path, verbose, dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000039 elif os.name == 'nt':
Greg Warda30f7ac2000-09-26 02:00:51 +000040 _spawn_nt(cmd, search_path, verbose, dry_run)
Marc-André Lemburg2544f512002-01-31 18:56:00 +000041 elif os.name == 'os2':
42 _spawn_os2(cmd, search_path, verbose, dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000043 else:
44 raise DistutilsPlatformError, \
45 "don't know how to spawn programs on platform '%s'" % os.name
46
47# spawn ()
48
Greg Warda4d132a1999-09-08 02:23:28 +000049
Greg Warde2b44522000-03-07 03:25:20 +000050def _nt_quote_args (args):
Greg Warda30f7ac2000-09-26 02:00:51 +000051 """Quote command-line arguments for DOS/Windows conventions: just
52 wraps every argument which contains blanks in double quotes, and
53 returns a new argument list.
54 """
Greg Warde2b44522000-03-07 03:25:20 +000055
56 # XXX this doesn't seem very robust to me -- but if the Windows guys
57 # say it'll work, I guess I'll have to accept it. (What if an arg
58 # contains quotes? What other magic characters, other than spaces,
59 # have to be escaped? Is there an escaping mechanism other than
60 # quoting?)
61
Greg Warda30f7ac2000-09-26 02:00:51 +000062 for i in range(len(args)):
63 if string.find(args[i], ' ') != -1:
Greg Warde2b44522000-03-07 03:25:20 +000064 args[i] = '"%s"' % args[i]
Greg Warda3c8bf32000-03-26 21:47:00 +000065 return args
Greg Warde2b44522000-03-07 03:25:20 +000066
67def _spawn_nt (cmd,
68 search_path=1,
69 verbose=0,
70 dry_run=0):
71
Greg Ward69628b01999-08-29 18:20:56 +000072 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000073 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000074 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000075 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000076 executable = find_executable(executable) or executable
Greg Ward69628b01999-08-29 18:20:56 +000077 if verbose:
Greg Warda30f7ac2000-09-26 02:00:51 +000078 print string.join([executable] + cmd[1:], ' ')
Greg Ward69628b01999-08-29 18:20:56 +000079 if not dry_run:
80 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000081 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000082 rc = os.spawnv(os.P_WAIT, executable, cmd)
Greg Ward3b49c9b2000-01-17 21:57:55 +000083 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])
Greg Ward69628b01999-08-29 18:20:56 +000087 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000088 # and this reflects the command running but failing
89 raise DistutilsExecError, \
90 "command '%s' failed with exit status %d" % (cmd[0], rc)
Greg Wardb4dbfb31999-08-14 23:57:17 +000091
Fred Drakeb94b8492001-12-06 20:51:35 +000092
Marc-André Lemburg2544f512002-01-31 18:56:00 +000093def _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 if verbose:
104 print string.join([executable] + cmd[1:], ' ')
105 if not dry_run:
106 # spawnv for OS/2 EMX requires a full path to the .exe
107 try:
108 rc = os.spawnv(os.P_WAIT, executable, cmd)
109 except OSError, exc:
110 # this seems to happen when the command isn't found
111 raise DistutilsExecError, \
112 "command '%s' failed: %s" % (cmd[0], exc[-1])
113 if rc != 0:
114 # and this reflects the command running but failing
115 print "command '%s' failed with exit status %d" % (cmd[0], rc)
116 raise DistutilsExecError, \
117 "command '%s' failed with exit status %d" % (cmd[0], rc)
118
119
Greg Wardb4dbfb31999-08-14 23:57:17 +0000120def _spawn_posix (cmd,
121 search_path=1,
122 verbose=0,
123 dry_run=0):
124
125 if verbose:
Greg Warda30f7ac2000-09-26 02:00:51 +0000126 print string.join(cmd, ' ')
Greg Wardb4dbfb31999-08-14 23:57:17 +0000127 if dry_run:
128 return
129 exec_fn = search_path and os.execvp or os.execv
130
Greg Warda30f7ac2000-09-26 02:00:51 +0000131 pid = os.fork()
Greg Wardb4dbfb31999-08-14 23:57:17 +0000132
133 if pid == 0: # in the child
134 try:
135 #print "cmd[0] =", cmd[0]
136 #print "cmd =", cmd
Greg Warda30f7ac2000-09-26 02:00:51 +0000137 exec_fn(cmd[0], cmd)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000138 except OSError, e:
Greg Warda30f7ac2000-09-26 02:00:51 +0000139 sys.stderr.write("unable to execute %s: %s\n" %
140 (cmd[0], e.strerror))
141 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000142
Greg Warda30f7ac2000-09-26 02:00:51 +0000143 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
144 os._exit(1)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000145
Fred Drakeb94b8492001-12-06 20:51:35 +0000146
Greg Wardb4dbfb31999-08-14 23:57:17 +0000147 else: # in the parent
148 # Loop until the child either exits or is terminated by a signal
149 # (ie. keep waiting if it's merely stopped)
150 while 1:
Greg Warda30f7ac2000-09-26 02:00:51 +0000151 (pid, status) = os.waitpid(pid, 0)
152 if os.WIFSIGNALED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000153 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000154 "command '%s' terminated by signal %d" % \
Greg Warda30f7ac2000-09-26 02:00:51 +0000155 (cmd[0], os.WTERMSIG(status))
Greg Wardb4dbfb31999-08-14 23:57:17 +0000156
Greg Warda30f7ac2000-09-26 02:00:51 +0000157 elif os.WIFEXITED(status):
158 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000159 if exit_status == 0:
160 return # hey, it succeeded!
161 else:
162 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000163 "command '%s' failed with exit status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000164 (cmd[0], exit_status)
Fred Drakeb94b8492001-12-06 20:51:35 +0000165
Greg Warda30f7ac2000-09-26 02:00:51 +0000166 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000167 continue
168
169 else:
170 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000171 "unknown error executing '%s': termination status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000172 (cmd[0], status)
173# _spawn_posix ()
Greg Ward88608ca2000-08-02 01:08:02 +0000174
175
176def find_executable(executable, path=None):
177 """Try to find 'executable' in the directories listed in 'path' (a
178 string listing directories separated by 'os.pathsep'; defaults to
179 os.environ['PATH']). Returns the complete filename or None if not
180 found.
181 """
182 if path is None:
183 path = os.environ['PATH']
184 paths = string.split(path, os.pathsep)
185 (base, ext) = os.path.splitext(executable)
Marc-André Lemburg2544f512002-01-31 18:56:00 +0000186 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000187 executable = executable + '.exe'
188 if not os.path.isfile(executable):
189 for p in paths:
190 f = os.path.join(p, executable)
191 if os.path.isfile(f):
192 # the file exists, we have a shot at spawn working
193 return f
194 return None
195 else:
196 return executable
197
Fred Drakeb94b8492001-12-06 20:51:35 +0000198# find_executable()