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
__revision__ = "$Id: spawn.py 59793 2008-01-06 21:13:42Z georg.brandl $"
12
from distutils.errors import *
13
from distutils import log
15
def spawn(cmd, search_path=1, verbose=0, dry_run=0):
16
"""Run another program, specified as a command list 'cmd', in a new
17
process. 'cmd' is just the argument list for the new process, ie.
18
cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
19
There is no way to run a program with a name different from that of its
22
If 'search_path' is true (the default), the system's executable
23
search path will be used to find the program; otherwise, cmd[0]
24
must be the exact path to the executable. If 'dry_run' is true,
25
the command will not actually be run.
27
Raise DistutilsExecError if running the program fails in any way; just
30
if os.name == 'posix':
31
_spawn_posix(cmd, search_path, dry_run=dry_run)
33
_spawn_nt(cmd, search_path, dry_run=dry_run)
34
elif os.name == 'os2':
35
_spawn_os2(cmd, search_path, dry_run=dry_run)
37
raise DistutilsPlatformError(
38
"don't know how to spawn programs on platform '%s'" % os.name)
41
def _nt_quote_args(args):
42
"""Quote command-line arguments for DOS/Windows conventions: just
43
wraps every argument which contains blanks in double quotes, and
44
returns a new argument list.
46
# XXX this doesn't seem very robust to me -- but if the Windows guys
47
# say it'll work, I guess I'll have to accept it. (What if an arg
48
# contains quotes? What other magic characters, other than spaces,
49
# have to be escaped? Is there an escaping mechanism other than
51
for i in range(len(args)):
52
if args[i].find(' ') != -1:
53
args[i] = '"%s"' % args[i]
56
def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
58
cmd = _nt_quote_args(cmd)
60
# either we find one or it stays the same
61
executable = find_executable(executable) or executable
62
log.info(' '.join([executable] + cmd[1:]))
64
# spawn for NT requires a full path to the .exe
66
rc = os.spawnv(os.P_WAIT, executable, cmd)
67
except OSError as exc:
68
# this seems to happen when the command isn't found
69
raise DistutilsExecError(
70
"command '%s' failed: %s" % (cmd[0], exc.args[-1]))
72
# and this reflects the command running but failing
73
raise DistutilsExecError(
74
"command '%s' failed with exit status %d" % (cmd[0], rc))
77
def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
79
#cmd = _nt_quote_args(cmd)
81
# either we find one or it stays the same
82
executable = find_executable(executable) or executable
83
log.info(' '.join([executable] + cmd[1:]))
85
# spawnv for OS/2 EMX requires a full path to the .exe
87
rc = os.spawnv(os.P_WAIT, executable, cmd)
88
except OSError as exc:
89
# this seems to happen when the command isn't found
90
raise DistutilsExecError(
91
"command '%s' failed: %s" % (cmd[0], exc.args[-1]))
93
# and this reflects the command running but failing
94
print("command '%s' failed with exit status %d" % (cmd[0], rc))
95
raise DistutilsExecError(
96
"command '%s' failed with exit status %d" % (cmd[0], rc))
99
def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
100
log.info(' '.join(cmd))
103
exec_fn = search_path and os.execvp or os.execv
106
if pid == 0: # in the child
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])
116
else: # in the parent
117
# Loop until the child either exits or is terminated by a signal
118
# (ie. keep waiting if it's merely stopped)
121
(pid, status) = os.waitpid(pid, 0)
122
except OSError as exc:
124
if exc.errno == errno.EINTR:
126
raise DistutilsExecError(
127
"command '%s' failed: %s" % (cmd[0], exc.args[-1]))
128
if os.WIFSIGNALED(status):
129
raise DistutilsExecError(
130
"command '%s' terminated by signal %d"
131
% (cmd[0], os.WTERMSIG(status)))
132
elif os.WIFEXITED(status):
133
exit_status = os.WEXITSTATUS(status)
135
return # hey, it succeeded!
137
raise DistutilsExecError(
138
"command '%s' failed with exit status %d"
139
% (cmd[0], exit_status))
140
elif os.WIFSTOPPED(status):
143
raise DistutilsExecError(
144
"unknown error executing '%s': termination status %d"
148
def find_executable(executable, path=None):
149
"""Try to find 'executable' in the directories listed in 'path' (a
150
string listing directories separated by 'os.pathsep'; defaults to
151
os.environ['PATH']). Returns the complete filename or None if not
155
path = os.environ['PATH']
156
paths = path.split(os.pathsep)
157
(base, ext) = os.path.splitext(executable)
158
if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
159
executable = executable + '.exe'
160
if not os.path.isfile(executable):
162
f = os.path.join(p, executable)
163
if os.path.isfile(f):
164
# the file exists, we have a shot at spawn working