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 | |
| 9 | # created 1999/07/24, Greg Ward |
| 10 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 11 | __revision__ = "$Id$" |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 12 | |
| 13 | import sys, os, string |
| 14 | from distutils.errors import * |
| 15 | |
| 16 | |
| 17 | def spawn (cmd, |
| 18 | search_path=1, |
| 19 | verbose=0, |
| 20 | dry_run=0): |
| 21 | |
| 22 | """Run another program, specified as a command list 'cmd', in a new |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 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 |
| 26 | executable. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 27 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 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. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 33 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 34 | Raise DistutilsExecError if running the program fails in any way; just |
| 35 | return on success. |
| 36 | """ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 37 | if os.name == 'posix': |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 38 | _spawn_posix(cmd, search_path, verbose, dry_run) |
Greg Ward | a4d132a | 1999-09-08 02:23:28 +0000 | [diff] [blame] | 39 | elif os.name == 'nt': |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 40 | _spawn_nt(cmd, search_path, verbose, dry_run) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 41 | else: |
| 42 | raise DistutilsPlatformError, \ |
| 43 | "don't know how to spawn programs on platform '%s'" % os.name |
| 44 | |
| 45 | # spawn () |
| 46 | |
Greg Ward | a4d132a | 1999-09-08 02:23:28 +0000 | [diff] [blame] | 47 | |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 48 | def _nt_quote_args (args): |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 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. |
| 52 | """ |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 53 | |
| 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 |
| 58 | # quoting?) |
| 59 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 60 | for i in range(len(args)): |
| 61 | if string.find(args[i], ' ') != -1: |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 62 | args[i] = '"%s"' % args[i] |
Greg Ward | a3c8bf3 | 2000-03-26 21:47:00 +0000 | [diff] [blame] | 63 | return args |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 64 | |
| 65 | def _spawn_nt (cmd, |
| 66 | search_path=1, |
| 67 | verbose=0, |
| 68 | dry_run=0): |
| 69 | |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 70 | executable = cmd[0] |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 71 | cmd = _nt_quote_args(cmd) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 72 | if search_path: |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 73 | # either we find one or it stays the same |
| 74 | executable = find_executable(executable) or executable |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 75 | if verbose: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 76 | print string.join([executable] + cmd[1:], ' ') |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 77 | if not dry_run: |
| 78 | # spawn for NT requires a full path to the .exe |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 79 | try: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 80 | rc = os.spawnv(os.P_WAIT, executable, cmd) |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 81 | except OSError, exc: |
| 82 | # this seems to happen when the command isn't found |
| 83 | raise DistutilsExecError, \ |
| 84 | "command '%s' failed: %s" % (cmd[0], exc[-1]) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 85 | if rc != 0: |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 86 | # and this reflects the command running but failing |
| 87 | raise DistutilsExecError, \ |
| 88 | "command '%s' failed with exit status %d" % (cmd[0], rc) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 89 | |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 90 | |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 91 | def _spawn_posix (cmd, |
| 92 | search_path=1, |
| 93 | verbose=0, |
| 94 | dry_run=0): |
| 95 | |
| 96 | if verbose: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 97 | print string.join(cmd, ' ') |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 98 | if dry_run: |
| 99 | return |
| 100 | exec_fn = search_path and os.execvp or os.execv |
| 101 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 102 | pid = os.fork() |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 103 | |
| 104 | if pid == 0: # in the child |
| 105 | try: |
| 106 | #print "cmd[0] =", cmd[0] |
| 107 | #print "cmd =", cmd |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 108 | exec_fn(cmd[0], cmd) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 109 | except OSError, e: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 110 | sys.stderr.write("unable to execute %s: %s\n" % |
| 111 | (cmd[0], e.strerror)) |
| 112 | os._exit(1) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 113 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 114 | sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0]) |
| 115 | os._exit(1) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 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) |
| 121 | while 1: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 122 | (pid, status) = os.waitpid(pid, 0) |
| 123 | if os.WIFSIGNALED(status): |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 124 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 125 | "command '%s' terminated by signal %d" % \ |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 126 | (cmd[0], os.WTERMSIG(status)) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 127 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 128 | elif os.WIFEXITED(status): |
| 129 | exit_status = os.WEXITSTATUS(status) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 130 | if exit_status == 0: |
| 131 | return # hey, it succeeded! |
| 132 | else: |
| 133 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 134 | "command '%s' failed with exit status %d" % \ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 135 | (cmd[0], exit_status) |
| 136 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 137 | elif os.WIFSTOPPED(status): |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 138 | continue |
| 139 | |
| 140 | else: |
| 141 | raise DistutilsExecError, \ |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 142 | "unknown error executing '%s': termination status %d" % \ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 143 | (cmd[0], status) |
| 144 | # _spawn_posix () |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 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 |
| 151 | found. |
| 152 | """ |
| 153 | if path is None: |
| 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): |
| 160 | for p in paths: |
| 161 | f = os.path.join(p, executable) |
| 162 | if os.path.isfile(f): |
| 163 | # the file exists, we have a shot at spawn working |
| 164 | return f |
| 165 | return None |
| 166 | else: |
| 167 | return executable |
| 168 | |
| 169 | # find_executable() |