blob: 847346cc2f9da93f8abfa20047041680e082549b [file] [log] [blame]
Greg Wardb4dbfb31999-08-14 23:57:17 +00001"""distutils.spawn
2
3Provides the 'spawn()' function, a front-end to various platform-
4specific functions for launching another program in a sub-process."""
5
6# created 1999/07/24, Greg Ward
7
Greg Ward3ce77fd2000-03-02 01:49:45 +00008__revision__ = "$Id$"
Greg Wardb4dbfb31999-08-14 23:57:17 +00009
10import sys, os, string
11from distutils.errors import *
12
13
14def spawn (cmd,
15 search_path=1,
16 verbose=0,
17 dry_run=0):
18
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.
24
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.
30
31 Raise DistutilsExecError if running the program fails in any way;
32 just return on success."""
33
34 if os.name == 'posix':
35 _spawn_posix (cmd, search_path, verbose, dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000036 elif os.name == 'nt':
Greg Ward69628b01999-08-29 18:20:56 +000037 _spawn_nt (cmd, search_path, verbose, dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000038 else:
39 raise DistutilsPlatformError, \
40 "don't know how to spawn programs on platform '%s'" % os.name
41
42# spawn ()
43
Greg Warda4d132a1999-09-08 02:23:28 +000044
Greg Ward69628b01999-08-29 18:20:56 +000045def _spawn_nt ( cmd,
46 search_path=1,
47 verbose=0,
48 dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000049 executable = cmd[0]
50 if search_path:
51 paths = string.split( os.environ['PATH'], os.pathsep)
52 base,ext = os.path.splitext(executable)
53 if (ext != '.exe'):
54 executable = executable + '.exe'
55 if not os.path.isfile(executable):
56 paths.reverse() # go over the paths and keep the last one
57 for p in paths:
58 f = os.path.join( p, executable )
59 if os.path.isfile ( f ):
60 # the file exists, we have a shot at spawn working
61 executable = f
62 if verbose:
63 print string.join ( [executable] + cmd[1:], ' ')
64 if not dry_run:
65 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000066 try:
67 rc = os.spawnv (os.P_WAIT, executable, cmd)
68 except OSError, exc:
69 # this seems to happen when the command isn't found
70 raise DistutilsExecError, \
71 "command '%s' failed: %s" % (cmd[0], exc[-1])
Greg Ward69628b01999-08-29 18:20:56 +000072 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000073 # and this reflects the command running but failing
74 raise DistutilsExecError, \
75 "command '%s' failed with exit status %d" % (cmd[0], rc)
Greg Wardb4dbfb31999-08-14 23:57:17 +000076
Greg Ward69628b01999-08-29 18:20:56 +000077
78
Greg Wardb4dbfb31999-08-14 23:57:17 +000079def _spawn_posix (cmd,
80 search_path=1,
81 verbose=0,
82 dry_run=0):
83
84 if verbose:
85 print string.join (cmd, ' ')
86 if dry_run:
87 return
88 exec_fn = search_path and os.execvp or os.execv
89
90 pid = os.fork ()
91
92 if pid == 0: # in the child
93 try:
94 #print "cmd[0] =", cmd[0]
95 #print "cmd =", cmd
96 exec_fn (cmd[0], cmd)
97 except OSError, e:
98 sys.stderr.write ("unable to execute %s: %s\n" %
99 (cmd[0], e.strerror))
100 os._exit (1)
101
102 sys.stderr.write ("unable to execute %s for unknown reasons" % cmd[0])
103 os._exit (1)
104
105
106 else: # in the parent
107 # Loop until the child either exits or is terminated by a signal
108 # (ie. keep waiting if it's merely stopped)
109 while 1:
110 (pid, status) = os.waitpid (pid, 0)
111 if os.WIFSIGNALED (status):
112 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000113 "command '%s' terminated by signal %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000114 (cmd[0], os.WTERMSIG (status))
115
116 elif os.WIFEXITED (status):
117 exit_status = os.WEXITSTATUS (status)
118 if exit_status == 0:
119 return # hey, it succeeded!
120 else:
121 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000122 "command '%s' failed with exit status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000123 (cmd[0], exit_status)
124
125 elif os.WIFSTOPPED (status):
126 continue
127
128 else:
129 raise DistutilsExecError, \
Greg Ward3b49c9b2000-01-17 21:57:55 +0000130 "unknown error executing '%s': termination status %d" % \
Greg Wardb4dbfb31999-08-14 23:57:17 +0000131 (cmd[0], status)
132# _spawn_posix ()