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