3 Provides the 'spawn()' function, a front-end to various platform-
4 specific functions for launching another program in a sub-process."""
6 # created 1999/07/24, Greg Ward
10 import sys
, os
, string
11 from distutils
.errors
import *
19 """Run another program, specified as a command list 'cmd', in a new
20 process. 'cmd' is just the argument list for the new process, ie.
21 cmd[0] is the program to run and cmd[1:] are the rest of its
22 arguments. There is no way to run a program with a name different
23 from that of its executable.
25 If 'search_path' is true (the default), the system's executable
26 search path will be used to find the program; otherwise, cmd[0] must
27 be the exact path to the executable. If 'verbose' is true, a
28 one-line summary of the command will be printed before it is run.
29 If 'dry_run' is true, the command will not actually be run.
31 Raise DistutilsExecError if running the program fails in any way;
32 just return on success."""
34 if os
.name
== 'posix':
35 _spawn_posix (cmd
, search_path
, verbose
, dry_run
)
37 _spawn_nt (cmd
, search_path
, verbose
, dry_run
)
39 raise DistutilsPlatformError
, \
40 "don't know how to spawn programs on platform '%s'" % os
.name
51 paths
= string
.split( os
.environ
['PATH'], os
.pathsep
)
52 base
,ext
= os
.path
.splitext(executable
)
54 executable
= executable
+ '.exe'
55 if not os
.path
.isfile(executable
):
56 paths
.reverse() # go over the paths and keep the last one
58 f
= os
.path
.join( p
, executable
)
59 if os
.path
.isfile ( f
):
60 # the file exists, we have a shot at spawn working
63 print string
.join ( [executable
] + cmd
[1:], ' ')
65 # spawn for NT requires a full path to the .exe
66 rc
= os
.spawnv (os
.P_WAIT
, executable
, cmd
)
68 raise DistutilsExecError("command failed: %d" % rc
)
72 def _spawn_posix (cmd
,
78 print string
.join (cmd
, ' ')
81 exec_fn
= search_path
and os
.execvp
or os
.execv
85 if pid
== 0: # in the child
87 #print "cmd[0] =", cmd[0]
91 sys
.stderr
.write ("unable to execute %s: %s\n" %
95 sys
.stderr
.write ("unable to execute %s for unknown reasons" % cmd
[0])
100 # Loop until the child either exits or is terminated by a signal
101 # (ie. keep waiting if it's merely stopped)
103 (pid
, status
) = os
.waitpid (pid
, 0)
104 if os
.WIFSIGNALED (status
):
105 raise DistutilsExecError
, \
106 "command %s terminated by signal %d" % \
107 (cmd
[0], os
.WTERMSIG (status
))
109 elif os
.WIFEXITED (status
):
110 exit_status
= os
.WEXITSTATUS (status
)
112 return # hey, it succeeded!
114 raise DistutilsExecError
, \
115 "command %s failed with exit status %d" % \
116 (cmd
[0], exit_status
)
118 elif os
.WIFSTOPPED (status
):
122 raise DistutilsExecError
, \
123 "unknown error executing %s: termination status %d" % \