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 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 11 | import sys |
| 12 | import os |
| 13 | |
| 14 | from distutils.errors import DistutilsPlatformError, DistutilsExecError |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 15 | from distutils.debug import DEBUG |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 16 | from distutils import log |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 17 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 18 | def spawn(cmd, search_path=1, verbose=0, dry_run=0): |
| 19 | """Run another program, specified as a command list 'cmd', in a new process. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 20 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 21 | 'cmd' is just the argument list for the new process, ie. |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | fec3262 | 2002-11-21 20:41:07 +0000 | [diff] [blame] | 26 | If 'search_path' is true (the default), the system's executable |
| 27 | search path will be used to find the program; otherwise, cmd[0] |
| 28 | must be the 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 | """ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 34 | # cmd is documented as a list, but just in case some code passes a tuple |
| 35 | # in, protect our %-formatting code against horrible death |
| 36 | cmd = list(cmd) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 37 | if os.name == 'posix': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 38 | _spawn_posix(cmd, search_path, dry_run=dry_run) |
Greg Ward | a4d132a | 1999-09-08 02:23:28 +0000 | [diff] [blame] | 39 | elif os.name == 'nt': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 40 | _spawn_nt(cmd, search_path, dry_run=dry_run) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 41 | elif os.name == 'os2': |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 42 | _spawn_os2(cmd, search_path, dry_run=dry_run) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 43 | else: |
| 44 | raise DistutilsPlatformError, \ |
| 45 | "don't know how to spawn programs on platform '%s'" % os.name |
| 46 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 47 | def _nt_quote_args(args): |
| 48 | """Quote command-line arguments for DOS/Windows conventions. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 49 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 50 | Just wraps every argument which contains blanks in double quotes, and |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 51 | returns a new argument list. |
| 52 | """ |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 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?) |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 58 | for i, arg in enumerate(args): |
| 59 | if ' ' in arg: |
| 60 | args[i] = '"%s"' % arg |
Greg Ward | a3c8bf3 | 2000-03-26 21:47:00 +0000 | [diff] [blame] | 61 | return args |
Greg Ward | e2b4452 | 2000-03-07 03:25:20 +0000 | [diff] [blame] | 62 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 63 | def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0): |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 64 | executable = cmd[0] |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 65 | cmd = _nt_quote_args(cmd) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 66 | if search_path: |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 67 | # either we find one or it stays the same |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 68 | executable = find_executable(executable) or executable |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 69 | log.info(' '.join([executable] + cmd[1:])) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 70 | if not dry_run: |
| 71 | # spawn for NT requires a full path to the .exe |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 72 | try: |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 73 | rc = os.spawnv(os.P_WAIT, executable, cmd) |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 74 | except OSError, exc: |
| 75 | # this seems to happen when the command isn't found |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 76 | if not DEBUG: |
| 77 | cmd = executable |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 78 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 79 | "command %r failed: %s" % (cmd, exc[-1]) |
Greg Ward | 69628b0 | 1999-08-29 18:20:56 +0000 | [diff] [blame] | 80 | if rc != 0: |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 81 | # and this reflects the command running but failing |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 82 | if not DEBUG: |
| 83 | cmd = executable |
Greg Ward | 3b49c9b | 2000-01-17 21:57:55 +0000 | [diff] [blame] | 84 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 85 | "command %r failed with exit status %d" % (cmd, rc) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 86 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 87 | def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0): |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 88 | executable = cmd[0] |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 89 | if search_path: |
| 90 | # either we find one or it stays the same |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 91 | executable = find_executable(executable) or executable |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 92 | log.info(' '.join([executable] + cmd[1:])) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 93 | if not dry_run: |
| 94 | # spawnv for OS/2 EMX requires a full path to the .exe |
| 95 | try: |
| 96 | rc = os.spawnv(os.P_WAIT, executable, cmd) |
| 97 | except OSError, exc: |
| 98 | # this seems to happen when the command isn't found |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 99 | if not DEBUG: |
| 100 | cmd = executable |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 101 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 102 | "command %r failed: %s" % (cmd, exc[-1]) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 103 | if rc != 0: |
| 104 | # and this reflects the command running but failing |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 105 | if not DEBUG: |
| 106 | cmd = executable |
| 107 | log.debug("command %r failed with exit status %d" % (cmd, rc)) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 108 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 109 | "command %r failed with exit status %d" % (cmd, rc) |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 110 | |
Ned Deily | 041645a | 2011-06-28 19:40:39 -0700 | [diff] [blame] | 111 | if sys.platform == 'darwin': |
| 112 | from distutils import sysconfig |
| 113 | _cfg_target = None |
| 114 | _cfg_target_split = None |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 115 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 116 | def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0): |
| 117 | log.info(' '.join(cmd)) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 118 | if dry_run: |
| 119 | return |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 120 | executable = cmd[0] |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 121 | exec_fn = search_path and os.execvp or os.execv |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 122 | env = None |
Ned Deily | 041645a | 2011-06-28 19:40:39 -0700 | [diff] [blame] | 123 | if sys.platform == 'darwin': |
| 124 | global _cfg_target, _cfg_target_split |
| 125 | if _cfg_target is None: |
| 126 | _cfg_target = sysconfig.get_config_var( |
| 127 | 'MACOSX_DEPLOYMENT_TARGET') or '' |
| 128 | if _cfg_target: |
| 129 | _cfg_target_split = [int(x) for x in _cfg_target.split('.')] |
| 130 | if _cfg_target: |
| 131 | # ensure that the deployment target of build process is not less |
| 132 | # than that used when the interpreter was built. This ensures |
| 133 | # extension modules are built with correct compatibility values |
| 134 | cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target) |
| 135 | if _cfg_target_split > [int(x) for x in cur_target.split('.')]: |
| 136 | my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: ' |
| 137 | 'now "%s" but "%s" during configure' |
| 138 | % (cur_target, _cfg_target)) |
| 139 | raise DistutilsPlatformError(my_msg) |
| 140 | env = dict(os.environ, |
| 141 | MACOSX_DEPLOYMENT_TARGET=cur_target) |
| 142 | exec_fn = search_path and os.execvpe or os.execve |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 143 | pid = os.fork() |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 144 | |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 145 | if pid == 0: # in the child |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 146 | try: |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 147 | if env is None: |
| 148 | exec_fn(executable, cmd) |
| 149 | else: |
| 150 | exec_fn(executable, cmd, env) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 151 | except OSError, e: |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 152 | if not DEBUG: |
| 153 | cmd = executable |
| 154 | sys.stderr.write("unable to execute %r: %s\n" % |
| 155 | (cmd, e.strerror)) |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 156 | os._exit(1) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 157 | |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 158 | if not DEBUG: |
| 159 | cmd = executable |
| 160 | sys.stderr.write("unable to execute %r for unknown reasons" % cmd) |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 161 | os._exit(1) |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 162 | else: # in the parent |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 163 | # Loop until the child either exits or is terminated by a signal |
| 164 | # (ie. keep waiting if it's merely stopped) |
| 165 | while 1: |
Hye-Shik Chang | 904de5b | 2004-02-24 23:54:17 +0000 | [diff] [blame] | 166 | try: |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 167 | pid, status = os.waitpid(pid, 0) |
Hye-Shik Chang | 904de5b | 2004-02-24 23:54:17 +0000 | [diff] [blame] | 168 | except OSError, exc: |
| 169 | import errno |
| 170 | if exc.errno == errno.EINTR: |
| 171 | continue |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 172 | if not DEBUG: |
| 173 | cmd = executable |
Hye-Shik Chang | 904de5b | 2004-02-24 23:54:17 +0000 | [diff] [blame] | 174 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 175 | "command %r failed: %s" % (cmd, exc[-1]) |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 176 | if os.WIFSIGNALED(status): |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 177 | if not DEBUG: |
| 178 | cmd = executable |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 179 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 180 | "command %r terminated by signal %d" % \ |
| 181 | (cmd, os.WTERMSIG(status)) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 182 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 183 | elif os.WIFEXITED(status): |
| 184 | exit_status = os.WEXITSTATUS(status) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 185 | if exit_status == 0: |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 186 | return # hey, it succeeded! |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 187 | else: |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 188 | if not DEBUG: |
| 189 | cmd = executable |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 190 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 191 | "command %r failed with exit status %d" % \ |
| 192 | (cmd, exit_status) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 193 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 194 | elif os.WIFSTOPPED(status): |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 195 | continue |
| 196 | |
| 197 | else: |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 198 | if not DEBUG: |
| 199 | cmd = executable |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 200 | raise DistutilsExecError, \ |
Éric Araujo | 31fe52d | 2014-03-12 22:19:39 -0400 | [diff] [blame] | 201 | "unknown error executing %r: termination status %d" % \ |
| 202 | (cmd, status) |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 203 | |
| 204 | def find_executable(executable, path=None): |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 205 | """Tries to find 'executable' in the directories listed in 'path'. |
| 206 | |
| 207 | A string listing directories separated by 'os.pathsep'; defaults to |
| 208 | os.environ['PATH']. Returns the complete filename or None if not found. |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 209 | """ |
| 210 | if path is None: |
| 211 | path = os.environ['PATH'] |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 212 | paths = path.split(os.pathsep) |
| 213 | base, ext = os.path.splitext(executable) |
| 214 | |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 215 | if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'): |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 216 | executable = executable + '.exe' |
Tarek Ziadé | abb4ec6 | 2009-06-02 15:58:43 +0000 | [diff] [blame] | 217 | |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 218 | if not os.path.isfile(executable): |
| 219 | for p in paths: |
| 220 | f = os.path.join(p, executable) |
| 221 | if os.path.isfile(f): |
| 222 | # the file exists, we have a shot at spawn working |
| 223 | return f |
| 224 | return None |
| 225 | else: |
| 226 | return executable |