3 Provides the 'spawn()' function, a front-end to various platform-
4 specific functions for launching another program in a sub-process.
5 Also provides the 'find_executable()' to search the path for a given
9 # created 1999/07/24, Greg Ward
13 import sys
, os
, string
14 from distutils
.errors
import *
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
28 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.
34 Raise DistutilsExecError if running the program fails in any way; just
37 if os
.name
== 'posix':
38 _spawn_posix(cmd
, search_path
, verbose
, dry_run
)
40 _spawn_nt(cmd
, search_path
, verbose
, dry_run
)
42 raise DistutilsPlatformError
, \
43 "don't know how to spawn programs on platform '%s'" % os
.name
48 def _nt_quote_args (args
):
49 """Quote command-line arguments for DOS/Windows conventions: just
50 wraps every argument which contains blanks in double quotes, and
51 returns a new argument list.
54 # XXX this doesn't seem very robust to me -- but if the Windows guys
55 # say it'll work, I guess I'll have to accept it. (What if an arg
56 # contains quotes? What other magic characters, other than spaces,
57 # have to be escaped? Is there an escaping mechanism other than
60 for i
in range(len(args
)):
61 if string
.find(args
[i
], ' ') != -1:
62 args
[i
] = '"%s"' % args
[i
]
71 cmd
= _nt_quote_args(cmd
)
73 # either we find one or it stays the same
74 executable
= find_executable(executable
) or executable
76 print string
.join([executable
] + cmd
[1:], ' ')
78 # spawn for NT requires a full path to the .exe
80 rc
= os
.spawnv(os
.P_WAIT
, executable
, cmd
)
82 # this seems to happen when the command isn't found
83 raise DistutilsExecError
, \
84 "command '%s' failed: %s" % (cmd
[0], exc
[-1])
86 # and this reflects the command running but failing
87 raise DistutilsExecError
, \
88 "command '%s' failed with exit status %d" % (cmd
[0], rc
)
91 def _spawn_posix (cmd
,
97 print string
.join(cmd
, ' ')
100 exec_fn
= search_path
and os
.execvp
or os
.execv
104 if pid
== 0: # in the child
106 #print "cmd[0] =", cmd[0]
110 sys
.stderr
.write("unable to execute %s: %s\n" %
111 (cmd
[0], e
.strerror
))
114 sys
.stderr
.write("unable to execute %s for unknown reasons" % cmd
[0])
118 else: # in the parent
119 # Loop until the child either exits or is terminated by a signal
120 # (ie. keep waiting if it's merely stopped)
122 (pid
, status
) = os
.waitpid(pid
, 0)
123 if os
.WIFSIGNALED(status
):
124 raise DistutilsExecError
, \
125 "command '%s' terminated by signal %d" % \
126 (cmd
[0], os
.WTERMSIG(status
))
128 elif os
.WIFEXITED(status
):
129 exit_status
= os
.WEXITSTATUS(status
)
131 return # hey, it succeeded!
133 raise DistutilsExecError
, \
134 "command '%s' failed with exit status %d" % \
135 (cmd
[0], exit_status
)
137 elif os
.WIFSTOPPED(status
):
141 raise DistutilsExecError
, \
142 "unknown error executing '%s': termination status %d" % \
147 def find_executable(executable
, path
=None):
148 """Try to find 'executable' in the directories listed in 'path' (a
149 string listing directories separated by 'os.pathsep'; defaults to
150 os.environ['PATH']). Returns the complete filename or None if not
154 path
= os
.environ
['PATH']
155 paths
= string
.split(path
, os
.pathsep
)
156 (base
, ext
) = os
.path
.splitext(executable
)
157 if (sys
.platform
== 'win32') and (ext
!= '.exe'):
158 executable
= executable
+ '.exe'
159 if not os
.path
.isfile(executable
):
161 f
= os
.path
.join(p
, executable
)
162 if os
.path
.isfile(f
):
163 # the file exists, we have a shot at spawn working