Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 1 | """distutils.spawn |
| 2 | |
| 3 | Provides the 'spawn()' function, a front-end to various platform- |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 4 | specific functions for launching another program in a sub-process. |
| 5 | Also provides the 'find_executable()' to search the path for a given |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 6 | executable name. |
| 7 | """ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 8 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 9 | __revision__ = "$Id$" |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 10 | |
| 11 | import sys, os, string |
| 12 | from distutils.errors import * |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 13 | from distutils import log |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 14 | |
| 15 | def spawn (cmd, |
| 16 | search_path=1, |
| 17 | verbose=0, |
| 18 | dry_run=0): |
| 19 | |
| 20 | """Run another program, specified as a command list 'cmd', in a new |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 21 | process. 'cmd' is just the argument list for the new process, ie. |
| 22 | cmd[0] is the program to run and cmd[1:] are the rest of its arguments. |
| 23 | There is no way to run a program with a name different from that of its |
| 24 | executable. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 25 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 26 | If 'search_path' is true (the default), the system's executable search |
| 27 | path will be used to find the program; otherwise, cmd[0] must be the |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 28 | exact path to the executable.If 'dry_run' is true, |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 29 | the command will not actually be run. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 30 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 31 | Raise DistutilsExecError if running the program fails in any way; just |
| 32 | return on success. |
| 33 | """ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 34 | if os.name == 'posix': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 35 | _spawn_posix(cmd, search_path, dry_run=dry_run) |
Greg Ward | a4d132a | 1999-09-08 02:23:28 +0000 | [diff] [blame] | 36 | elif os.name == 'nt': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 37 | _spawn_nt(cmd, search_path, dry_run=dry_run) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 38 | elif os.name == 'os2': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 39 | _spawn_os2(cmd, search_path, dry_run=dry_run) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 40 | else: |
| 41 | raise DistutilsPlatformError, \ |
| 42 | "don't know how to spawn programs on platform '%s'" % os.name |
| 43 | |
| 44 | # spawn () |
| 45 | |
Greg Ward | a4d132a | 1999-09-08 02:23:28 +0000 | [diff] [blame] | 46 | |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 47 | def _nt_quote_args (args): |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 48 | """Quote command-line arguments for DOS/Windows conventions: just |
| 49 | wraps every argument which contains blanks in double quotes, and |
| 50 | returns a new argument list. |
| 51 | """ |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 52 | |
| 53 | # XXX this doesn't seem very robust to me -- but if the Windows guys |
| 54 | # say it'll work, I guess I'll have to accept it. (What if an arg |
| 55 | # contains quotes? What other magic characters, other than spaces, |
| 56 | # have to be escaped? Is there an escaping mechanism other than |
| 57 | # quoting?) |
| 58 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 59 | for i in range(len(args)): |
| 60 | if string.find(args[i], ' ') != -1: |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 61 | args[i] = '"%s"' % args[i] |
Greg Ward | a3c8bf3 | 2000-03-26 21:47:00 +0000 | [diff] [blame] | 62 | return args |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 63 | |
| 64 | def _spawn_nt (cmd, |
| 65 | search_path=1, |
| 66 | verbose=0, |
| 67 | dry_run=0): |
| 68 | |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 69 | executable = cmd[0] |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 70 | cmd = _nt_quote_args(cmd) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 71 | if search_path: |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 72 | # either we find one or it stays the same |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 73 | executable = find_executable(executable) or executable |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 74 | log.info(string.join([executable] + cmd[1:], ' ')) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 75 | if not dry_run: |
| 76 | # spawn for NT requires a full path to the .exe |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 77 | try: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 78 | rc = os.spawnv(os.P_WAIT, executable, cmd) |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 79 | except OSError, exc: |
| 80 | # this seems to happen when the command isn't found |
| 81 | raise DistutilsExecError, \ |
| 82 | "command '%s' failed: %s" % (cmd[0], exc[-1]) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 83 | if rc != 0: |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 84 | # and this reflects the command running but failing |
| 85 | raise DistutilsExecError, \ |
| 86 | "command '%s' failed with exit status %d" % (cmd[0], rc) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 87 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 88 | |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 89 | def _spawn_os2 (cmd, |
| 90 | search_path=1, |
| 91 | verbose=0, |
| 92 | dry_run=0): |
| 93 | |
| 94 | executable = cmd[0] |
| 95 | #cmd = _nt_quote_args(cmd) |
| 96 | if search_path: |
| 97 | # either we find one or it stays the same |
| 98 | executable = find_executable(executable) or executable |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 99 | log.info(string.join([executable] + cmd[1:], ' ')) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 100 | if not dry_run: |
| 101 | # spawnv for OS/2 EMX requires a full path to the .exe |
| 102 | try: |
| 103 | rc = os.spawnv(os.P_WAIT, executable, cmd) |
| 104 | except OSError, exc: |
| 105 | # this seems to happen when the command isn't found |
| 106 | raise DistutilsExecError, \ |
| 107 | "command '%s' failed: %s" % (cmd[0], exc[-1]) |
| 108 | if rc != 0: |
| 109 | # and this reflects the command running but failing |
| 110 | print "command '%s' failed with exit status %d" % (cmd[0], rc) |
| 111 | raise DistutilsExecError, \ |
| 112 | "command '%s' failed with exit status %d" % (cmd[0], rc) |
| 113 | |
| 114 | |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 115 | def _spawn_posix (cmd, |
| 116 | search_path=1, |
| 117 | verbose=0, |
| 118 | dry_run=0): |
| 119 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 120 | log.info(string.join(cmd, ' ')) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 121 | if dry_run: |
| 122 | return |
| 123 | exec_fn = search_path and os.execvp or os.execv |
| 124 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 125 | pid = os.fork() |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 126 | |
| 127 | if pid == 0: # in the child |
| 128 | try: |
| 129 | #print "cmd[0] =", cmd[0] |
| 130 | #print "cmd =", cmd |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 131 | exec_fn(cmd[0], cmd) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 132 | except OSError, e: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 133 | sys.stderr.write("unable to execute %s: %s\n" % |
| 134 | (cmd[0], e.strerror)) |
| 135 | os._exit(1) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 136 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 137 | sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0]) |
| 138 | os._exit(1) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 139 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 140 | |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 141 | else: # in the parent |
| 142 | # Loop until the child either exits or is terminated by a signal |
| 143 | # (ie. keep waiting if it's merely stopped) |
| 144 | while 1: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 145 | (pid, status) = os.waitpid(pid, 0) |
| 146 | if os.WIFSIGNALED(status): |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 147 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 148 | "command '%s' terminated by signal %d" % \ |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 149 | (cmd[0], os.WTERMSIG(status)) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 150 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 151 | elif os.WIFEXITED(status): |
| 152 | exit_status = os.WEXITSTATUS(status) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 153 | if exit_status == 0: |
| 154 | return # hey, it succeeded! |
| 155 | else: |
| 156 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 157 | "command '%s' failed with exit status %d" % \ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 158 | (cmd[0], exit_status) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 159 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 160 | elif os.WIFSTOPPED(status): |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 161 | continue |
| 162 | |
| 163 | else: |
| 164 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 165 | "unknown error executing '%s': termination status %d" % \ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 166 | (cmd[0], status) |
| 167 | # _spawn_posix () |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | def find_executable(executable, path=None): |
| 171 | """Try to find 'executable' in the directories listed in 'path' (a |
| 172 | string listing directories separated by 'os.pathsep'; defaults to |
| 173 | os.environ['PATH']). Returns the complete filename or None if not |
| 174 | found. |
| 175 | """ |
| 176 | if path is None: |
| 177 | path = os.environ['PATH'] |
| 178 | paths = string.split(path, os.pathsep) |
| 179 | (base, ext) = os.path.splitext(executable) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 180 | if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'): |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 181 | executable = executable + '.exe' |
| 182 | if not os.path.isfile(executable): |
| 183 | for p in paths: |
| 184 | f = os.path.join(p, executable) |
| 185 | if os.path.isfile(f): |
| 186 | # the file exists, we have a shot at spawn working |
| 187 | return f |
| 188 | return None |
| 189 | else: |
| 190 | return executable |
| 191 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 192 | # find_executable() |