blob: 5dd415a283d1de57af8c01c625d4415d42568cd2 [file] [log] [blame]
Greg Wardb4dbfb31999-08-14 23:57:17 +00001"""distutils.spawn
2
3Provides the 'spawn()' function, a front-end to various platform-
Greg Ward88608ca2000-08-02 01:08:02 +00004specific functions for launching another program in a sub-process.
5Also provides the 'find_executable()' to search the path for a given
Greg Warda30f7ac2000-09-26 02:00:51 +00006executable name.
7"""
Greg Wardb4dbfb31999-08-14 23:57:17 +00008
Tarek Ziadé861d6442009-06-02 16:18:55 +00009import sys
10import os
11
12from distutils.errors import DistutilsPlatformError, DistutilsExecError
Éric Araujo45fc8712014-03-13 04:55:35 -040013from distutils.debug import DEBUG
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000014from distutils import log
Greg Wardb4dbfb31999-08-14 23:57:17 +000015
Collin Winter5b7e9d72007-08-30 03:52:21 +000016def spawn(cmd, search_path=1, verbose=0, dry_run=0):
Tarek Ziadé861d6442009-06-02 16:18:55 +000017 """Run another program, specified as a command list 'cmd', in a new process.
18
19 'cmd' is just the argument list for the new process, ie.
Greg Warda30f7ac2000-09-26 02:00:51 +000020 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
21 There is no way to run a program with a name different from that of its
22 executable.
Greg Wardb4dbfb31999-08-14 23:57:17 +000023
Andrew M. Kuchlingfec32622002-11-21 20:41:07 +000024 If 'search_path' is true (the default), the system's executable
25 search path will be used to find the program; otherwise, cmd[0]
26 must be the exact path to the executable. If 'dry_run' is true,
Greg Warda30f7ac2000-09-26 02:00:51 +000027 the command will not actually be run.
Greg Wardb4dbfb31999-08-14 23:57:17 +000028
Greg Warda30f7ac2000-09-26 02:00:51 +000029 Raise DistutilsExecError if running the program fails in any way; just
30 return on success.
31 """
Éric Araujo45fc8712014-03-13 04:55:35 -040032 # cmd is documented as a list, but just in case some code passes a tuple
33 # in, protect our %-formatting code against horrible death
34 cmd = list(cmd)
Greg Wardb4dbfb31999-08-14 23:57:17 +000035 if os.name == 'posix':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000036 _spawn_posix(cmd, search_path, dry_run=dry_run)
Greg Warda4d132a1999-09-08 02:23:28 +000037 elif os.name == 'nt':
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000038 _spawn_nt(cmd, search_path, dry_run=dry_run)
Greg Wardb4dbfb31999-08-14 23:57:17 +000039 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +000040 raise DistutilsPlatformError(
41 "don't know how to spawn programs on platform '%s'" % os.name)
Greg Wardb4dbfb31999-08-14 23:57:17 +000042
Collin Winter5b7e9d72007-08-30 03:52:21 +000043def _nt_quote_args(args):
Tarek Ziadé861d6442009-06-02 16:18:55 +000044 """Quote command-line arguments for DOS/Windows conventions.
45
46 Just wraps every argument which contains blanks in double quotes, and
Greg Warda30f7ac2000-09-26 02:00:51 +000047 returns a new argument list.
48 """
Greg Warde2b44522000-03-07 03:25:20 +000049 # 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é861d6442009-06-02 16:18:55 +000054 for i, arg in enumerate(args):
55 if ' ' in arg:
56 args[i] = '"%s"' % arg
Greg Warda3c8bf32000-03-26 21:47:00 +000057 return args
Greg Warde2b44522000-03-07 03:25:20 +000058
Collin Winter5b7e9d72007-08-30 03:52:21 +000059def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
Greg Ward69628b01999-08-29 18:20:56 +000060 executable = cmd[0]
Greg Warda30f7ac2000-09-26 02:00:51 +000061 cmd = _nt_quote_args(cmd)
Greg Ward69628b01999-08-29 18:20:56 +000062 if search_path:
Greg Ward88608ca2000-08-02 01:08:02 +000063 # either we find one or it stays the same
Fred Drakeb94b8492001-12-06 20:51:35 +000064 executable = find_executable(executable) or executable
Neal Norwitz9d72bb42007-04-17 08:48:32 +000065 log.info(' '.join([executable] + cmd[1:]))
Greg Ward69628b01999-08-29 18:20:56 +000066 if not dry_run:
67 # spawn for NT requires a full path to the .exe
Greg Ward3b49c9b2000-01-17 21:57:55 +000068 try:
Greg Warda30f7ac2000-09-26 02:00:51 +000069 rc = os.spawnv(os.P_WAIT, executable, cmd)
Guido van Rossumb940e112007-01-10 16:19:56 +000070 except OSError as exc:
Greg Ward3b49c9b2000-01-17 21:57:55 +000071 # this seems to happen when the command isn't found
Éric Araujo45fc8712014-03-13 04:55:35 -040072 if not DEBUG:
73 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +000074 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -040075 "command %r failed: %s" % (cmd, exc.args[-1]))
Greg Ward69628b01999-08-29 18:20:56 +000076 if rc != 0:
Greg Ward3b49c9b2000-01-17 21:57:55 +000077 # and this reflects the command running but failing
Éric Araujo45fc8712014-03-13 04:55:35 -040078 if not DEBUG:
79 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +000080 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -040081 "command %r failed with exit status %d" % (cmd, rc))
Greg Wardb4dbfb31999-08-14 23:57:17 +000082
Ned Deilya8f8b502011-06-28 19:44:24 -070083if sys.platform == 'darwin':
84 from distutils import sysconfig
85 _cfg_target = None
86 _cfg_target_split = None
87
Collin Winter5b7e9d72007-08-30 03:52:21 +000088def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
Neal Norwitz9d72bb42007-04-17 08:48:32 +000089 log.info(' '.join(cmd))
Greg Wardb4dbfb31999-08-14 23:57:17 +000090 if dry_run:
91 return
Éric Araujo45fc8712014-03-13 04:55:35 -040092 executable = cmd[0]
Greg Wardb4dbfb31999-08-14 23:57:17 +000093 exec_fn = search_path and os.execvp or os.execv
Éric Araujo45fc8712014-03-13 04:55:35 -040094 env = None
Ned Deilya8f8b502011-06-28 19:44:24 -070095 if sys.platform == 'darwin':
96 global _cfg_target, _cfg_target_split
97 if _cfg_target is None:
98 _cfg_target = sysconfig.get_config_var(
99 'MACOSX_DEPLOYMENT_TARGET') or ''
100 if _cfg_target:
101 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
102 if _cfg_target:
103 # ensure that the deployment target of build process is not less
104 # than that used when the interpreter was built. This ensures
105 # extension modules are built with correct compatibility values
106 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
107 if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
108 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
109 'now "%s" but "%s" during configure'
110 % (cur_target, _cfg_target))
111 raise DistutilsPlatformError(my_msg)
112 env = dict(os.environ,
113 MACOSX_DEPLOYMENT_TARGET=cur_target)
114 exec_fn = search_path and os.execvpe or os.execve
Greg Warda30f7ac2000-09-26 02:00:51 +0000115 pid = os.fork()
Collin Winter5b7e9d72007-08-30 03:52:21 +0000116 if pid == 0: # in the child
Greg Wardb4dbfb31999-08-14 23:57:17 +0000117 try:
Éric Araujo45fc8712014-03-13 04:55:35 -0400118 if env is None:
119 exec_fn(executable, cmd)
120 else:
121 exec_fn(executable, cmd, env)
Guido van Rossumb940e112007-01-10 16:19:56 +0000122 except OSError as e:
Éric Araujo45fc8712014-03-13 04:55:35 -0400123 if not DEBUG:
124 cmd = executable
125 sys.stderr.write("unable to execute %r: %s\n"
126 % (cmd, e.strerror))
Greg Warda30f7ac2000-09-26 02:00:51 +0000127 os._exit(1)
Fred Drakeb94b8492001-12-06 20:51:35 +0000128
Éric Araujo45fc8712014-03-13 04:55:35 -0400129 if not DEBUG:
130 cmd = executable
131 sys.stderr.write("unable to execute %r for unknown reasons" % cmd)
Greg Warda30f7ac2000-09-26 02:00:51 +0000132 os._exit(1)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000133 else: # in the parent
Greg Wardb4dbfb31999-08-14 23:57:17 +0000134 # Loop until the child either exits or is terminated by a signal
135 # (ie. keep waiting if it's merely stopped)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000136 while True:
Hye-Shik Chang904de5b2004-02-24 23:54:17 +0000137 try:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000138 pid, status = os.waitpid(pid, 0)
Guido van Rossumb940e112007-01-10 16:19:56 +0000139 except OSError as exc:
Éric Araujo45fc8712014-03-13 04:55:35 -0400140 if not DEBUG:
141 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +0000142 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -0400143 "command %r failed: %s" % (cmd, exc.args[-1]))
Greg Warda30f7ac2000-09-26 02:00:51 +0000144 if os.WIFSIGNALED(status):
Éric Araujo45fc8712014-03-13 04:55:35 -0400145 if not DEBUG:
146 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +0000147 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -0400148 "command %r terminated by signal %d"
149 % (cmd, os.WTERMSIG(status)))
Greg Warda30f7ac2000-09-26 02:00:51 +0000150 elif os.WIFEXITED(status):
151 exit_status = os.WEXITSTATUS(status)
Greg Wardb4dbfb31999-08-14 23:57:17 +0000152 if exit_status == 0:
Tarek Ziadé861d6442009-06-02 16:18:55 +0000153 return # hey, it succeeded!
Greg Wardb4dbfb31999-08-14 23:57:17 +0000154 else:
Éric Araujo45fc8712014-03-13 04:55:35 -0400155 if not DEBUG:
156 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +0000157 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -0400158 "command %r failed with exit status %d"
159 % (cmd, exit_status))
Greg Warda30f7ac2000-09-26 02:00:51 +0000160 elif os.WIFSTOPPED(status):
Greg Wardb4dbfb31999-08-14 23:57:17 +0000161 continue
Greg Wardb4dbfb31999-08-14 23:57:17 +0000162 else:
Éric Araujo45fc8712014-03-13 04:55:35 -0400163 if not DEBUG:
164 cmd = executable
Collin Winter5b7e9d72007-08-30 03:52:21 +0000165 raise DistutilsExecError(
Éric Araujo45fc8712014-03-13 04:55:35 -0400166 "unknown error executing %r: termination status %d"
167 % (cmd, status))
Greg Ward88608ca2000-08-02 01:08:02 +0000168
Greg Ward88608ca2000-08-02 01:08:02 +0000169def find_executable(executable, path=None):
Tarek Ziadé861d6442009-06-02 16:18:55 +0000170 """Tries to find 'executable' in the directories listed in 'path'.
171
172 A string listing directories separated by 'os.pathsep'; defaults to
173 os.environ['PATH']. Returns the complete filename or None if not found.
Greg Ward88608ca2000-08-02 01:08:02 +0000174 """
175 if path is None:
176 path = os.environ['PATH']
Tarek Ziadé861d6442009-06-02 16:18:55 +0000177
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000178 paths = path.split(os.pathsep)
Tarek Ziadé861d6442009-06-02 16:18:55 +0000179 base, ext = os.path.splitext(executable)
180
Jesus Cead17833d2012-10-11 01:20:12 +0200181 if (sys.platform == 'win32') and (ext != '.exe'):
Greg Ward88608ca2000-08-02 01:08:02 +0000182 executable = executable + '.exe'
Tarek Ziadé861d6442009-06-02 16:18:55 +0000183
Greg Ward88608ca2000-08-02 01:08:02 +0000184 if not os.path.isfile(executable):
185 for p in paths:
186 f = os.path.join(p, executable)
187 if os.path.isfile(f):
188 # the file exists, we have a shot at spawn working
189 return f
190 return None
191 else:
192 return executable