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